Dear Sir/Madam,
I have a CRED form
[credform]
[cred_field field='application-tour-number' force_type='field' class='form-control ' output='bootstrap' urlparam='tour-id']
[cred_field field='form_messages' class='alert alert-warning']
...
If I empty the field 'application-tour-number', post still can be saved.
function fn_cred_form_validate($error_fields, $form_data)
{
list($fields,$errors)=$error_fields;
if ($form_data['id']==221) {
if (empty($fields['wpcf-application-tour-number']['value'])) {
$errors['wpcf-application-tour-number']='Please select tour first';
}
}
return array($fields,$errors);
}
add_filter('cred_form_validate','fn_cred_form_validate',10,2);
I did try
if ($fields['wpcf-application-tour-number']['value']=='') {
$errors['wpcf-application-tour-number']='Please select tour first';
}
but no error display and continue to save.
Please advise how problem to my form and script.
Try this syntax:
if (empty($fields['wpcf-application-tour-number']['value'])) {
$errors['application-tour-number']='Please select tour first';
}
I think the wpcf- prefix is required in $fields but not in $errors.
I do try empty() or $errors['application-tour-number'] or $errors['wpcf-application-tour-number'] but not work
function fn_cred_form_validate($error_fields, $form_data)
{
list($fields,$errors)=$error_fields;
if ($form_data['id']==221) {
if (empty($fields['wpcf-application-tour-number']['value'])) {
//set error message for my_field
$errors['wpcf-application-tour-number']='Please select tour first';
}
}
return array($fields,$errors);
}
add_filter('cred_form_validate','fn_cred_form_validate',10,2);
I just ran a test with a Types number field, and this format is validated as I expected:
if (empty($fields['wpcf-slug']['value']))
{
//set error message for slug field
$errors['slug']='slug field number is required';
}
The wpcf- prefix is required for Types custom fields in the $fields array, but the wpcf- prefix should not be used in the $errors array. Maybe there is another difference in the fields we are validating.
- What type of field is application-tour-number?
- Is it a Types custom field, or a generic field?
- Does this field allow multiple values (repeating field), or only one value?
Dear Christian Cox,
Answer your question as below
application-tour-number is a single line field
I put below code in CRED form
[cred_field field='application-tour-number' force_type='field' class='form-control ' output='bootstrap' urlparam='tour-id']
The field is only one value
Something else must be going on, because this syntax is correct for a single line text field:
function fn_cred_form_validate($error_fields, $form_data)
{
list($fields,$errors)=$error_fields;
if ($form_data['id']==221) {
if (empty($fields['wpcf-application-tour-number']['value'])) {
//set error message for my_field
$errors['application-tour-number']='Please select tour first';
}
}
return array($fields,$errors);
}
add_filter('cred_form_validate','fn_cred_form_validate',10,2);
I just tested this in a local environment and a validation message is displayed when the empty form is submitted. I need to log in and take a closer look. Can you provide:
- Login credentials
- URL where I can see the Form on the front-end of the site
- Where can I find the custom code with cred_form_validate hook?
I just tested the Form here:
hidden link
I see an error message if the Tour Number field is empty. See the screenshot empty-tour.png here. The error message says "The post was not saved because of the following problem: Tour Number: Please select tour first".
If I add a tour number like 123, the Form submits successfully and I am redirected to the single Application post:
hidden link
See application-post.png here. Everything seems to work as expected in this simple test. What am I missing?
My issue is resolved now. Thank you!