Question

Which parsley.js functions do I need to call to custom validate with javascript?

The reason I ask this is because I defined the following Assert Array in the back end and want to reuse this in the front end: [ new Assert().Length( { min: 33, max: 25 } ), new Assert().NotBlank() ]

Thank you,

Was it helpful?

Solution

You'll find here the documentation on how to define your custom validators for Parsley.

What you are trying to do may look something like that:

<script type="text/javascript">
window.ParsleyConfig = {
  validators: {
    myvalidator: {
      fn: function (value) {
        return Validator.validate(value, [ new Assert().Length( { min: 33, max: 25 } ), new Assert().NotBlank() ]);
      },
      priority: 32
    }
  },
  i18n: {
    en: {
      myvalidator: 'Your field is invalid, or some different message'
    }
  }
};
</script>

Note: why defining these two asserts? NotBlank() is redundant with previous one, because a Blank field would obviously be wrong, its length being inferior to 33 min length. I don't really see the point to add NotBlank() here :)

Best

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top