Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manually validate a single field #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

BinaryGeometry
Copy link

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]"}]

This commit resolves issue rickharrison#197 might be helpful as a starting point for issue rickharrison#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
Copy link
Author

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.

@dsandstrom
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants