validate.js icon indicating copy to clipboard operation
validate.js copied to clipboard

manually validate a single field

Open BinaryGeometry opened this issue 8 years ago • 2 comments

This commit resolves issue #197 might be helpful as a starting point for issue #126

Adds a public valididateOne method to the validateForm object which allows you to validate a single field manually.

/* Usage /
var valid, field; field = $('[name="name"]).attr('name"); valid = validator.validateOne(field) if(!valid === true){ /
valid === [{name, "name", message="Current error" messages="[Current error, Other errors]"}]

BinaryGeometry avatar Jan 29 '18 07:01 BinaryGeometry

Hi Rick, I added a validationOne method to your code which reuses much of the validateField function to allow a single field to be checked for errors at any time. I didn't yet test it with the depends function, but essentially it doesn't change the existing logic so I'm hoping it might stand up as a useful contribution.

BinaryGeometry avatar Jan 29 '18 07:01 BinaryGeometry

I just found this PR randomly, but it seems to fit my needs. One suggestion, change what the function returns. It returns true if the input is valid and an array if not. This prevents me from doing something like:

isValid = @validator.validateOne(name)
if isValid
  clearError
else
  displayError

If the field is not valid, it will return an array and hence the else clause is never reached.

Sure you could do something like

errors = @validator.validateOne(name)
if errors == true
  clearError
else
  displayError

However, that feels awkward to write.

One idea is to run a callback, similar to what _validateForm does.

dsandstrom avatar Feb 08 '18 04:02 dsandstrom