netceteragroup / valdr

A model centric approach to AngularJS form validation
http://netceteragroup.github.io/valdr/
MIT License
153 stars 43 forks source link

Support validation of non input attributes. #80

Closed grivescorbett closed 9 years ago

grivescorbett commented 9 years ago

An example would be "A person must have at least 3 addresses." I'd like to be able to lay out the constraints like so:

'Person': {
  'firstName': {
    'size': {
      'min': 2,
      'max': 20,
      'message': 'First name must be between 2 and 20 characters.'
    }
  },
  'addresses': {
    'size': {
        'min': 3,
        'message': 'Must have at least three addresses.'
    }
  }
}

There is no <input> element for the addresses array but it still has constraints against it. Is this currently possible with valdr? If not, is there interested in a feature like this?

marcelstoer commented 9 years ago

Something similar was requested in issue #68 and pull request #75.

The latter would allow you to build a custom validator that has access to the entire form and which you could bind to a hidden addresses input field (with no value). Not sure though how you'd properly display constraint violations for that field...

luaks commented 9 years ago

I would use a hidden input field. Validators are only called on existing fields, therefore, there would always be the need for an input field, even when referencing the whole form in a custom validator. This is far more complex, than using an input field, which solves this by just adding a single line.

marcelstoer commented 9 years ago

I discussions like this one should never forget that client-side validation is only about convenience, UX-sugar if you will. So, I feel that even if you can only validate 80% of your constraints on the client and the rest is validated exclusively on the server you achieved a lot.

philippd commented 9 years ago

I'll close this as we have a feature request that would make this possible in #68. As mentioned in #75 we currently focus on single field validation until someone comes up with a good idea how to access all form values and make cross-field validation possible.