Skip Navigation

[Resolved] Validation of form fields both custom and non-custom

This support ticket is created 5 years ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 1 reply, has 2 voices.

Last updated by Beda 5 years ago.

Assisted by: Beda.

Author
Posts
#1488975

Tell us what you are trying to do?

I have seen threads such as this one that allow you to setup validation of custom fields such as restricting the number of characters.
https://toolset.com/forums/topic/limit-and-control-fields-input-of-a-content-form/

However, is it possible to do this for non-custom fields such as Post Title and Content?

In short I would like to restrict the number of characters for post titles and content fields when the post is being submitted from a front end form.

Rob.

#1489241

Such validations are not native to Forms, and if you need or want to see them in Forms, or Toolset, you would have to suggest them here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Product Management can then decide to implement such features or not.

To understand what you can validate with the Toolset Forms API hook cred_form_validate() you need minimal PHP and WordPress API understanding.
Then, you can consult this DOC https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate where we explain how the hook works.

To find out what you can validate you should always use something like error_log(print_r($variable_to_debug, true));, with this statement you can see exactly what is in the $variable_to_debug.
In our case this would be $fields, to see if the Post title is part of this variable.

It is, as if you do error_log(print_r($data, true)) in your form you will see that it is an array of arrays.
And one of the first keys is "post_title"
That is the value of the post title field.
It is by default validated with

[active] => 1
[value] => 1
[message] => This field is required

So, you can check this for example (if title has more than 20 characters)

add_filter( 'cred_form_validate', 'my_function', 10, 2 );
function my_function( $data, $form_data ) {

    list($fields,$errors)=$data;
    // error_log(print_r($fields, true));
    // error_log(print_r($errors, true));
	if ( strlen($fields['post_title']['value']) > 20 ) {
	    $errors['post_title'] = __('Maximum 20 characters for this field', 'your-language-domain');
	}
    
    $data =array($fields,$errors);

  return $data;
}

Note that this is custom code, and for complete custom code assistance, you'd have to contact a contractor or other 3rd party developers (https://toolset.com/contractors/).

I hope the above helps!