简体   繁体   中英

Parsley : custom validation

I'm trying to set up a custom validation in Parsley to have at least one of two fields filled up.

I started with this example on Parsley documentation : http://parsleyjs.org/doc/examples/events.html

But it seems to be an old example not working anymore.

Here is my form HTML code :

<form id="prestations-search-form" class="navbar-form" role="search" data-parsley-validate>
<label for="search-input">Keywords</label>
<div class="form-group">
    <input type="text" class="form-control" id="search-input" name="search-input" required data-parsley-group="searchText" />
</div>
<div class="form-group">
    <label for="domaines-list">Select a domain</label>
    <select id="domaines-list" class="form-control" name="domaine" required data-parsley-notblank data-parsley-group="searchDomaine">
        <option value="">All domains</option>
        <option value="1">Domain 1</option>
        <option value="2">Domain 2</option>
        ...
    </select>
</div>
<button id="submit-search" class="btn btn-block btn-primary" type="submit">Search</button>

And my JS Parsley custom validation :

this.$(this.search_form_selector).parsley().on('form:validate', function (formInstance) {
    if (formInstance.fields[0].isValid() || formInstance.fields[1].isValid()) {
      console.log("My custom validator => OK");
      formInstance.validationResult = true;
      return true;
    }
    console.log("My custom validator => KO");
    formInstance.validationResult = false;
    return false;
});

I do catch the "form:validate" event and my callback function is executed. But, even setting formInstance.validationResult = true; and returning true , I cannot validate and submit the form.

There is a lack of documentation. I don't know what to do next.

How can I validate the form and execute the form submission if my condition is fulfilled ? How can I stop the submission if not ?

Thanks for your help.

I'm sorry about your issue. Many of our examples need reworking.

You should set formInstance.validationResult = false if the validation is meant to fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM