سؤال

i would like to do a custom Range Validator but i'm not sure how to proceed in this. I need the Range validator to be active only if a boolean stay false, if the bool is true, the validation should not apply. Something like the RequiredIfFalse of foolproof validation.

[RequiredIfFalse("UnEquip", ErrorMessage = "Le champ 'Nombre de salariés' doit être     renseigné")]
    [Range(1, 1000000, ErrorMessage = "Le nombre de salariés doit être compris entre 1 et 1000000")]
    public int Salaries { get; set; }

something like this, but combining those 2 type of validation.

Thanks in advance

هل كانت مفيدة؟

المحلول

You need to perform this validation at model level, because it implies several properties at once. Check this link, for a similar issue : Make the email field required based on the dropdown list value selection

In your case, Validate will test your boolean and Salaries value, if false and Salaries > 1000000 or Salaries < 1, you'll return a new ValidationResult containing your error message.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top