Song King
Song King

Reputation: 3

Is there a way to do partial parsley.js validations?

I am creating a page with a long form for which I'd like to have two action buttons: 'Save' and 'Submit'. I would like the 'Save' to be nearly identical to 'Submit' except that it would only run a subset of the built-in Parsley validations, namely 'required' and 'pattern'. The Parsley documentation doesn't seem to address this. Does parsley allow this, or is there an elegant work-around? My only idea thus far is to run all validations on the page, use CSS to hide irrelevant error messages, and consider the form 'validated' as long as there are no error messages regarding the subset of validations I'm testing for for 'Save'.

Upvotes: 0

Views: 1065

Answers (2)

Heitor Althmann
Heitor Althmann

Reputation: 367

Consider using the data-parsley-group attribute on your form controls to create validation groups.

Check this example:

$('document').ready(function() {
  $('input[type=submit]').click(function(event) {
    if ($('.demo-form').parsley().validate({
        group: 'block1',
        force: true
      })) {
      alert('Valid group!');
    }
  })
});
<!DOCTYPE html>
<html>

<head>
  <title></title>
</head>

<body>
  <h4>Correctly fill at least one of these blocks</h4>
  <form class="demo-form" data-parsley-validate="">
    <div class="first">
      <label for="firstname">Firstname:</label>
      <input type="text" name="firstname" data-parsley-group="block1" required="required">

      <label for="lastname">Lastname:</label>
      <input type="text" name="lastname" data-parsley-group="block1" required="required">
    </div>
    <hr>
    <div class="second">
      <label for="fullname">Fullname:</label>
      <input type="text" name="fullname" required="required">
    </div>

    <div class="invalid-form-error-message"></div>
    <input type="submit" class="btn btn-default validate">
  </form>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="http://parsleyjs.org/dist/parsley.min.js"></script>
</body>

</html>

Check here for further reference:

http://parsleyjs.org/doc/examples/events.html

Hope it helps! :)

Upvotes: 1

Marc-Andr&#233; Lafortune
Marc-Andr&#233; Lafortune

Reputation: 79552

There is no way to only run a subset of the validation types with Parsley.

You should consider removing and adding the data-attributes for the other validations you want before and after the save?

Upvotes: 0

Related Questions