Skip to content

[Bug] [Form Validation Context] Following the documentation example does not reproduce the expected result #1262

Description

@afshin29

What happened?

I am following the Forms docs. The following example does not give me the promised result

https://microsoft.github.io/microsoft-ui-reactor/latest/forms/#validation-context

example code:

class ValidationContextDemo : Component
{
	public override Element Render()
	{
		var ctx = this.UseValidationContext();
		var (email, setEmail) = UseState("");
		var (password, setPassword) = UseState("");
		var (submitted, setSubmitted) = UseState(false);

		return VStack(12,
			SubHeading("Validation Context"),
			TextBox(email, v => { setEmail(v); ctx.NotifyValueChanged("email", v); },
				placeholderText: "user@example.com", header: "Email")
				.Validate("email", email,
					Validate.Required(),
					Validate.Email()),
			When(ctx.IsTouched("email") && ctx.HasError("email"), () =>
				TextBlock(ctx.GetMessages("email").First().Text)
					.Foreground(Theme.SystemCritical).FontSize(12)),
			PasswordBox(password, v => { setPassword(v); ctx.NotifyValueChanged("password", v); },
				placeholderText: "Min 8 characters")
				.Header("Password")
				.Validate("password", password,
					Validate.Required(),
					Validate.MinLength(8)),
			When(ctx.IsTouched("password") && ctx.HasError("password"), () =>
				TextBlock(ctx.GetMessages("password").First().Text)
					.Foreground(Theme.SystemCritical).FontSize(12)),
			Button("Register", () =>
			{
				ctx.MarkAllTouched();
				System.Diagnostics.Debug.WriteLine(
					$"email errors: {ctx.GetMessages("email").Count()}, " +
					$"password errors: {ctx.GetMessages("password").Count()}, " +
					$"valid: {ctx.IsValid()}");
				if (ctx.IsValid()) setSubmitted(true);
			}).IsEnabled(!submitted),
			When(submitted, () =>
				TextBlock("Registration successful!")
					.Foreground(Theme.SystemSuccess).SemiBold())
		).Padding(24)
		.RequestedTheme(ElementTheme.Light)
		.Backdrop(BackdropKind.Mica);
	}
}

When I click the "Register" button, the form gets submitted successfully without any error.

Image

Steps to reproduce

  1. Follow the get started guide to setup the Reactor (preview 15)
  2. Follow the validation context example from docs (https://microsoft.github.io/microsoft-ui-reactor/latest/forms/#validation-context)

Reactor version / commit

0.1.0-preview.15

Platform

x64

.NET SDK version

10.0.401

Windows version

Windows 11 25H2 (26200.9457)

Windows App SDK version

2.5.1

Logs / stack trace


Confirmation

  • I have searched existing issues and this isn't a duplicate.
  • This bug reproduces against the current main branch (or I've noted otherwise above).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions