1
votes

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.

1
Looks like the example code in your link returns after validating the two fields, therefore it never hits the formInstance.submitEvent.preventDefault(); line. What are you trying to do that's different? Also, if you supply your HTML, that would help. - Marc

1 Answers

0
votes

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.