Home › Toolset Professional Support › [Resolved] Validate generic field
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Tagged: Toolset Forms
This topic contains 15 replies, has 2 voices.
Last updated by Nicholas 6 years, 6 months ago.
Assisted by: Minesh.
Hey
I am using the following generic field for YouTube video URLs
[cred_generic_field field='videourl1' type='url' placeholder='Paste Youtube Video URL' class='videourl1 sfc-campaign-new-input' urlparam='']
{
"required":1,
"validate_format":0,
"default":""
}
[/cred_generic_field]
How do I validate this generic field to only allow YouTube URLs?
Regards,
Nick
Hello. Thank you for contacting the Toolset support.
You can validate the Generic fields using CRED API hook: cred_form_validate
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
For example:
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']==99999){ //check my_field value if ($fields['videourl1']['value']!='correct_value') { //set error message for my_field $errors['videourl1']='Wrong Value'; } } //return result return array($fields,$errors); }
Where:
- Replace 99999 with your orignal form ID
- Replace field names as required and adjust the code as required
Thanks.
Will this work with AJAX too?
Because the standard field will show the validation above the field and will give the user an immediate response.
You mean - ajax form submit - correct? yes, it should work.
Yes but will the error show above the generic field?
You know as the user types and the user doesn't paste a youtube url but something that isn't an url the user should get instant feedback. Do you know what I mean?
Yes - it will work.
You need to set "validate_format":1 to validate the URL format.
For example:
[cred_generic_field field='videourl1' type='url' class='' placeholder='Paste Youtube Video URL' urlparam=''] { "required":1, "validate_format":1, "default":"" } [/cred_generic_field]
Now, if you enter any other content other than URL it will throw the error just above the field.
Thanks.
Why do I need this code again?
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']==99999){ //check my_field value if ($fields['videourl1']['value']!='correct_value') { //set error message for my_field $errors['videourl1']='Wrong Value'; } } //return result return array($fields,$errors); }
Because it seems to work without this code.
ahh - well, I shared that as I thought you want to validate generic field against some value. If you just want to make it required the code I shared is not needed, feel free to remove it.
Ah thanks.
So if I want the user to enter youtube urls only then I with your code I could do something like this maybe?
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']==99999){ //check my_field value if ($fields['videourl1']['value']!='<em><u>hidden link</u></em>') { //set error message for my_field $errors['videourl1']='Wrong Value'; } } //return result return array($fields,$errors); }
so a url from i.e.: hidden link shouldn't work. The user should only be allowed to paste YouTube URLs
Well - You should try something like the following code to validate the youtube URL.
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']==99999){ //check my_field value $parts = parse_url($fields['videourl1']['value']); if $parts['host'] != 'youtube.com' or $parts['host'] != 'www.youtube.com' ) { //set error message for my_field $errors['videourl1']='Wrong Value'; } } //return result return array($fields,$errors); }
Hm.. unfortunately this code breaks my site.
ahhh - there is a typo.
Please use following code:
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']==99999){ //check my_field value $parts = parse_url($fields['videourl1']['value']); if ($parts['host'] != 'youtube.com' or $parts['host'] != 'www.youtube.com' ) { //set error message for my_field $errors['videourl1']='Wrong Value'; } } //return result return array($fields,$errors); }
Dont forget to replace the form ID.
Hm... while this doesn't break my site anymore it still doesn't work.
I pasted hidden link but I still get the error. I am not sure why.
Well - to check that I need problem URL where you added CRED form and access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Do you need help here?