Hi @j0111
I checked the last code shared in the previous thread and it seems there was either a slight mistake or misunderstanding on our end which lead to code glitch. So let me make sure: the error defined in this code should show up in any of following cases:
– if both email and phone fields are not provided
– if one of them (either email or phone) is not provided
In other words, it should act as if both fields are required, right?
If so, please edit the code and make small change in it. In this line (I’m assuming you used email-1 and phone-1 fields in it):
if ( empty( $submitted_data['email-1'] ) && empty( $submitted_data['phone-1'] ) ) {
replace the
&&
with
||
so the line would be
if ( empty( $submitted_data['email-1'] ) || empty( $submitted_data['phone-1'] ) ) {
The difference here is the logical operator. In the code originally there was AND operator used, meaning that both these fields would have to be empty to trigger the error and if just one of them would have something entered – error would not show up.
The change makes it an OR operator, so if both or one of these fields (any of them) will be empty, error will show up.
Additionally, in this case I’d also suggest setting both these fields to “required” directly in the form (this doesn’t affect he code but will let you display additional validation message right below the field; will mark the field as required and will provide additional submission prevention).
Also just in case, double-check if form ID is correct in both
if ( $form_id != 2871 ) {
these lines (there are two of them in code, both have to be set to the same form ID!).
With above changes applied to the code (note: I also changed error message in the code on my end) and form, I tested the it on my test setup and it seems to be working just fine.
Both fields empty:
https://app.screencast.com/ZKDnB4WdX5EHy
Phone field empty only:
https://app.screencast.com/xPIZrwdl9Xx0k
Email field empty only:
https://app.screencast.com/Re4J976azT2c5
Best regards,
Adam