I found this post
https://toolset.com/forums/topic/add-ability-to-set-wysiwyg-field-to-required/
I have a wysiwyg custom field and need it to be required.
How do I do this?
Hello,
There isn't such kind of built-in feature, I assume we are talking about a WYSIWYG field in Toolset post form, you can try the cred_form_validate filter hook like this:
Add below codes into your theme file "functions.php":
//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-my-wyswyg-slug']['value'])) {
$errors['wpcf-my-wyswyg-slug'] = 'Missing wyswyg';
}
return array($fields,$errors);
}
Please replace "my-wyswyg-slug" with your custom WYSIWYG field slug
More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
This hook provides custom validation for form fields.
copied and pasted this
//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-scope-of-work']['value'])) {
$errors['wpcf-scope-of-work'] = 'Missing wyswyg';
}
return array($fields,$errors);
}
And got this error
Your PHP code changes were rolled back due to an error on line 11 of file wp-content/toolset-customizations/wysiwyg.php. Please fix and try saving again.
Cannot redeclare validate_form_wyswyg() (previously declared in wp-content/themes/Avada-Child-Theme/functions.php:16)
Line 16 is.......function validate_form_wyswyg( $data ) {
My mistake. Fixed function.php. It was accepted but nothing seems to happen.
1-Paste code to function.php
2-replace "my-wyswyg-slug" with my custom slug "wpcf-scope-of-work"
And should work, correct?
Hi,
Thanks for writing back.
Luo is on vacation so I'll be following up on this ticket.
I've tested the following code on my test website with a field with slug "scope-of-work" and it worked as expected. The error message showed when the form was submitted:
add_filter('cred_form_validate', 'validate_form_wyswyg');
function validate_form_wyswyg( $data ) {
list($fields,$errors)=$data;
if (empty($fields['wpcf-scope-of-work']['value'])) {
$errors['wpcf-scope-of-work'] = 'Missing wyswyg';
}
return array($fields,$errors);
}
Can you please check the code again and make sure that you've used the field's slug correctly?
In case the issue still persists, you're welcome to share temporary admin login details along with the link to the page where this form can be seen.
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar