valdr
valdr copied to clipboard
Validate one input against another
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!
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.
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!
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.
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!
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(); } }; });
-
Add the validator: valdrProvider.addValidator('passwordMatch');
-
Use the validator:
'passwordMatch':{ 'message': 'field.password.match', 'field':'current.password', 'matchField':'new_password' }
Hope it helps.
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
@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 :(
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.
@hugobessa huh that is strange. I'll give it a go! 👍