-2

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!

Juan Salvador Portugal
  • 1,233
  • 4
  • 20
  • 38
  • 1
    You need to create you own custom validation attribute. [This answer](https://stackoverflow.com/questions/28504122/foolproof-multiple-validators-on-the-same-fields/28512316#28512316) explains why you cannot apply the same `ValidationAttribute` more than once. For a good article to get you started, refer [The Complete Guide To Validation In ASP.NET MVC 3 - Part 2](https://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2). Have a go, and if your still having problems, post what you have tried –  Apr 19 '18 at 22:52

1 Answers1

-1

This question has been answered here:

Foolproof multiple validators on the same fields by Stephen Muecke

The Foolproof.RequiredIfNotAttribute derives from Foolproof.ModelAwareValidationAttribute (which in turn derives from System.ComponentModel.DataAnnotation.ValidationAttribute). ModelAwareValidationAttribute is marked with [AttributeUsage(AttributeTargets.Property)]. Refer source code. By default the the AllowMultiple parameter of AttributeUsage is false which 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 true and allowing it to be applied multiple times would possibly cause problems in setting the $.validator.methods and $.validator.unobtrusive.adapters functions used by unobtrusive validation.

You will need to use some other validation attributes or create your own ValidationAtribute that implements IClientValidatable, or rely on server side validation.

You can implement custom validations in the model properties. See this tutorial: Creating Custom Validation Attribute in MVC to create a custom validation attribute to do the work you need, where you should write your own jquery validation script for client side validation in MVC framework mode if using IClientValidatable , which is also explained in it.

Good luck, greetings!

J. Rodríguez
  • 256
  • 1
  • 6
  • 21
  • When I was reviewing your answer, I didn't realise that you were linking to another SO answer. That is an indication that the question is a duplicate, so you really should flag the question as a duplicate, rather than try to replicate the answer. I didn't give you the downvote, btw, but linking to other SO answers will leave you open to such actions from other users. – DeanOC Apr 19 '18 at 23:57
  • @DeanOC I have not marked it as a duplicate, because with my reputation I can not vote to mark it like this, now I have edited my response and I have added additional information – J. Rodríguez Apr 20 '18 at 00:11