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
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.
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
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.
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
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
My issue is resolved now. Thank you!