vivin / regula

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

label & groups should have global definitions per element #11

Open troyji opened 13 years ago

troyji commented 13 years ago

Every constraint allows for a label and groups. In most cases, all constraints for an element need to have the same value for these parameters. I propose that there should be a way to define these values globally for all other constraints on the element. This could be achieved with either a special constraint, or through another attribute.

Additionally, there are some scenarios where you would want the label and/or groups to be different per constraint. Therefore, the constraint parameters for them should remain. If they are present, they would override the globally defined label or group. This would also allow for backwards compatibility.

Ex.

<input type="text" class="regula-validation" 
  data-constrains="@Label(value='My Label') @Groups(value='MyGroup') @Required @IsNumeric @Min(value=0)"/> 

Or

<input type="text" class="regula-validation" label="My Label" groups="MyGroup" 
  data-constrains="@Required @IsNumeric @Min(value=0)"/> 
vivin commented 13 years ago

That's an interesting idea. I think it would require some more thought as to the best way to implement this. Using predefined groups for all constraints attached to an element is certainly attractive.

troyji commented 13 years ago

I am thinking that we should just add a new html attribute for adding element-wide variables to an element. Perhaps custom constraints could have access to this feature too. You would still be able to override these on the constraint level if needed as well.

<input type="text" 
    data-variables="%label('My Label') %groups('grpA') %myCustVar('someVal')" 
    data-constraints="@Required @Number @MyCustConst"/>
vivin commented 13 years ago

That certainly seems possible; would still need to think a bit more about this one.