Skip Navigation

[Resolved] Need help with custom validation

This support ticket is created 6 years, 9 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Last updated by Beda 6 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#610194

I want do check if at least one of three input fields are not empty. If all of fields are empty I want to display a validation error. But the function doesn't return a validation error. What is wrong?

function my_validation($error_fields, $form_data)
{
    //field data are field values and errors    list($fields,$errors)=$error_fields;    
    if ($form_data['id']==2257 || $form_data['id']==2405)
    {
     	if($fields['wpcf-kontakt-typ']['value'] == 3 ){
			if (empty($fields['wpcf-kontakt-name-des-unternehmen']['value']) && empty($fields['wpcf-kontakt-vorname1']['value']) && empty($fields['wpcf-kontakt-nachname1']['value'])) {				
        		$errors['wpcf-kontakt-name-des-unternehmen'] = 'Mind. Unternehmen, Name oder Nachname müssen ausgefüllt werden.';
				$errors['wpcf-kontakt-vorname1'] = 'Mind. Unternehmen, Name oder Nachname müssen ausgefüllt werden.';
				$errors['wpcf-kontakt-nachname1'] = 'Mind. Unternehmen, Name oder Nachname müssen ausgefüllt werden.';
    		}
		}
		
		
    }    
    return array($fields,$errors);
}
add_filter('cred_form_validate','my_validation',10,2);
#610334

Try this instead:

if ( ($fields['wpcf-my_field']['value']=='') && ($fields['wpcf-my_other_value']['value']=='') { magic_code_here }

If that fails, try to remove if ($form_data['id']==2257 || $form_data['id']==2405), so to test when the code is applied (as test) to all forms you have.

I would like to suggest another approach all together thou.

I imagine the user of this form would like to see the error before the form is submitted.
With AJAX.

1. I would suggest you put a message on the form to inform the user about the minimal requirements.
2. Then, to actually validate this, you use CRED Conditional Fields.

Let me put a code example below with 2 fields.
If none is filled, you cannot submit the form and a message will tell you what is wrong.
If at least one field is filled, you may submit the form.

[cred_show_group if="($(first-field) eq  '' ) AND ($(second-field) eq  '' )"  mode='fade-slide']
  You need to fill at least... xy fileds... to submit the form
[/cred_show_group]

[cred_show_group if="($(first-field) ne  '' ) OR ($(second-field) ne  '' )"  mode='fade-slide']
  [cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']
[/cred_show_group]

With this, if no field is filled, the Form cannot be submitted and a message is displayed.
If at least one field is filled, you can submit the form and the message disappears.

And, the best is, this all happens before the user even needs to submit the form.

#611884

Dear Beda,
sorry for my late reply.

I tried your solution, but without success. Should I send you the login details?

Greetings
Frank

#611888

Which solution did you try?

The second works almost fail proof.

Please let me set up an example form with this if you wish.

If it's about the PHP validation, please let me know so I can craft an example for you.

#612152

The problem is, your Custom Fields are already set as required in Toolset > Post Fields > your_fields_group > Edit.
At least Name and Surname are set as required, and even without any additional form of validation, the CRED form will ask you to enter both, Name and Surname.

To have a control like "If at least one of 3" is filled, you need to remove all validations on the Toolset Types level, and add them to the CRED Form Fields instead.

If you want to do that, please proceed by making the Custom Fields "not required" in Toolset > Post Fields > your_post_fields_group > Edit.
Then, create a CRED Form where those Fields are included.
After, you address those Fields with cred_form_validate()
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

An example, that checks if one of 3 fields are filled, and if not, aborts with an error:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==237)
    {
        //check my_field value
        if ( ($fields['wpcf-name']['value']=='') && ($fields['wpcf-surname']['value']=='') && ($fields['wpcf-business']['value']=='') )
        {
            $errors['wpcf-surname']='Fill either of these fields';
            $errors['wpcf-name']='Fill either of these fields';
            $errors['wpcf-business']='Fill either of these fields';
        }
    }
    //return result
    return array($fields,$errors);
}
#613007

Hello!
I think, this isn't the problem. I have inserted a test field wpcf-validation-test (it's set to not required in the Post Fields) to the form and changed the function into:

add_filter('cred_form_validate','my_validation',10,2);

function my_validation($error_fields, $form_data){
  list($fields,$errors)=$error_fields;
  if (($fields['wpcf-validation-test']['value'])==''){
    $errors['wpcf-validation-test'] = 'Required';
   }
  return array($fields,$errors);	
}

But the validation still doesn't work.

#613304

I have provided you a fully working Custom Code example here:
https://toolset.com/forums/topic/need-help-with-custom-validation/#post-612152

To have a control like "If at least one of 3" is filled, you need to remove all validations on the Toolset Types level, and add them to the CRED Form Fields instead.

I tested that on my local install and it works.

If this does not work on your install, it might be that you have complex fields?
My code and your last code works on simple fields like single lines, not repeating.

The same I saw on your install.

Since it seems to not work, I would suggest to update the system, disable Plugins and Theme until this works, and then check what Plugin or Theme is disabling CRED API.

I can also debug this locally, if you can provide me a copy of the site?
https://toolset.com/faq/provide-supporters-copy-site/

I can then make a few tests - as this might also be due to the Server blocking the scripts.

What also is important to know is that those are PHP validations, not AJAX validations.
Hence, you need to submit the form, with PHP, not with AJAX as there we might bypass some validation processes, although it would be elaborated here:
https://toolset.com/documentation/programmer-reference/cred-api/

In general, I suggest to try my example code on a simple test site to see it working. Then, try to find what is blocking it on your site, or send me a copy of that site so I can "take it apart" locally 🙂

#613706

Dear Beda!
Thank you for your support. Before I waste your time, I will test it by myself and inform you about the result as soon as possible.

Greetings
Frank

#613736

Please let me know with anything I can help.

I will be off the next 2 days and back on Saturday.
If the request is urgent, I suggest to open a new ticket.
If not, I will reply as soon I can.

Thank you!