Skip Navigation

[Resolved] Need help with a form validation

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

Problem: I have a Form that creates RFG instances. I would like to use cred_form_validate to ensure a parent post is selected in the post relationship field, but the parent field always seems to be empty in the $fields array.

Solution:
The empty parent field issue is known and our developers are working on a fix. In the meantime, you can find the parent field information in the $_POST superglobal instead. Use an "_" underscore character instead of a "." period character, like so:

if ($_POST['@exstra-admins_parent']=='')

Relevant Documentation:
https://toolset.com/errata/validation-of-form-fields-for-selecting-related-posts-cannot-be-validated/

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
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)

This topic contains 6 replies, has 2 voices.

Last updated by Rune Brynestad 4 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1471881

I have this form that insert values into a repeatable field group

[credform]
	[cred_field field='form_messages' class='alert alert-warning']
	<div class="form-group">
		<label>Navn</label>
		[cred_field field='navn' force_type='field' class='form-control' output='bootstrap']
	</div>
	<div class="form-group">
		<label>E-post</label>
		[cred_field field='e-post' force_type='field' class='form-control' output='bootstrap']
	</div>
	<div class="form-group">
		<label>Minneside</label>
		[cred_field field='@exstra-admins.parent' class='form-control' output='bootstrap' select_text='--- Velg minneside fra  nedtrekksmeny ---' author='$current']
	</div>
	[cred_field field='form_submit' output='bootstrap' value='Send' class='btn btn-primary btn-lg']
[/credform]

The last field with the label "Minneside" is a dropdown menu containing the available posts that the current author can update. You need to choose one.

I have validated the form using this script:

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']==3692)
    {
        //check my_field value
        if ($fields['wpcf-navn']['value']=='')
        {
            //set error message for my_field
            $errors['wpcf-navn']='Feltet er obligatorisk';
        }
		//check my_field value
		if ($fields['wpcf-e-post']['value']=='')
        {
            //set error message for my_field
            $errors['wpcf-e-post']='Feltet er obligatorisk';
        }
		//check my_field value
		if ($fields['@exstra-admins.parent']['value']=='')
        {
            //set error message for my_field
            $errors['@exstra-admins.parent']='Feltet er obligatorisk';
        }
    }
    //return result
    return array($fields,$errors);
}

The validation works, but my problem is that even if I select a post from the dropdown, I'm not able to submit the form. It still printing a validation error for the dropdown.

When I print the field values using the print_r($fields); function it looks like this:

Array ( [wpcf-navn] => Array ( [value] => Ny testbruker [name] => wpcf-navn [type] => textfield [repetitive] => [plugin_type] => types ) [wpcf-e-post] => Array ( [value] => ca547035@gmail.com [name] => wpcf-e-post [type] => email [repetitive] => [plugin_type] => types [validation] => Array ( [email] => Array ( [active] => 1 [message] => Ugyldig E-post adresse ) ) ) [@exstra-admins.parent] => Array ( [value] => [name] => @exstra-admins.parent [type] => select [repetitive] => ) [form_submit_1] => Array ( [value] => Send [name] => form_submit_1 [type] => submit [repetitive] => ) [_cred_cred_wpnonce_cred_form_3692] => Array ( [value] => 3ae4ff6fd3 [name] => _cred_cred_wpnonce_cred_form_3692 [type] => hidden [repetitive] => ) [_cred_cred_prefix_post_id] => Array ( [value] => 3713 [name] => _cred_cred_prefix_post_id [type] => hidden [repetitive] => ) [_cred_cred_prefix_cred_container_id] => Array ( [value] => 3700 [name] => _cred_cred_prefix_cred_container_id [type] => hidden [repetitive] => ) [_cred_cred_prefix_form_id] => Array ( [value] => 3692 [name] => _cred_cred_prefix_form_id [type] => hidden [repetitive] => ) [_cred_cred_prefix_form_count] => Array ( [value] => 1 [name] => _cred_cred_prefix_form_count [type] => hidden [repetitive] => ) ) 

Any Idea why I can't submit the form?

Thanks in advice

Kind regerda
Rune

#1472017

Hello, sorry for the inconvenience here, but there is currently a known issue with post relationship field validation as described in the following erratum post: https://toolset.com/errata/validation-of-form-fields-for-selecting-related-posts-cannot-be-validated/

Until this issue is resolved in a future release, you can access the selected parent post ID in the $_POST superglobal instead of the $fields array. The key is the same as the field name, except the "." character is replaced by an underscore "_" character like so:

 if ($_POST['@exstra-admins_parent']=='')

You should not change the field name in the $errors array, please leave it as you have it now. Let me know if this workaround does not solve the problem.

#1472545
validation.jpg

Hi Christian

Thanks for looking into this.

The post relationship field validation works as it should now. I get past the validation if I choose from the dropdown menu. However, the last edit caused the first 2 fields to fail, even though I have valid values in the fields. See attached screenshot

My validation code looks like this now

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']==3692)
    {
        //check my_field value
        if ($fields['wpcf-navn']['value']=='')
        {
            //set error message for my_field
            $errors['wpcf-navn']='Feltet er obligatorisk';
        }
		//check my_field value
		if ($fields['wpcf-e-post']['value']=='')
        {
            //set error message for my_field
            $errors['wpcf-e-post']='Feltet er obligatorisk';
        }
		//check my_field value
		if ($_POST['@exstra-admins_parent']=='')
        {
            //set error message for my_field
            $errors['@exstra-admins.parent']='Feltet er obligatorisk';
        }
    }
    //return result
    return array($fields,$errors);
}

Thanks again for looking into this

Kind regards
Rune

#1473177

Okay it looks like you have an important line of code commented out, perhaps you were testing something:

//list($fields,$errors)=$error_fields;

You should uncomment this line to validate the other fields.

#1473925

Yes of course. I commented the wrong line 🙂

Another ting. Could you please tell me how to validate an email address.

The code below check if the email address field is empty. I also want to check if it's a valid email address.

 //check my_field value
        if ($fields['wpcf-e-post']['value']=='')
        {
            //set error message for my_field
            $errors['wpcf-e-post']='Feltet er obligatorisk';
        }

Thanks in advice

Kind regards
Rune

#1475113
Screen Shot 2020-01-23 at 10.24.30 AM.png

Actually if I have an email address field, I would not validate that in PHP. I would make the field required and use the automatic email validation provided by Types/Forms (see the screenshot attached). There's usually no need to recreate this functionality, but if you want to validate in PHP manually for some reason you can check the answer here: https://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php

If you want to leave the field unrequired in Types but enforce a requirement in Forms, we have a PHP API that can help with that - see the example code here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_filter_field_before_add_to_form

#1477163

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.