I'm new to toolset, I was trying to validate a form using cred api.
I entered the following code in the function.php file :
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']==2882)
{
//check my_field value
if ($fields['wpv-post-title']['value']<'10')
{
//set error message for my_field
$errors['wpv-post-title']='Minore di 10';
}
}
//return result
return array($fields,$errors);
}
I created a small test form. I want to implement a check on field about-me.
In the reference guides posted on your site I found this code , I've put this code in my file function.php,
I'm obviously changing the slug of the field.
Theoretically if I submit the form leaving the blank field I should receive a alert.
My problem is that when I make the form subbmit I do not receive any alert.
What am I wrong I do not understand?
Can you give me an example based on my test form?
I look forward to feedback.
Best Regards.
Davide
EXAMPLE CODE
//Text-like fields (textarea, WYSIWYG) have only one value, which is the actual input text.
add_filter('cred_form_validate', 'validate_form_wyswyg');
function validate_form_wyswyg( $data ) {
list($fields,$errors)=$data;
if (empty($fields['about-me']['value'])) {
$errors['about-me'] = 'Missing wyswyg';
}
return array($fields,$errors);
}
I solved my initial issue, I verified and I was wrong to inserting the slug into the function.
Now I want understand how to validate a text field using this function:
I need to make a check on the characters entered by the user, for example excluding the symbols! $ /.. ecc
regex can be used?
//Text-like fields (textarea, WYSIWYG) have only one value, which is the actual input text.
add_filter('cred_form_validate', 'validate_form_wyswyg');
function validate_form_wyswyg( $data ) {
list($fields,$errors)=$data;
if (empty($fields['wpcf-wyswyg']['value'])) {
$errors['wpcf-wyswyg'] = 'Missing wyswyg';
}
return array($fields,$errors);
}
in alternative to your API i can use jquery to validate the form field?
Ok, can you provide me 2 validation examples ?
One using jquery and one utilizing your api eventually with a regex.
So far my team and I have not been able to implement jquery.
Can you give us a demonstration of how to use jquery in post forms and how to use your Api with regexes?
/Text-like fields (textarea, WYSIWYG) have only one value, which is the actual input text.
add_filter('cred_form_validate', 'validate_form_wyswyg');
function validate_form_wyswyg( $data ) {
list($fields,$errors)=$data;
$field_value = $fields['wpcf-myfield']['value']
$pattern='~\b(\d+)\s*(\w+)$~';
if (preg_match($pattern,$field_value,$match) == false) {
$errors['wpcf-wyswyg'] = 'Missing wyswyg';
}
return array($fields,$errors);
This should work if the regex fails, I suggest taking a look at the link here. hidden link