Skip Navigation

[Resolved] How can I make user field required without it being added in every user form?

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 6.87 hours from now. Thank you for your understanding.

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

Last updated by Minesh 4 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2706109

I have several user forms. There is one group of user fields where I want to know the diet wishes of someone. This field is mentioned in several other user forms. There is one question if the client is allergic. Conditional logic opens een mutli line field asking them what allergic it is. This field needs to be required. I can't make it required in the user fields level because then they will show up in every form where I can't delete them. In the normal mode I haven't any option to make the field required so I have to switch to expert mode. How can I make the field required there? I read that I have to add code to functions.php but is this really neccessary? It feels not right to have to add so much code for only to make something required. Why is this?

Chatgpt says "[cred_field field='user_email' value='' urlparam='user_email' required="required"]: This line makes the email field required.". But I in in doubt if this is the case.

I hope you don't mind explaining it to me?

Cheers
Marco

#2706223

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to set the mutli line field required in one form but not with others - are you using the same field with other forms?

If you can share problem URL of your form on frontend and share admin access details, once I review your current setup I will be able to guide you in the right direction.

*** 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.

#2706237

Minesh
Supporter

Languages: English (English )

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

Can you please share information for what form you want to make the field required and what is the field name and share frontend URL of the page where you added the form.

#2706238

Stupid me!!

This is the user field group hidden link and this is the field hidden link

This is one of the forms hidden link and here you find the field hidden link

Sorry for that!

#2706241

Minesh
Supporter

Languages: English (English )

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

Yes but on frontend where on what page I can see the form?

Also, the field you are talking, you want to make it required only when the conditional group condition is true - is that correct?

#2706242

Yes but on frontend where on what page I can see the form?
hidden link

Also, the field you are talking, you want to make it required only when the conditional group condition is true - is that correct?
Yes please!

#2706245

Minesh
Supporter

Languages: English (English )

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

Yes but on frontend where on what page I can see the form?
hidden link
===>
Thank you for sharing the above URL but I do not able to see any form on the above URL. Can you please guide me how can I see the form or do I need to login as frontend user? if yes, please share access details for frontend user.

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

#2706246

Hi Minesh,

It was a new test site. I had to refresh the permalinks. Now it works.

#2706276

Minesh
Supporter

Languages: English (English )

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

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

add_filter('cred_filter_field_before_add_to_form', 'func_set_conditional_required', 10, 1);
function func_set_conditional_required($field){
   $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('vul-aan-welke-allergie-je-hebt');
    if($form_id==1447 and in_array($field['id'],$field_slugs)){
 
       // 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;
}

Can you please confirm it works as expected:

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

#2706281

Hi Minesh,

It is looking good! Why is this so difficult? Why could there not be a required checkbox on every field in the fields in the form?

Cheers
Marco

#2706283

Minesh
Supporter

Languages: English (English )

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

Well - Honestly, I do not have answer for that and I do not know why it's so difficult as I'm not the right person to answer the question you asked, as I'm not the decision maker and do not decide weather features should be added to product or not.

I'm happy to share the solution and glad to know that solution I share help you to resolve your issue.

#2706284

Thanks for your support Minesh! I will make a clipping of this for future use.