netceteragroup / valdr

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

Allow validation of one input against another #98

Open hugobessa opened 8 years ago

hugobessa commented 8 years ago

Changes: feat, docs Scope: valdrFormItem-directive, valdr-service Subject: Implementing cross model validation

Description

Implements the possibility of creating custom validation using other models in the form as parameters.

The user will define the required models in a requireModels contraint in the custom validator, which receives a list of field names. It will allow him to receive a third parameter in the validate function of its custom validator with the model values required in the requreModels constraint.

eg.:

yourApp.factory('customValidator', function () {
  return {
    name: 'customValidator', // this is the validator name that can be referenced from the constraints JSON
    validate: function (value, arguments, requiredModels) {
      // value: the value to validate
      // arguments: the validator arguments
      // requireModels: an object which has the required models values
      return value === requiredModels.password;
    }
  };
});

yourApp.config(function (valdrProvider) {
  valdrProvider.addConstraints({
    'Person': {
      'password': {
        'minLength': 8,
        'message': 'Password must have at least 8 characters.'
      },
      'password_confirm': {
        'customValidator': {
          'requireModels': ['password'],
          'message': 'Password onfirmation must be equals to Password'
        }
      },
    }
  });
});

It uses the parent form to find the model values and force revalidation of dependent fields after a dependency model changes, and passes the dependency model value as parameters in the custom validator.

Changes per component:

hugobessa commented 8 years ago

I tested it with examples, but didn't create automated tests yet. I would appreciate if anybody could help me with that, I'm not used to frontend testing.

andreneto commented 8 years ago

+1

mikesir87 commented 8 years ago

+1

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-4.4%) to 95.622% when pulling 0f7a0ae2a2aef40ccc558eb6223392348fbf2c1a on badoque:master into ec235ce5c36306ec0d3f82436808a12ca52c69df on netceteragroup:master.

hugobessa commented 8 years ago

As I said, I only tested manually, but not atomatically because I was not familiar with jasmine. I'm implementing the feat #57 in my fork, then I'll try to make the tests for both features now.

tyronedougherty commented 8 years ago

+1, would love if this could be merged 😄

criles25 commented 7 years ago

Hey, is there any update for this?

Is the holdup the missing jasmine tests?