Skip Navigation

[Resolved] Cannot create required field

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 22 replies, has 2 voices.

Last updated by Shane 5 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#1281963

I am trying to:
I created some "Add Custom Post Forms" with these options >> Are you interested to: 1) BUY / 2) SELL. I need the customer to be forced to click on one of them (buy or sell, not both - just one) and I don't know how to make the selection required.

From your support forum I tried this function:

function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;

//uncomment this if you want to print the field values
//print_r($fields);

//validate if specific form
if ($form_data['id']==25559)
{

//check if field is selected
if (empty($fields['wpcf-type_of_proposal']['value']))
{
//set error message for featured image
$errors['wpcf-type_of_proposal'] = 'Please Specify if you are looking to Buy or Sell';
}
}

//return result
return array($fields,$errors);
}

This works only with one form. When I try to create a similar function for other forms (with other ID's) it does not work.

Link to a page with the form:
hidden link

Please advice.
Thank you!

#1282091

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Valentine,

Thank you for contacting our support forum.
Could you let me know which field on this form here below you are trying to make required ?
hidden link

Thanks,
Shane

#1282745
req field.png

I need the user to check only one of those two fields (image attached), I need the selection to be required.
For you to see that page you must create a free account.

#1283227

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

snippet.PNG

Hi Valentine,

I'm actually not seeing the exact same thing as you have in your screenshot.

Perhaps I need to be logged in?

If so i've enabled the private fields for your next response.

Thanks,
Shane

#1283243

Yes , you need to create a free account (from that page that you see) and then you need to login.

#1283251

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Valentine,

Based on what I see here your form ID is incorrect in the Hook.

It should be 25621

Thanks,
Shane

#1283261

That is what I'm telling you. I created that function and it only works with one form (the form with the ID 25621). How can I make that function to work on multiple forms?

#1283269

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Valentine,

Thank you for the clarity. This is actually quite a simple fix.

Change this here

//validate if specific form
if ($form_data['id']==25559)
{

To


$form_ids = array(1,2,3,4,5)

//validate if specific form
if (in_array($form_data['ids'],$form_ids))
{

Now you just need to put your ids in the id array separated by commas.

Please let me know if this helps.
Thanks,
Shane

#1283293

It still does not work.

#1283341

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Valentine,

Could you paste to me exactly what you have now ?

Try this


$form_ids = array(1,2,3,4,5)
 
//validate if specific form
if (in_array($form_data['id'],$form_ids))
{

I made a simple error in the ID call for the form.

Thanks,
Shane

#1283739

When I try to save the functions file I get this message:
"
Changes you made to the PHP code have been canceled due to an error on line 227 of the wp-content / themes / kleo-child / functions.php file. Please correct and try saving again.

syntax error, unexpected 'if' (T_IF)

"

This is what I have now:

function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;

//uncomment this if you want to print the field values
//print_r($fields);

$form_ids = array(25559,25621,25640)

//validate if specific form
if (in_array($form_data['id'],$form_ids))
{

//check if field is selected
if (empty($fields['wpcf-type_of_proposal']['value']))
{
//set error message for featured image
$errors['wpcf-type_of_proposal'] = 'Please Specify if you are looking to Buy or Sell';
}
}

//return result
return array($fields,$errors);
}

#1284109

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Valentine,

There was a missing semi-colon

$form_ids = array(25559,25621,25640);

That should work now

#1284149

It still does not work. 🙁

#1284287

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Valentin,

That is odd as it should now work.

Would you mind allowing me to have admin access to the site so I can see what is happening here?

Thanks,
Shane

#1286421

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Valentine,

I believe i know what the issue is.

The field you are trying to validate, it's a custom taxonomy and not a custom field.

We actually don't have a way to set the taxonomy as required through our api. What you can do is to use JS to set the field as required.

Take a look at this here.
https://stackoverflow.com/questions/849155/jquery-require-checkbox-and-radio-button-before-submit

This should be able to help you with this issue.

Thanks,
Shane