简体   繁体   中英

Parsley form not validating when input fields are disabled

I have a button that enables or disables input fields in a form. The form can be submitted whether the fields are enabled or disabled. Parsley is validating correctly when fields are enabled but it's not validating when fields are disabled.

<form class="user-form">
  <div class="form-group row mb-3 d-flex align-items-center">
    <label class="control-label col-md-3 text-end">First Name</label>
    <div class="col-md-9">
      <input
        type="text"
        class="form-control"
        id="firstName"
        name="firstName"
        data-parsley-required="true"
        disabled
      />
    </div>
  </div>
  <div class="form-group row mb-3 d-flex align-items-center">
    <label class="control-label col-md-3 text-end">Last Name</label>
    <div class="col-md-9">
      <input
        type="text"
        class="form-control"
        id="lastName"
        name="lastName"
        data-parsley-required="true"
        disabled
      />
    </div>
  </div>
</form>
<button type="button">Submit</button>

I have a validate function that takes in a form element and validates like this. $form.parsley().whenValidate() where I pass in the form element. I get correct behavior when inputs are enabled but no validation errors when inputs are disabled. I also found out that if I validate the individual input elements seperatly(firstName, lastName) then I get the correct behavior even when they are disabled.

I can manually enable all fields again and validate once someone submits but is there a better solution? I am using parsely version 2.9.1

This behaviour is by design.

Inputs that are disabled are not sent on form submission, and thus do not require validation. Also, the fact that they are disabled prevents the user from modifying them, so if it's invalid the user can't do anything about it.

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