Skip Navigation

[Resolved] cred_filter_field_before_add_to_form hook – how to use it

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

Problem:
How to use the cred_filter_field_before_add_to_form hook to make a user first and last name required on a User Form?

Solution:
This requires some custom code, that you can write based on this example:
https://toolset.com/forums/topic/user-form-problem-first_name-and-last_name-dont-work-in-required_fields_func/#post-1472921

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

This support ticket is created 4 years, 3 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by katjaL 4 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#1452409

I have a user registration form and I need to have required fields in frontend, not in backend. I found this from your documentation:

// make the first_name and last_name fields in a User form required
add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
if(in_array($field['id'], array('first_name', 'last_name'))){
// in some cases $fields['data'] is an empty string, so you need to first set its expected format
$field['data']['validate']['required'] = array (
'active' => 1,
'message' => 'This field is required'
) ;
}
return $field;
}

But for me this code produces this error in frontend "There has been a critical error on your website."
If I put my other (custom) fields instead, everything works ok. But these first_name and last_name break the form.

#1452583

The code is wrong.
It produces a

Warning: Illegal string offset 'validate' in (line of code where $field['data']['validate']['required'] is populated)

In that Forms hook, "$field" holds (for each Form Field) an array like the below (taken from the "First Name" Field)

  'id' => string 'first_name' (length=10)
  'slug' => string 'first_name' (length=10)
  'type' => string 'textfield' (length=9)
  'name' => string 'First Name' (length=10)
  'data' => string '' (length=0)
  'meta_key' => string 'first_name' (length=10)
  'post_type' => string 'user' (length=4)
  'meta_type' => string 'usermeta' (length=8)
  'post_labels' => string 'First Name' (length=10)
  'plugin_type' => string '' (length=0)
  'plugin_type_prefix' => string '' (length=0)
  'form_html_id' => string 'cred_form_26_1_first_name' (length=25)
  'field_name' => string 'first_name' (length=10)
  'title' => string 'First Name' (length=10)
  'field_configuration' => string '' (length=0)
  'field_value' => string '' (length=0)

So $field['data']['validate']['required'] can't really produce anything, as $field['data'] already is an empty string.

But this is not the case with all inputs.
Most other Form Fields will feature data in the $field['data']
For the nickname, it's an empty array, and for First, Last Name, Description an empty string.

The DOC says it may not always be the same:
data. An associative array containing repetition and validation configurations, which vary by field type and may include but are not limited to.

But, given the only available code sample is (almost seems on purpose) set to the one inputs that won't return any data, there is either a BUG or an error in the doc.

I will investigate this and let you know how this will be resolved next week.

#1472921

History repeats itself, and I forgot that I debugged this precise issue already in past.

The thing is using PHP 7.1 you'll need to specify if something is a string or array, and hence, the code in the example of the DOC needs an update like below for PHP 7.1 compatibility:

// make the first_name and last_name fields in a User form required
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('first_name', 'last_name'))){
        // in some cases $fields['data'] is 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;
}

add_filter('use_block_editor_for_post', '__return_false', 10);

That should help you resolve the issue.

I requested (again) to update the DOC as well 🙂

#1474387

Thank you Beda, works! My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.