Malvinka
Malvinka

Reputation: 1379

parsly validator message not displayed when second validator pass

I've got a field that has two validators: one is parsley-pattern, second is custom parsley validator "optional if" . When the custom validation pass (the other field has valid value provided) but the pattern validation failed (invalid value for this input) parsley indicates that it failed by setting focus on that field after validation but shows no error message. If the custom validation failed and only one invalid value is provided the message is shown correctly. How can I fix that?

custom validator:

window.Parsley.addValidator("optionalIf", {
  validateString: function(value, requirement) {
    theOtherField = $("[name = " + requirement + "]")
    if (!!value || !!($(theOtherField)).val()) {
      $(theOtherField).parsley().reset()
      return true;
    }
    return false;
  },
  messages: {
    en: 'You need to provide the answer to either this field or %s field (or both)'
  },
  priority: 1
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.9.2/parsley.min.js" integrity="sha512-eyHL1atYNycXNXZMDndxrDhNAegH2BDWt1TmkXJPoGf1WLlNYt08CSjkqF5lnCRmdm3IrkHid8s2jOUY4NIZVQ==" crossorigin="anonymous"></script>

<input data-parsley-different-to="input2" 
data-parsley-optional-if="input2" 
data-parsley-pattern="^\d{4}-\d{3}(\d|X|x){1}$" 
data-parsley-validate-if-empty="true" id="input1" name="pissn"
pattern="^\d{4}-\d{3}(\d|X|x){1}$" placeholder="11111111" type="text"
value="" data-parsley-errors-container="#input1_checkbox-errors" 
data-parsley-group="block-1" 
data-parsley-id="18" class="" 
aria-describedby="parsley-id-18">

Upvotes: 0

Views: 37

Answers (1)

Malvinka
Malvinka

Reputation: 1379

Changing the priority from 1 to 300 made it work properly.

Upvotes: 0

Related Questions