Hello I'm using MVC Fool Proof Validation. to validate my model, and i need to use RequiredIfNotEmpty with two fields but i'm getting issues with it
Model
public class Conexionado{
[DisplayName("Conexión")]
[RequiredIfNotEmpty("Conex_BT2_Pos", ErrorMessage = "Error!")]
[RequiredIfNotEmpty("Conex_BT2_N", ErrorMessage = "Conex_BT2 Cant be empty if Conex_BT2_N isnt!")]
public string Conex_BT2 { get; set; }
public string Conex_BT2_N { get; set; }
[DisplayName("Ángulo BT")]
[Range(0, 11, ErrorMessage = "Incorrect number")]
public int? Conex_BT2_Pos { get; set; }
}
I have tried some like
[RequiredIfNotEmpty("Conex_BT2_Pos , Conex_BT2_N", ErrorMessage = "Error!")]
[RequiredIfNotEmpty("Conex_BT2_Pos || Conex_BT2_N", ErrorMessage = "Error!")]
But in this case, i can compile, but when i try to use Conex_BT2 i get
'System.NullReferenceException' en FoolproofValidation.dll
Someone know how i must deal with it?
Thanks!
This question has been answered here:
The
Foolproof.RequiredIfNotAttributederives fromFoolproof.ModelAwareValidationAttribute(which in turn derives fromSystem.ComponentModel.DataAnnotation.ValidationAttribute).ModelAwareValidationAttributeis marked with[AttributeUsage(AttributeTargets.Property)]. Refer source code. By default the theAllowMultipleparameter ofAttributeUsageisfalsewhich means that you can only apply the attribute once to a property. You have tried to apply it 3 times, hence the error.Having it
trueand allowing it to be applied multiple times would possibly cause problems in setting the$.validator.methodsand$.validator.unobtrusive.adaptersfunctions used by unobtrusive validation.You will need to use some other validation attributes or create your own
ValidationAtributethat implementsIClientValidatable, or rely on server side validation.Good luck, greetings!