Skip Navigation

[Résolu] How to use cred api

This support ticket is created Il y a 6 années et 9 mois. 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 réponses, has 3 voix.

Last updated by Shane Il y a 6 années et 8 mois.

Assisted by: Shane.

Auteur
Publications
#556681

Hi Support,

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

The field I want to validate is:

<div class="form-group">
<label>Escort Name</label>
[cred_field field='post_title' post='escort' value='' urlparam='' class='form-control' output='bootstrap']
</div>

Id of the form is: 2882

Even by entering the code in the function.php file

It does not work, I have to do some other operation to run hooks ?

I look forward to feedback
Best regards

#556706

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roberto,

Thank you for contacting our support forum.

Since this is a default post field there is no need to add the wpcf- prefix all you need to do is use the post_title field name in your hook.

Thanks,
Shane

#557048
fail.PNG

Hello Shane,

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

EXAMPLE FORM:

[credform class='cred-form cred-keep-original']

[cred_field field='form_messages' value='' class='alert alert-warning']

[cred_field field='post_title' post='escort' value='' urlparam='' class='form-control' output='bootstrap']

[cred_field field='about-me' post='escort' value='' urlparam='' class='form-control' output='bootstrap']

[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']
[/credform]

#557281

Hi, Shane is currently unavailable but will return tomorrow to continue working on this request. Thanks for your patience.

#557747

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roberto,

Could you provide me with admin access to the website so that I can have a more detailed look ?

The private fields will be enabled for your next response.

Thanks,
Shane

#557919

Hi Shane,

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?

I look forward to feedback
Best Regards

#558216

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roberto,

Yes regex can be used to evaluate the values.

Also yes JS can be used as a frontend mechanism to validate the fields as well.

Thanks,
Shane

#559595

Hello Shane,

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?

I look forward to feedback.
Best Regards

#559737

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roberto,

An example of REGEX would be.

/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

Js is frontend validation so I would recommend having a look here.
https://stackoverflow.com/questions/13785529/add-regex-to-jquery-validate

Please let me know if this helps.
Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.