Skip Navigation

[Resolved] How to dispaly a not required field in the both forms and show required and not

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to dispaly a not required field in the both forms and show required and not

Solution:
you can use Toolset forms hook cred "cred_form_validate" to validate the form.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/how-to-dispaly-a-not-required-field-in-the-both-forms-and-show-required-and-not/#post-918242

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 6 years, 6 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#918209

Hi

I set a non-required field in the backend.

But I want to use this field for both forms in the front end.

One form is not required, and the other form is required.

How to set it up?

Thank you

#918242

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - you can use Toolset forms hook cred cred_form_validate to validate the form.

For example:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==99999 )
    {
        //check my_field value
        if ($fields['wpcf-my_field']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['wpcf-my_field']='Wrong Value';
        }
        //check if featured image exists
        if (empty($fields['_featured_image']['value']))
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Missing featured image';
        }
    }
    //return result
    return array($fields,$errors);
}

Where:
- Replace 99999 with your original form ID.
- Adjust fields you want to validate within the if condition

More info:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate