3

I am using custom validation within Blazor, the validation is performed correctly, however when it is not valid the ValidationMessage For is not displayed, I tried adding ValidationSummary and here it does appear ErrorMessage. I hope someone can support me

enter image description here

 public class Domicilio
    {
        public int Id { get; set; }
        public DateTimeOffset FechaRegistro { get; set; }
        [Required(ErrorMessage ="Favor de capturar la calle")]
        public string Calle { get; set; }
        [Required(ErrorMessage = "Favor de capturar el número exterior")]
        public string NumeroExt { get; set; }
        public string NumeroInt { get; set; }
        [Range(1,int.MaxValue, ErrorMessage="Favor de seleccionar la colonia")]
        public int CodigoPostalId { get; set; }
        public CatCodigosPostales CodigoPostal { get; set; }
        [OtroCPValidation]
        public string OtroCodigoPostal { get; set; }
        public string OtraColonia { get; set; }
        public string OtraCiudad { get; set; }
        public string OtroEstado { get; set; }
        [Range(1,int.MaxValue,ErrorMessage="Favor seleccione el país")]
        public int PaisId { get; set; }
        public CatPaises Pais { get; set; }
        public string Observaciones { get; set; }
    }

public class OtroCPValidation : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var domicilio = (Domicilio)validationContext.ObjectInstance;
            if (domicilio.Pais.Pais != "México" && domicilio.OtroCodigoPostal == null)
            {
                return new ValidationResult($"Captura el código postal extranjero");
            }
                
            else
                return ValidationResult.Success;
        }
    }

<EditForm Model="Domicilio" OnValidSubmit="OnValidSubmit" style="height:75vh" class="row justify-content-center align-items-center">
    <DataAnnotationsValidator />
<div class="form-group">
                            <label>Código postal</label>
                            <div>
                                <SfMaskedTextBox Placeholder="Código postal" Mask="[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" @bind-Value="@Domicilio.OtroCodigoPostal"></SfMaskedTextBox>
                                <ValidationMessage For="@(() => Domicilio.OtroCodigoPostal)" />
                            </div>
                        </div>
</EditForm>
3
  • Hi, You need to provide some more detail. Add your code that does the validation. Commented Oct 22, 2021 at 17:12
  • Thaks i have added the code Commented Oct 23, 2021 at 0:21
  • Is SfMaskedTextBox a custom component of yours? If so, did you add the class="@Css" to the input controller? If you do have it then EditForm will add Invalid css class to the input field after a failed validation. Commented Oct 23, 2021 at 20:31

2 Answers 2

15

I was able to solve the problem I had, I was missing an overload in the return of the IsValid method, I leave the return as it should be, I just needed that change.

return new ValidationResult(ErrorMessage, new[] { validationContext.MemberName});
Sign up to request clarification or add additional context in comments.

Comments

0

Two things to check:

  1. Do a simple validator to always return an error.
using System.ComponentModel.DataAnnotations;

namespace StackOverflow.Answers
{
    public class AlwaysInvalidValidation : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            return new ValidationResult($"Error Message");
        }
    }
}
  1. Test with the basic Blazor Input controls.

  2. Refactor the logic so you return an error unless the value passes the logic test.

I've tested a version of your code and it works.

2 Comments

I have tried all three options and the result is the same, I am still looking for the reason, thank you for your contribution
OK. I'll post a working version tomorrow of an abbreviated version of your code for you to work with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.