Need to add a custom validator which does some complex validation based on the values of other fields in the html.
Tried adding custom validator function as an attribute to the paper-input element but it wont get called at all.
<dom-module id='custom-ele'>
<paper-input is="iron-input" id='input_1' label='Label_1' validator='validate_1'></paper-input>
<paper-input is="iron-input" id='input_2' label='Label_2' validator='validate_2'></paper-input>
...
</dom-module>
<script>
Polymer({
is: 'custom-ele',
validate_1: function() {
//validation code
},
validate_2: function() {
//validation code
}
});
</script>
Ok, my answer might not be the "Polymer way" but it works and is more the "traditional programmatic" way.
The short list of ideas for this solution:
So this is a short code I derived from the points above:
You can put this code in any 'attached' or 'created' event handler. But run it before any validation is done of course...
Then you can write
If you wanna check if your validator is register with the input, navigate down the dom-tree in any dev-tool and hit:
$0.hasValidator()
and$0.validator
to see if your validator has been successfully registered with the input.The validator has to implement
IronValidatorBehavior
(see docs). Also, if you want to validate automatically, you need to set theauto-validate
attribute. To achieve your goal you could create a custom validator for each type of validation that you want to use. Alternatively, you could create a generic custom validator and set the validate function upon initialisation. Here's an example.You can find a working example in this plunk.