Skip Navigation

[Resolved] Split: Registering Users by front end form – make required field

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

Last updated by Minesh 10 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2682542

Hi Minesh, thank you for the answer. Q1 is solved. I have deactivated all unnecessary plugins. Used a standard theme. And creating an account now works.

So now Q2. To make some fields required. I use the expert mode of the form editor. And then:
<div class="form-group">
<label for="%%FORM_ID%%_first_name">[cred_i18n name='first_name-label']First name *[/cred_i18n]</label>
[cred_field field='first_name' class='form-control' output='bootstrap' required='true']
</div>
Code found here: https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_field
But if I leave the field empty, it still is submitted.
You can see it here: hidden link

#2682544

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share admin access details and let me review whats going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2682568

Minesh
Supporter

Languages: English (English )

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

Can you please try to add the following cod the "Custom Code" section offered by Toolset:
Or
To your current theme's functions.php file:

add_filter('cred_filter_field_before_add_to_form', 'func_set_firstname_lastname_required', 10, 2);
function func_set_firstname_lastname_required($field, $computed_values){

   $form_id = 0;
   $form_html_id = $field['form_html_id'];
    
   if ( isset($form_html_id) ) {
        $parts = explode( '_', $form_html_id);
        $form_id = (int) $parts[2];
    }
    $field_slugs = array('first_name', 'last_name');
    if(in_array($field['id'],$field_slugs)){
        $field['data']['validate']['required'] = array ( 
            'active' => 1, 
            'value' => 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

Please check the following related ticket:
- https://toolset.com/forums/topic/split-user-sign-up-not-working-validate-user-form/#post-1392381

#2682854

Hi Minesh, thank you for help.
With the code provided I can still make an account without filling First en Last name.
When I change $form_id = 0; to $form_id = 3803; (My USerform ID number) still the same.

And when I use this code:
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;
}
It's the same.
With all 3 I can make an account without any required field filling.

Is there something wrong with my form??

#2682860

Minesh
Supporter

Languages: English (English )

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

Can you please tell me where exactly you added the code I shared and let me review whats going wrong with your setup.

#2683262

Hi Minesh,
I added the code in the functions.php from my child theme. I have send you the credentials already, and the site is a copy of the live site. So feel free to check my setup.
Kind regards,
Corine

#2683363

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

Actually when I logged in in the admin and I found that your current theme is twentytwentyfour.
- hidden link

I edit the functions.php file of the "twentytwentyfour" theme and added the following code:

add_filter('cred_filter_field_before_add_to_form', 'func_set_firstname_lastname_required', 10, 2);
function func_set_firstname_lastname_required($field, $computed_values){
 
   $form_id = 0;
   $form_html_id = $field['form_html_id'];
     
   if ( isset($form_html_id) ) {
        $parts = explode( '_', $form_html_id);
        $form_id = (int) $parts[2];
    }
    $field_slugs = array('first_name', 'last_name');
	
    if($form_id==3803 and in_array($field['id'],$field_slugs)){
	$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;
}

I can see it makes the fields required. Can you please confirm it works at your end as well.

#2683576

Hi Minesh, thank you so much! Your totaly right about the child theme. Excuse! It is working now. Kind regards,
Corine