• Resolved pcgardner

    (@pcgardner)


    In my application I have an optional checkbox that, when checked, causes a text field to become visible (using jQuery). The text field ought then to be required, because it asks for information that’s needed if the checkbox is checked. However, I can’t specify it as required in the field settings because it will then cause an error on submission when the checkbox is unchecked. The text field needs to be changed to being required on the fly, if the checkbox is checked when the form is submitted.

    Is this possible? If so, how can it be done? Thanks for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author xnau webdesign

    (@xnau)

    A very simple way to do this is to use your jQuery to set the “required” attribute of the input.

    Take a look at this article for the details:

    Client-side form validation

    Thread Starter pcgardner

    (@pcgardner)

    Thank you – that’s great! Should I set the attribute or the property of the input?

    Thread Starter pcgardner

    (@pcgardner)

    It works (using attr), but on submitting the form, instead of the usual error message and highlighting of the field a little popup appears pointing to the field that says “Please fill out this field”. If more than one field has been set to required, and all are left blank, the popup only appears on the first one. Having corrected it, when I submit the form again the popup appears on the next empty field.

    I can live with this, but it’s rather confusing to the user to have two different systems of highlighting errors. Is there any way of avoiding that?

    Plugin Author xnau webdesign

    (@xnau)

    Yes, it’s not ideal, but it’s an easy way to go. If you do more reading on using the client-side validation, you can find ways to improve the UI.

    If you want to validate on the server side, you can set up a conditional validation filter on the backend with some custom code. To do that, the filter to use is pdb-before_validate_field.

    Thread Starter pcgardner

    (@pcgardner)

    Thank you for those useful tips. I hadn’t seen the article on client-side validation before – there is so much to learn about PDB!

    • This reply was modified 3 years, 6 months ago by pcgardner. Reason: added link
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Change ‘field required’ property on the fly?’ is closed to new replies.