Support nullable

Jul 21, 2010 at 6:04 PM

Many of the validation attributes don't seem to support ignoring validation if something is null (since required would be used for that).  Specifically LessThan and GreaterThan.  Would it be possible to change the design to ignore the validation if the value is null, and if the user requires a value they can use a RequiredAttribute or something?

I would be willing to make the changes, I think it would be a good addition to the framework.

Coordinator
Jul 21, 2010 at 6:57 PM
Edited Jul 21, 2010 at 6:57 PM
So in the case of: [GreaterThan("StartDate")] public DateTime? EndDate { get; set; } If EndDate was null, you are suggesting that we skip the GreaterThan validation all together - correct? I can see where that would be useful - and flexible since you could pair it with [Required] if you needed the property to be required. What are your thoughts on the dependent property being null? In this example, StartDate. What if EndDate was not null, and Start Date was null?
Jul 21, 2010 at 7:38 PM

Yeah, I think thats actually the case with some of the .NET built in validators, if the value is null they just report valid.

The specific scenario where I encountered this I wanted it off if either were null, but I'm not sure that would always be the case or not, hard to say.   My scenario was a StartDate / EndDate scenario as well, but null basically meant unknown start date or never ending, so needed the attributes to not report invalid there.

Maybe this could be an option in the attribute or something to not break compatibility.

Coordinator
Aug 11, 2010 at 6:50 PM

just an update - I plan to look at doing this soon. I've been in a whirlwind of conferences lately. 

Aug 11, 2010 at 8:26 PM

Sounds good.  If you need any help let me know.

Nov 25, 2011 at 8:31 PM

Do you have any update on this? I'm stuck with specific validators instead of the ones on Foolproof due this issue.

I think is important to maintain consistency with the rest of the DataAnnotation in which every validator performs the minimum validation specified and every exception is handled as "return true" for it to be handled for another specific validator (like the validator "Required" in this case).

Coordinator
Nov 29, 2011 at 3:16 PM

Check out the latest change set I just checked in: 83441

Mind the comment on the check in. Let me know how your testing goes.

Example: 

[GreaterThan("Value1", PassOnNull = true)]
Dec 2, 2011 at 9:00 PM

Nick, I've tested the new version and it still doesn't work for my case. But the good news is that it can be easily fixed.

The problem is that I need that when one of the properties (value or depedentValue) is null, or when both properties are null these validators let it pass. And I will explain why IMHO this is not only correct from the point of view of practicity (because with my proposed implementation we can handle the nulls in another validator and let alone this one which just handle well known use cases), but also from logical the point of view:

Null usually means "undefined" or "not applicable", and should not be confused with "0" or some other defined/well-know comparable value. And so undefined is not equal to undefined, we can not compare undefined because we don't know what it is. I believe that data annotations validators are made to mark user failures that are totally expected, and not unexpected cases which are not really clear. That is why the RangeAttribute will not return false if the user leave the field black. If you want to filter nulls you don't touch the range validator, you just add the RequiredAttribute too.

These are the lines of code which are affected (they return true when only one of the values are null, but the validation continues (and fails) when both are):

C#: if (PassOnNull && (value == null || dependentValue == null) && (value != null || dependentValue != null))
JS: if ((value1nullish && !value2nullish) || (value2nullish && !value1nullish))

What I've done to fix my problems was just changing those lines to:

 

C#: if (PassOnNull && (value == null || dependentValue == null))
JS: if (value1nullish || value2nullish)

What do you think?

Feb 10, 2012 at 3:36 PM

proton, I agree with your assessment here and have modified my downloaded version to match.  Thanks!

Mar 2, 2012 at 9:56 PM

Hello chaps,

I need this "PassOnNull" parameter, however am unsure how to apply the updated version to my Visual Studio 2010 MVC project.

Currently I have a reference to the DLL "MVC Foolproof Validation.dll" (beta version 4048) plus script reference to "mvcfoolproof.unobtrusive.min.js" in the master page.  I also updated my "mvcfoolproof.unobtrusive.min.js" script per change set 83441, but the model still doesn't recognize the "PassOnNull" parameter.

Do I need a new "MVC Foolproof Validation.dll" to reference?  Or am I missing something here on how to update the code?

Many thanks on a wonderful library.

Adam

Coordinator
Mar 5, 2012 at 12:51 PM
I'm not a positive a newer version will help you, but it might. Build the latest and see if it helps. Or post an example of it not working for me to look at.

On Fri, Mar 2, 2012 at 4:56 PM, Welsh_Dragon <notifications@codeplex.com> wrote:

From: Welsh_Dragon

Hello chaps,

I need this "PassOnNull" parameter, however am unsure how to apply the updated version to my Visual Studio 2010 MVC project.

Currently I have a reference to the DLL "MVC Foolproof Validation.dll" (beta version 4048) plus script reference to "mvcfoolproof.unobtrusive.min.js" in the master page. I also updated my "mvcfoolproof.unobtrusive.min.js" script per change set 83441, but the model still doesn't recognize the "PassOnNull" parameter.

Do I need a new "MVC Foolproof Validation.dll" to reference? Or am I missing something here on how to update the code?

Many thanks on a wonderful library.

Adam

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Nick Riggs
Mar 5, 2012 at 4:42 PM
Edited Mar 5, 2012 at 4:43 PM

Thanks for the reply Nick.  I am unsure which "new version" I am supposed to build (beyond current 4048 beta).  Additionaly, not sure how to build it and create a new DLL.

For now I have created a custom "IsDateAfter" validator for use in jQuery unobtrusive.  I am however using the Foolproof "RequiredIfNotEmpty" validator.  I look forward to a new beta release of Foolproof with "PassOnNull" included.

Adam

Sep 16, 2013 at 4:19 AM
I grabbed the latest source (94877) and applied @protron's changes and it worked a treat.
Apr 2 at 8:57 AM
@protron's fix should be pushed into the main code branch. I remember thinking this last year when searching for this issue. DateTime2 is not "not" greater than or equal to DateTime1 if DateTime1 doesn't exist yet. The validation attribute seems to be trying to do the work of [Required] too.