Skip Navigation

[Resolved] CRED form and required fileds

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 2 replies, has 2 voices.

Last updated by andreaV-4 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2549763

Hi,
I am creating forms to edit fields of a custom post type and would like to understand this.

In my custom post type I have fields, example field1, field2 , field3 and field4. Field2 and field4 are required.

I wanted to understand this:
- From the cred forms can I set a field, example field1, as mandatory even if it is not?
- If I believe an edit cred form, and I enter camp1 and camp2 (camp3 and camp4 are not entered) is the form saved even if camp4 is mandatory (and it is empty because it was not valorized because it was not entered in the cred form)?

Regards

#2550247

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

In my custom post type I have fields, example field1, field2 , field3 and field4. Field2 and field4 are required.
I wanted to understand this:
- From the cred forms can I set a field, example field1, as mandatory even if it is not?
==>
Yes, you can make the field as required on fly using the Toolset form hook: cred_filter_field_before_add_to_form

For example:

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
    //error_log(print_r($field, true));

    if(in_array($field['id'], array('field1'))){
        // in some cases $fields['data'] is an empty string, so you'll need to first set it's expected format for PHP 7.1 compatibility
        if (!is_array($field['data'])) {
            $field['data'] = array();
        }
        $field['data']['validate']['required'] = array ( 
            'active' => 1,
            'message' => 'This field is required'
        ) ;
 
         
    }
    return $field;
}

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

#2553643

My issue is resolved now. Thank you!