Navigation überspringen

[Gelöst] Require first and last name in a User Form

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to make the first_name and last_name fields required in a User (CRED) Form, and I would like to make the error message translatable. The code I have doesn't seem to be working.

Solution:

add_filter('cred_filter_field_before_add_to_form', 'required_user_fields_func', 10, 2);
function required_user_fields_func($field, $computed_values){
  if(in_array($field['id'], array('first_name', 'last_name'))){
    $field['data'] = is_array($field['data']) ? $field['data'] : array();
    $field['data']['validate']['required'] = array (
      'active' => 1,
      'value' => 1,
      'message' => __('This field is required', 'your-theme-context');
    );
  }
  return $field;
}
This support ticket is created vor 6 Jahren, 7 Monaten. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 5 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von PiotrO586 vor 6 Jahren, 7 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#914267

Hi,

I need to have first and last name required on user registration and editing forms.

I used the following code:

function required_user_account_fields( $field, $computed_values ) {
	if ( in_array( $field[ 'id' ], array( 'first_name', 'last_name' ) ) ) {
		$field[ 'data' ][ 'validate' ][ 'required' ] = array(
			'active' => 1,
			'value' => 1
		);
	}
	return $field;

}
add_filter( 'cred_filter_field_before_add_to_form', 'required_user_account_fields', 10, 2 );

But after updating Toolset to the newest version it doesn't seem to work anymore.

#914487

Hi, please replace your code with this alternate version. It includes a couple of improvements that may resolve the issue:

add_filter('cred_filter_field_before_add_to_form', 'required_user_fields_func', 10, 2);
function required_user_fields_func($field, $computed_values){
  if(in_array($field['id'], array('first_name', 'last_name'))){
    $field['data'] = is_array($field['data']) ? $field['data'] : array();
    $field['data']['validate']['required'] = array (
      'active' => 1,
      'value' => 1,
      'message' => 'This field is required'
    );
  }
  return $field;
}

If this does not seem to help, please tell me more about what happens. Is the form shown at all? Is an error logged anywhere? Can you submit the form without the first or last name?

#914984

Thanks Christian, that helped.

#914990

Dear Christian, although one more think related to your answer, how to make 'This field is required' translatable?

#915000

You can use this code to register the string for translation with WPML String Translation or another translation system:

add_filter('cred_filter_field_before_add_to_form', 'required_user_fields_func', 10, 2);
function required_user_fields_func($field, $computed_values){
  if(in_array($field['id'], array('first_name', 'last_name'))){
    $field['data'] = is_array($field['data']) ? $field['data'] : array();
    $field['data']['validate']['required'] = array (
      'active' => 1,
      'value' => 1,
      'message' => __('This field is required', 'your-theme-context');
    );
  }
  return $field;
}

You can use any custom context to manage the translation of this string.

#915099

Thank you!