Skip Navigation

[Resolved] Make field required in Forms

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

Problem:

The issue here is that the user wanted to make his featured image field required.

Solution:

To do this you will need to use the Toolset Forms validation hook to make the field required.

Add the following to your functions.php file

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']==12)
    {
 
        //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);
}

Change the ID from 12 to the id of your form.

This support ticket is created 6 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.

Our next available supporter will start replying to tickets in about 0.53 hours from now. Thank you for your understanding.

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)

This topic contains 2 replies, has 2 voices.

Last updated by Eso 6 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#907472

Eso

Hi friends,

I have added (required="true") in the form div but still the Featured image field is NOT set as required.

What am I doing wrong ? Please help ! 🙂

Thanks.

My Code which is not setting the div as required:

<div class="form-group">
<label>Söyleşi Görseli</label>
[cred_field field='_featured_image' value='' required="true" urlparam='' output='bootstrap']
</div>

#907530

Shane
Supporter

Languages: English (English )

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

Hi Eso,

Thank you for contacting our support forum.

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']==12)
    {

        //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);
}

Please add this to your functions.php file and let me know if it helps. Also change the 12 to the ID of your form.

Thanks,
Shane

#907610

Eso

Thanks !