Required fields with condition?
-
Hi folks! FANTASTIC plugin! Thank you!
I have two fields in a form. Is it possible to configure the form such that at least one of those fields MUST be filled out by the user for the form to be allowed to be submitted? In other words, not necessarily both fields but at least one?
We look forward to your wisdom ??- This topic was modified 1 year, 8 months ago by korg007.
-
I hope you’re well today!
I know what you mean but currently such conditional logic isn’t supported yet.
But there is a workaround that you could possibly use and that would be using visibility conditions for “Submit” button. Below example assumes that fields in question are just regular input fields and the only requirements is that they aren’t empty:
1. you would need to set both of those fields in question as NOT required first and make sure they have no default value set (so by default they are empty)
2. then edit “Submit” button and set visibility logic to “Hide this field if Any of the following rules match
3. and add two rules:
– one for the first of those input fields with “is null” condition (so you just leave “value” field empty)
– and one for the second of those input fields, set the same way.Result should be that those fields are not required but until at least one of them is filled-in, the submit button is not available so submission is not possible.
Best regards,
AdamThanks so much for responding, Adam. Much appreciated.
I was afraid you’d suggest this lol. I can understand the challenges of implementing this. The issue we have is the site will be used by seniors and hiding/revealing the Submit button will undoubtedly confuse many visitors.
We may have to leave the fields as non-required and hope for the best. We use those fields as the recipient(s)’s email. I’ll have to test what happens if Forminator attempts to send an email to a blank email address as this will be the case if both email fields are left blank.
I’m open to other solutions should you have some in your magic hat.
I hope you are doing well.
We may be able to use a hook for that and try to adapt a similar code:
https://gist.github.com/patrickfreitasdev/ec5d3d44f504e42f92891346520d3bcfBut to understand better your form, can you export it https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export and share using Google drive?
Best Regards
Patrick FreitasThanks for your willingness to help, Patrick.
Here’s the link to the file. PS. it includes extra “readme” text ??
https://drive.google.com/file/d/1b7CsfMUceJ6vWTuaOjuZh2LKDBfAlF0V/view?usp=share_link
Let me know ASAP after you’ve copied the file so I can remove it from my google drive.
I look forward to hearing form you.
Thanks for response but, unfortunately, we can’t access this export file.
Would you please go to your Google Drive and edit settings of the share link? Change it, please, to “everyone with the link can view” option and let us know once it’s done.
Note: if you have exported the form itself (as requested) it does not contain any submission data – only the form settings – so it’s fine to share it like this.
I’d also like to add up to my previous response and suggested solution with hiding submit button:
You wrote: “The issue we have is the site will be used by seniors and hiding/revealing the Submit button will undoubtedly confuse many visitors.”.
I can surely understand it but there is also yet an additional step that could be done to mitigate that “risk of confusion”. In addition to conditional logic configuration that I suggested you could simply add one more field to the form:
– a HTML type field
– place it as a last field of the form so it would be right above the “submit” button
– put some clearly stated and formatted message in it, similar to “You did not select any option. Please select option to be able to submit the form”; that’s probably not the best wording but I’m sure you know what I mean ??
– and finally – set conditional logic for that field to be exactly opposite as for submit button – so it would show when submit button is hidden and hide when submit button is visible (note: conditions should be set based on the same fields as for submit button – not based on submit button itself)I think this would help avoid any confusion – with a nicely and clearly stated message, users should understand that they must choose one of those options, I believe.
Best regards,
AdamThanks Adam. Sorry about that. You should now have access to view the file.
Thanks for the additional workaround for the dilemma. It’s still a bit clunky but certainly is a valid option. I’m not sure my client will find it sufficiently appealing though.
Looking forward to see if you can come up with a seamless solution :).
Thanks so much for your willingness to help.
Hi @gillesgagnon,
I have pinged our developers to check if an alternate workaround could be suggested. We’ll update you here once we have more feedback regarding this as soon as possible.
Kind Regards,
Nebu JohnHi @gillesgagnon,
Please try adding the following code snippet using a mu-plugin.
<?php add_filter( 'forminator_custom_form_submit_errors', 'forminator_mail_submissions_test', 10, 3 ); function forminator_mail_submissions_test( $submit_errors, $form_id, $field_data_array ) { if ( $form_id != 4871 ) { return $submit_errors; } $GLOBALS['email_error'] = ''; $submitted_data = wp_list_pluck( $field_data_array, 'value', 'name' ); if ( empty( $submitted_data['email-2'] ) && empty( $submitted_data['email-3'] ) ) { $submit_errors[]['submit'] = __( 'Either of the email field should be filled.', 'forminator' ); $GLOBALS['email_error'] = __( 'Either of the email field should be filled.', 'forminator' ); } return $submit_errors; } add_filter( 'forminator_custom_form_invalid_form_message', 'wpmudev_invalid_form_error', 10, 2 ); function wpmudev_invalid_form_error( $invalid_form_message, $form_id ){ if ( $form_id != 4871 ) { return $invalid_form_message; } if ( ! empty( $GLOBALS['email_error'] ) ) { $invalid_form_message = $GLOBALS['email_error']; } return $invalid_form_message; }
The above code will add validation for the email field with the names email-2 and email-3. In the above code, 4871 should be changed to your form’s ID and you can change the validation error by replacing “Either of the email field should be filled”.
Please find how to add a mu-plugin here: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
I hope that helps, please feel free to get back to us if you need any further clarification.
Kind Regards,
Nebu JohnHello @gillesgagnon ,
We haven’t heard from you for 4 days now, so it looks like there are no more questions for us.
?
Feel free to re-open this ticket if needed.Kind regards
KasiaHey! I’m reopening the ticket, I was trying to reimplement the above mu plugin solution to my situation as well. But when I try to do a test submission, I’m not seeing the error message float up and the validation fails to fail and continues to submit.
The only portions I’ve updated are the “form_id” and form fields which are “email-1” and “phone-1”. Would appreciate the help!
Hello @j0111,
The above snippet doesn’t have any additional validation, and you will have to create additional HTML fields with visibility conditions applied to them, to serve as error messages.
The mu-plugin should not allow you to submit the form with both of the fields empty.
If that happens with your form, or there’s any other issue (with configuring the visibility conditions, etc.), could you please create a new forum thread, and share the exported form. Please also reference this thread in the new one.
Best Regards,
Dmytro
- The topic ‘Required fields with condition?’ is closed to new replies.