Support » Plugin: Contact Form 7 - Conditional Fields » Conditionnally required field

  • Resolved Loran A. Rendel

    (@loranrendel)


    Is there any way to make a field optionally required?

    I have a checkbox and a text field, and text field should be required if checkbox is checked.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Jules Colle

    (@jules-colle)

    not as such. although, required fields in hidden groups will lose their “requiredness”. So you could wrap the group with a <div class="never-hide"> and add this CSS:

    .never-hide > div { display:block !important; }

    Although.. now I come to think of it, you will probably need to kill the javascript events as well. Give this a try first and let me know how it goes.

    Actually I need to hide the field and unrequire it and vice versa.
    Probably it could be done only by wpcf7_validate hook.

    Plugin Author Jules Colle

    (@jules-colle)

    If you want to unrequire the field while hiding it, there is nothing extra you need to do. The plugin will make sure thate hidden fields do not get validated, even if they are required.

    Oh, right, it’s already work, I set required for the checkbox by mistake.

    But there is another problem: if I fill some field and then hid it, the filled hidden field will be sent.

    Plugin Author Jules Colle

    (@jules-colle)

    Yes, this is by design.

    However, you can make sure hidden fields are hidden in the email message by wrapping them inside a the corresponding group tag in the email as well. Something like this

    Checkbox value: [checkbox-1]
    [group-1]
    Sub value: [my-field]
    this text and values will only be shown if "group-1" was visible during form submission.
    [/group-1]

    Or it could also be filtered by $_POST['_wpcf7cf_hidden_group_fields'] inside wpcf7_posted_data hook.

    Plugin Author Jules Colle

    (@jules-colle)

    That’s right. You could create your own hook to do this.
    But I decided to still post hidden fields, because some user might want to create forms where hidden fields actually need to be processed. I’m thinking about a multistep-like workflow for example.

    Thank you 🙂

    Plugin Author Jules Colle

    (@jules-colle)

    you’re welcome 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Conditionnally required field’ is closed to new replies.