vivin / regula

Regula: An annotation-based form-validation framework in Javascript
126 stars 24 forks source link

Need a way to validate a subset of elements easily #13

Closed troyji closed 13 years ago

troyji commented 13 years ago

From what I can see, the only way to validate a subset of elements is to use the group feature. Unfortunately, the group feature is way too tedious to use for validating sets of elements. In my mind groups are for when you want to validate only certain rules on an element instead of all of the defined rules on the element.

Use Case: There is a page with several forms present. Each form needs to be validated individually. You should be able to validate all elements within a single form, easily.

Possible Implementations

//pass in the id of a parent element who contains descendants that should be validated
regula.validate('myParentId');

//pass in an array of elements that should be validated
regula.validate([element1, element2]);

//pass in an array of ids of elements that should be validated
regula.validate(['element1Id', 'element2Id']);

The second implementation, which takes a list of elements, would allow for easy integration with third party selector libraries like jQuery. It is also probably the most convenient to implement in regula.

regula.validate($(mySelector).get());

The first and third implementations are convenient for users who do not use third party selector libraries. The second and third implementations allow for a set of cherry-picked elements (does not require a shared parent; does not force siblings to be included) to be validated together.

If going with the first and third implementations, a more mature signature scheme will be needed. The is because passing in a single string could mean 'validate just this element' or 'validate this element and all of its descendants, or 'validate this elements descendants but not this element itself'. Similarly, the array of element ids would be ambiguous.

Side Note: I suspect that one goal of this library may be independence from external libraries. However, adopting jQuery would make these tasks a breeze, and would allow for a very fluid interface (it would also allow for a much more simplified codebase).

$(mySelector).validate();
vivin commented 13 years ago

I've been trying to think of a good way to do this as well. I inevitably end up going the CSS selector route, which would mean a dependency on jQuery; I would like to avoid that if possible. The other option is to reinvent the wheel or introduce a dependency on sizzle.js (that might not be so bad). That being said, I saw an example of regula as a jQuery plugin. I like the second implementation better, and as you said, this would probably be the easiest to implement.

troyji commented 13 years ago

I hacked in support for this in my 'validate_each' branch. Check out the implementation here:

Branch: https://github.com/troyji/regula/tree/validate_each Code Changes: https://github.com/troyji/regula/commit/c51db0d499b1d34a1444ea0dba3a44c6e834841e

I am not sure if the performance will be satisfactory. It depends on your implementation of the underlying routines. Take a look and see if you think it is a viable implementation. At the least, I think that this syntax will do.

//validate all as before
regula.validate();

//validate based on options as before
regula.validate(options);

//validate each element as if we used regula.validate({elementId: '...'}) for each element
regula.validate(elements);

//validate each element like the previous example, except use the additional options with each call
regula.validate(elements, options);

Example of use with jQuery:

var elements = $(".regulaSubset").get();
regula.validate(elements);

It would be trivial to create a jquery plugin from here to map the above regula calls to these:

//no selector without options
$.validate();

//no selector with options
$.validate(options);

//selector without options
$(selector).validate();

//selector with options
$(selector).validate(options);

If you find this implementation to be satisfactory, I can push it to my master and submit a pull request.

vivin commented 13 years ago

Sorry I haven't had a chance to look at it. A little busy at work. I gave it a quick glance and it looks pretty close to what I would have done. You can go ahead and submit a pull. If I make any changes, I will let you know. Thanks for doing this!

Sent via Hubroid