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

Validate one input against another #68

Open
trevhud opened this issue Mar 13, 2015 · 9 comments
Open

Validate one input against another #68

trevhud opened this issue Mar 13, 2015 · 9 comments

Comments

@trevhud
Copy link

trevhud commented Mar 13, 2015

Would there possibly be a way to validate one input against another and specify it in the JSON (without an angular directive in the html)? Below I am using ng-required, which gives me the correct functionality. Only issue is I'd like to give it that logic in the JSON coming from our Java backend instead of in the html.

  <div class="col-sm-8 col-md-6 col-lg-4">
    <dl class="col-sm-6" valdr-form-group>
      <dd>
        <div ng-form="amount" class="input-group">
          <input type="text"
                 name="taxAmount"
                 class="form-control text-right"
                 ng-model="model.taxAmount"
                 ng-required="!model.percent"/>
        </div>
      </dd>
    </dl>
    <dl class="col-sm-6" valdr-form-group>
      <dd>
        <div ng-form="percent" class="input-group">
          <input type="text"
                 name="percent"
                 class="form-control text-right"
                 ng-model="model.percent"
                 ng-required="!model.taxAmount"/>
        </div>
      </dd>
    </dl>
  </div>

I'm attempting to create a custom validator that works with the following structure:

valdrProvider.addConstraints({
          "BomTax" : {
            "taxAmount" : {
              "validateBoth" : {
                "fieldnames" : ["taxAmount", "percent"],
                "message" : "bad amount"
              }
            },
            "percent" : {
              "validateBoth" : {
                "fieldnames" : ["taxAmount", "percent"],
                "message" : "bad percent"
              }
            }
          }
        });

In the custom validation service, I have access to the value, and the arguments. Do you know how I might use the strings in my JSON to refer to the other input value? Or is there a better way to achieve this? I'd really appreciate any feedback or direction. Thanks for the great library!

@philippd
Copy link
Collaborator

Right now there is no way to validate one field against another one. This is because the directive that validates one input has no access to the complete form, to read the other fields values from.
It should be possible to extend the valdrFormInput directive to pass the complete form to the validator which could make cross-field validation possible.
I'll keep this here as a feature request.

@vulkanino
Copy link

I need this too, a way to add cross-field validation.
I have validation rules that go like: Phone is 10 chars, but if the Country is USA, then Phone is 15 chars and the document combobox should be disabled!

Thanks!

@nipuna-g
Copy link

nipuna-g commented Dec 4, 2015

I too would like this feature implemented.

@philippd Could you provide some guidance in how this feature would be implemented? If it's relatively simple to implement. I'd like to try getting it implemented.

hugobessa added a commit to badoque/valdr that referenced this issue Feb 17, 2016
@hugobessa
Copy link

I've sent a PR with this feature implemented, but i couldn't think in a way of testing it automatically... I'm not very familiar with jasmine and frontend testing so I would really appreciate suggestions!

@emmanuelt88
Copy link

emmanuelt88 commented May 26, 2016

Hi guys, this is a way to implement the validation against another field, at least it was the workaround in my case:
Example for Password match:

1 ) Define the extension
mainApp.factory('passwordMatch', function () {
return {
name: 'passwordMatch', // this is the validator name that can be referenced from the constraints JSON
validate: function (value, params) {
return value == $("#"+params.matchField).val();
}
};
});

  1. Add the validator:
    valdrProvider.addValidator('passwordMatch');

  2. Use the validator:

'passwordMatch':{
'message': 'field.password.match',
'field':'current.password',
'matchField':'new_password'
}

Hope it helps.

@tyronedougherty
Copy link

Not really an optimal/scalable solution to use jQuery for this imo, has anyone thought about this further? Trying to implement a password match validation as well currently, not sure what the best way to do that with Valdr is

@hugobessa
Copy link

@tyronedougherty I'm using the fork I made of valdr in production and it's working like a charm. I made a PR to this repository but it was never accepted :(

@hugobessa
Copy link

Oh, I also added async validation in my fork. But I didn't document how to use and didn't test it a lot, so it may be bugged. The sync validation still works even with the async validation code though.

@tyronedougherty
Copy link

@hugobessa huh that is strange. I'll give it a go! 👍

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

No branches or pull requests

7 participants