Parsley JS - Custom error message %s format

2019-08-31 09:55发布

问题:

I'm trying to validate a YYYY-MM-DD date with the following code (parsley plugin), but i wish to show an error message with the %s value in DD/MM/YYYY format. Is there a way to do that? Thx!

<div class='input-group date' id='datetimepicker'>
  <input type='text' name="contact-date" id="contact-date" data-parsley-mindate="2000-01-01" />
</div>



<script>
     window.ParsleyValidator
        .addValidator('mindate', function (value, requirement) {
            // is valid date?
            var timestamp = Date.parse(value),
                minTs = Date.parse(requirement);

            return isNaN(timestamp) ? false : timestamp > minTs;    
        }, 32)
        .addMessage('en', 'mindate', 'This date should be greater than %s');

    $('#myForm').parsley();

    $('#datetimepicker').datetimepicker({
        language:'en'
    });
    </script>

回答1:

You could return a "dynamic" error message is by returning a failed promise from your validateString method. This example uses this technique.



回答2:

Thank you!! I added the following code when the validation fails and it works (it allows me to access the "%s" value to customize the message)

return $.Deferred().reject("custom message");


标签: parsley.js