Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Allow validation of one input against another #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

hugobessa
Copy link

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:

  • Require the FormController in valdrFormItem directive and passed the fields to the valdr service;
  • In the valdr service it checks the dependent models of each model to revalidate them after the dependency model changed and had its validation called;

@hugobessa
Copy link
Author

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
Copy link

+1

1 similar comment
@mikesir87
Copy link

+1

@coveralls
Copy link

Coverage Status

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

@hugobessa
Copy link
Author

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
Copy link

+1, would love if this could be merged 😄

@criles25
Copy link

criles25 commented Sep 7, 2017

Hey, is there any update for this?

Is the holdup the missing jasmine tests?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants