whatgoodisaroad / validity

Client-Side Validation for jQuery
http://whatgoodisaroad.github.io/validity/
132 stars 74 forks source link

Assert doesn't work properly on multiple calls (non-form, non-aggregate) #45

Closed davegot13 closed 10 years ago

davegot13 commented 10 years ago

I am using validity for a non-form and ran into this issue when trying to use assert. Here is a snippit of the code I'm trying to use (I know there's a greaterThan operator, but I need to do some custom formatting first):

$.validity.start(); $priceField.assert(priceGreaterCheck, "Price must be greater than 0"); var result = $.validity.end();

function priceGreaterCheck(element) {
var rv = $priceField.asNumber() > 0; return rv; };

The assert call raises the error the first time I try to validate the field. However, if I try to validate the field again, the assert statement returns true even if I don't change the value. The reason I think this is happening is because after the first run through, the validate method is doing

$obj.reduction = $(elements);

which is returning a list of passed elements. However, if nothing passes, then $obj.reduction = [](but not undefined). Therefore in the assert method the line:

var $reduction = this.reduction || this;

is returning [] instead of the object contained in this. This causes

if ($reduction.length)

to evaluate to false and not try the validation which is not the desired behavior. I don't want to modify the validity file so as a workaround I'm doing:

$priceField.reduction = undefined; $priceField.assert(priceGreaterCheck, "Price must be greater than 0");

to force this.reduction || this to always evaluate to this. This is obviously not a permanent solution and should be handled somewhere inside of validity.

davegot13 commented 10 years ago

Wanted to provide an update. I did some more work and this applies more broadly. If you assign a selector to a jquery variable like:

$myVar = $("#someInput");

if you do $myVar.require(), it works the first time. However, if you call it again, the code doesn't raise an error. This all points back to the validate method where the method sets $obj.reductions to [] in the case nothings passes. When $obj.reductions = [] then that should be treated in the code as undefined (maybe even just set $obj.reductions to undefiend if nothing passes).

whatgoodisaroad commented 10 years ago

I see the problem you're describing. This appears to be a design flaw in validity. Expect a fix soon.

ajwaka commented 10 years ago

I too am now running into this issue - Hope to see the fix soon.

Thanks for this plugin - It's great since I don't have to apply the validation to a form - I can simply validate anything I need on any action. Really appreciated!

whatgoodisaroad commented 10 years ago

Hey guys. I've pushed a fix with a version bump to 1.3.7. My tests show the problem gone, but I'd appreciate it if you guys had a look.

Thanks a lot!

davegot13 commented 10 years ago

Looks like the fix works. I cannot reproduce the problem anymore

whatgoodisaroad commented 10 years ago

Excellent. Thanks for the report!