-
Notifications
You must be signed in to change notification settings - Fork 11
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
Elements should support manual (async) validation #36
Comments
Could go hand-in-hand with support for async custom validation in simple-schema (which would just hook into this functionality). https://github.com/aldeed/meteor-simple-schema#asynchronous-custom-validation-on-the-client |
This looks complex and I cannot imagine a real life use case for this. I don't see why you would want to perform per-element async validation when you do a per-form async validation anyway in your |
Of course data should be validated on the server (or at least pass the allow/deny rules for a collection). I think the classic use-case for something like this is to do real-time checks on unique values (like the availability of a username) but I could also see it being used for website validation and real-time checks using third-party HTTP APIs. |
If, by real-time checks, you mean checking on each keypress over the Internet, I am doubtful this is reasonable in most real-life cases. If you mean checking on submit, then this is what the |
I mean checking before submit, but not necessarily on every keypress. If it was based around keypresses, it would have to be throttled or debounced. Anyway, you're right for the vast majority of cases--having an immediate remote check isn't that important and it can be handled inside the It may be useful to be able to override validation on certain types of elements, but I think I'll shelve this issue until I or someone else encounters a real-world use case where it's required. |
I've just run into a case where I'm using two elements as sub-elements inside another element. One element is used to provide a select box, and the selection from that box is used to show items in another list with checkboxes. In this case, being able to control which I've added this functionality for the next release--it's invisible unless someone wants to use it. |
I have found a use case: I want the user to input an image URL and I want to validate it client-side (i.e. checking that it actually points to an image). This validation is asynchronous. How should I move forward with this? |
You'll want to return The current implementation can be improved. For example, right now there's no |
It works, thanks! However, it is a bit hacky, because now my UI element contains a validation function, meaning it is not re-usable anymore. To somehow fix this, and in case it can help other folks, I added this function to all my elements: validationValue: function (el, clean, template) {
var self = this;
// Get the element value
var value = $(el).val();
// Support for the template argument 'customValidate'
if (template.data.customValidate) {
// 'customValidate' should be a function
if (! _.isFunction(template.data.customValidate))
throw new Error('customValidate should be a function');
// Get the schema context and reset it
var schemaContext = Template.parentData().context.schemaContext;
schemaContext.resetValidation();
// Get the field to check
var field = template.data.field;
// Put the form in the invalidated state
// self.invalidate();
// Inside the 'customValidate' callback (or asynchronously), call
// "validationDone()" to indicate that the field is ok, or
// "validationDone(errorType)" to indicate an error (errorType being a
// simple-schema error message constant).
var validationDone = function (errorType) {
if (errorType)
schemaContext.addInvalidKeys([{ name: field, type: errorType }]);
else
self.validate();
};
// Call the 'customValidate' callback
// If 'customValidate' returns a falsey value, the initial schema
// validation is not performed ('customValidate' replaces the original
// schema validation). If 'customValidate' returns a truthy value, the
// original schema validation is performed immediately, then
// 'customValidate' is called to perform additional custom validation.
if (template.data.customValidate(el, clean, template, validationDone)) {
var objectToValidate = {};
objectToValidate[field] = value;
schemaContext.validateOne(objectToValidate, field);
}
// Quit, keeping validation in standby
return this.stop;
}
// Standard behavior
return clean(value, { removeEmptyStrings: false });
} So now I can use my elements like this {{>bootstrapFormTextarea field="profile.picture" customValidate=th_validatePictureUrlFun}} th_validatePictureUrlFun: function () {
return function (el, clean, template, validationDone) {
var self = this;
Meteor.setTimeout(function async() {
var errorType= <my validation here, null if no error>
self.validationDone(errorType);
}, 3000);
return true;
};
}, Maybe you should reopen this issue for a better fix in the future. Some more thoughts : I am uncomfortable with the fact that |
Ok, I'll reopen this because clearly there is more work to be done. You've done something smart, essentially creating your own custom validation hook. This points me to the real issue, which is that custom validation shouldn't have to be done in I should probably take what you've done and work it into something more official, like an element-level validation hook that runs after I'm already working on designing a schema API to decouple the package from SimpleSchema. I'll create another issue with those ideas so we can move this discussion there. Some thoughts on
Because of the above points, it makes sense to keep https://github.com/meteortemplates/forms/blob/master/lib/module.coffee#L773 I could add |
That is great news.
Got your point.
Looks like a good idea. Not sure about the name, though. |
Imagine being able to call a Meteor method during validation--it would open up a world of possibilities for Elements.
There would have to be a local
{{validating}}
state for when it's loading.The text was updated successfully, but these errors were encountered: