logo
Tags down

shadow

Form Validation not working in Blazor 3.1


By : Narohna
Date : September 16 2020, 01:00 PM
Any of those help I had made a tiny mistake which went un-noticed, Validation started working when i changed the input type to submit
code :
 <input type="button" class="btn btn-primary" value="Save" />
   <input type="submit" class="btn btn-primary" value="Save" />


Share : facebook icon twitter icon

FuelPHP Form validation not working with IE (Internet Explorer) browser... Client side validation not working


By : Alex D
Date : March 29 2020, 07:55 AM
it fixes the issue Fuel just outputs HTML5 validation rules with the form elements. It's up to the browser to support them.
Google Chrome (16+) Mozilla Firefox (8+) and Internet Explorer (10+)

Symfony2 - form global validation not working if validation group is defined


By : Hal Pulaar
Date : March 29 2020, 07:55 AM
seems to work fine I found, that my validation for textarea is not working, if I'm not logged. If am I, it works OK. Here is my Comment.php entity: , So I found solution. My problem was here:
code :
     'validation_groups' => (is_null($this->user) ? 'not_logged' : 'Default'), // here I set validation group if user is logged or not
     'validation_groups' => (is_null($this->user) ? array('Default', 'not_logged') : array('Default')),

Blazor Webassembly validation not support individual field validation


By : user3425244
Date : March 29 2020, 07:55 AM
I hope this helps . Option 1
Divide your view model into several submodels "MySubmodel1", "MySubmodel2", etc. Then in your wizard-like form, have multiple elements, each specifying the corresponding submodel, ie. in the first set Model=@MySubmodel1, in the second set Model=@MySubmodel2, etc.

Blazor Modal Form Validation: You've to click the cancel button twice to close the modal when you delete a form field


By : user3595350
Date : March 29 2020, 07:55 AM
wish help you to fix your issue @Jaap, here's a solution that is based on the internals of Forms validation. I hope it'll satisfy you until a better solution is offered.
Forms validation support in Blazor, added to the EditContext object is performed on two level: object-level and field-level. When you click on the Submit button, the whole Model is validated. We've already seen that the Submit button works perfectly well, and does not allow you to submit unless the Model's fields' values are valid. When you hit the Cancel button and the Model's fields' values are valid, the dialog is closed without issue. But when one or more fields are having invalid values (as for instance, after clearing the email field), and you click on the Cancel button, a field-level validation instantly starts, before the code in the Cancel button's event handler has the slightest chance to do something. This behavior is by design and is repeated even when I used Fluent Validation instead of DataAnnotations validation. Conclusion: It is our limitations, not the system. We need to invest more time in learning Blazor. The solution I propose is to disable a field-level validation when we click on the Cancel button, and thus instantly close the dialog without any validation taking place at all.
code :
public class UserLogin
{
    public string Email { get; set; }
    public string Password { get; set; }
}
public class UserLoginValidator : AbstractValidator<UserLogin>
    {
       public UserLoginValidator()
     {

         RuleFor(user => user.Email).NotEmpty().WithMessage("You must enter an email address");
         RuleFor(user => user.Email).EmailAddress().WithMessage("You must provide a valid email address");
         RuleFor(user => user.Password).NotEmpty().WithMessage("You must enter a password");
         RuleFor(user => user.Password).MaximumLength(50).WithMessage("Password cannot be longer than 50 characters");
    }
 }
public class FluentValidationValidator : ComponentBase
{
    [CascadingParameter] EditContext CurrentEditContext { get; set; }
    [Parameter] public bool ShouldValidate { get; set; }

    protected override void OnInitialized()
    {
        if (CurrentEditContext == null)
        {
            throw new InvalidOperationException($"{nameof(FluentValidationValidator)} requires a cascading " +
                $"parameter of type {nameof(EditContext)}. For example, you can use {nameof(FluentValidationValidator)} " +
                $"inside an {nameof(EditForm)}.");
        }

        CurrentEditContext.AddFluentValidation(ShouldValidate);
    }
}
 public static class EditContextFluentValidationExtensions
{
    public static EditContext AddFluentValidation(this EditContext editContext, bool shouldValidate)
    {
        if (editContext == null)
        {
            throw new ArgumentNullException(nameof(editContext));
        }

        var messages = new ValidationMessageStore(editContext);

        editContext.OnValidationRequested +=
            (sender, eventArgs) => ValidateModel((EditContext)sender, messages);

        editContext.OnFieldChanged +=
            (sender, eventArgs) => ValidateField(editContext, messages, eventArgs.FieldIdentifier, shouldValidate);

        return editContext;
    }

    private static void ValidateModel(EditContext editContext, ValidationMessageStore messages)
    {
        var validator = GetValidatorForModel(editContext.Model);
        var validationResults = validator.Validate(editContext.Model);

        messages.Clear();
        foreach (var validationResult in validationResults.Errors)
        {
            messages.Add(editContext.Field(validationResult.PropertyName), validationResult.ErrorMessage);
        }

        editContext.NotifyValidationStateChanged();
    }

    private static void ValidateField(EditContext editContext, ValidationMessageStore messages, in FieldIdentifier fieldIdentifier, bool shouldValidate)
    {
        Console.WriteLine(fieldIdentifier.FieldName.ToString());

        if (shouldValidate)
        {
            var properties = new[] { fieldIdentifier.FieldName };
            var context = new FluentValidation.ValidationContext(fieldIdentifier.Model, new PropertyChain(), new MemberNameValidatorSelector(properties));

            var validator = GetValidatorForModel(fieldIdentifier.Model);
            var validationResults = validator.Validate(context);

            messages.Clear(fieldIdentifier);

            foreach (var validationResult in validationResults.Errors)
            {
                messages.Add(editContext.Field(validationResult.PropertyName), validationResult.ErrorMessage);
            }

            editContext.NotifyValidationStateChanged();
        }
    }

    private static IValidator GetValidatorForModel(object model)
    {
        var abstractValidatorType = typeof(AbstractValidator<>).MakeGenericType(model.GetType());
        var modelValidatorType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(t => t.IsSubclassOf(abstractValidatorType));
        var modelValidatorInstance = (IValidator)Activator.CreateInstance(modelValidatorType);

        return modelValidatorInstance;
    }
}
@page "/cancel"
@using System.ComponentModel.DataAnnotations;

<h3>Cancel Validation</h3>

<button type="submit" class="btn btn-primary" @onclick="Login">Login</button>
<hr />
<p>Status: @status</p>

@if (showModal)
{
    <div class="modal" tabindex="-1" role="dialog" style="display:block" id="taskModal">
        <div class="modal-dialog shadow-lg bg-white rounded" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Login</h5>
                </div>
                <div class="modal-body">
                    <EditForm Model="user" OnValidSubmit="HandleValidSubmit">
                        @*<DataAnnotationsValidator />*@
                        <FluentValidationValidator ShouldValidate="false" />
                        @*<ValidationSummary />*@
                        <div class="form-group row">
                            <label class="col-3 col-form-label">Email: </label>
                            <InputText class="col-8 form-control" @bind-Value="user.Email" />
                            <ValidationMessage For="@(() => user.Email)" />
                        </div>
                        <div class="form-group row">
                            <label class="col-3 col-form-label">Password: </label>
                            <InputText type="password" class="col-8 form-control" @bind-Value="user.Password" />
                            <ValidationMessage For="@(() => user.Password)" />
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary">Submit</button>
                            <button type="button" class="btn btn-secondary" @onclick="CancelSubmit">Cancel</button>
                        </div>
                    </EditForm>
                </div>
            </div>
        </div>
    </div>
}

@code {
    UserLogin user = new UserLogin();

    bool showModal;
    string status;

    protected override void OnInitialized()
    {
        showModal = false;
        status = "Init";
        // for demo purposes, if you delete user.Email in the login dialog, you'll need to press 'Cancel' 2 times.
        user.Email = "user@example.com";
        user.Password = "12345";
    }

    private void Login()
    {
        status = "Show Login Modal";

        showModal = true;
    }

    private void HandleValidSubmit()
    {
        status = "Valid Submit";
        showModal = false;
    }

    private void CancelSubmit()
    {
        Console.WriteLine("CancelSubmit");

        status = "Cancelled";
        showModal = false;
    }
}

Blazor form failing validation due to items in the data model that are not shown in the form


By : user3659696
Date : March 29 2020, 07:55 AM
this will help Simon, check this information microsoft docs, about
Related Posts Related Posts :
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk