Skip Navigation

[Resolved] Make CRED fields required, but do not require the fields in wp-admin

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to make some custom fields required in a CRED Form, but I would like to make the same fields optional when editing in wp-admin.

Solution:

add_filter( 'cred_form_validate', 'custom_story_validation', 10, 2 );
function custom_story_validation( $data, $form_data ) {
  $forms = array( 123, 456 );
  if( in_array( $form_data['id'], $forms ) ){
    list($fields,$errors)=$data;
    if (empty($fields['wpcf-email'])) {
      $errors['email'] = __('Email address is required', 'your-language-domain');
    }
    if (empty($fields['wpcf-telephone'])) {
      $errors['telephone'] = __('Telephone is required', 'your-language-domain');
    }
    if (empty($fields['wpcf-location'])) {
      $errors['location'] = __('Location is required', 'your-language-domain');
    }
    $data =array($fields,$errors);
  }
  return $data;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

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

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by Christian Cox 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#785885

Hi there,

We have a post type ("yourstories") which has a CRED form for visitors to submit their stories.

Sometimes visitors will submit the stories from the front end, in which case we require some additional fields to be completed (email, telephone, location, etc).

Sometimes site admins create these stories on the backend of the site itself. In these cases, those fields are not required.

But if I make those fields required in Types, then they are required regardless of whether the story is being added by a site visitor form the front end, or an admin on the back.

So my question: is there any way to make it so that fields are required when the post is created from the front, but not required when created in the backend by an admin (in wp-admin)?

Many thanks and best wishes,
Andrew.

#786594

Hi, you can use the cred_form_validate hook to enforce any requirements on a CRED form, and these requirements will not be applied when editing the same post in wp-admin. These requirements are enforced on the back-end, meaning that the form must be submitted before the validation is performed. Then the error messages will appear in the form if necessary. Here's an example:

add_filter( 'cred_form_validate', 'custom_story_validation', 10, 2 );
function custom_story_validation( $data, $form_data ) {
  $forms = array( 123, 456 );
  if( in_array( $form_data['id'], $forms ) ){
    list($fields,$errors)=$data;
    if (empty($fields['wpcf-email'])) {
      $errors['email'] = __('Email address is required', 'your-language-domain');
    }
    if (empty($fields['wpcf-telephone'])) {
      $errors['telephone'] = __('Telephone is required', 'your-language-domain');
    }
    if (empty($fields['wpcf-location'])) {
      $errors['location'] = __('Location is required', 'your-language-domain');
    }
    $data =array($fields,$errors);
  }
  return $data;
}

Change 123, 456 to be the ID of the CRED form, or a comma-separated list of form IDs. Then you may need to adjust the field slugs and error slugs to match your custom field names.

#786793

Thanks Christian.

When you say: "These requirements are enforced on the back-end, meaning that the form must be submitted before the validation is performed. Then the error messages will appear in the form if necessary. "

...how does this work out? If a visitor submits the form without required fields, how are they informed?

Many thanks,
Andrew.

#787414

...how does this work out? If a visitor submits the form without required fields, how are they informed?
The error messages will appear in your CRED form wherever you place the form_messages field shortcode.

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

If errors are found when the form is submitted, the Story post will not be created or edited. The form will be displayed again with the error messages.

#788429

Thank you,

Will that happen immediately they submit...so they would be aware on the screen that appears after submission, and they'd be told what needed to be corrected?

#788897

Correct, the error messages will be displayed on the page after they submit the form.

#903894

Hi Christian,

Where do I get error slugs from?

#903944

Hi Christian,

I have removed the Required checkbox from each filed, completed the code in functions.php that you advised, and then tried submitting the form from the front end without completing the required fields...but the form submits without error messages 🙁

Hope you can help.

Note that I don't know what I should put in the code at each point where it says $errors[' ']. I've madeup slugs there as I don't know what to use. Could this be the issue?

add_filter( 'cred_form_validate', 'custom_story_validation', 10, 2 );
function custom_story_validation( $data, $form_data ) {
  $forms = array( 43206 );
  if( in_array( $form_data['id'], $forms ) ){
    list($fields,$errors)=$data;
    if (empty($fields['ecpt_field_yourstory_name'])) {
      $errors['nameerror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['ecpt_field_yourstory_info'])) {
      $errors['infoerror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-email'])) {
      $errors['emailerror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-telephone'])) {
      $errors['telephoneerror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-year-of-birth'])) {
      $errors['yearbirtherror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-place-of-birth'])) {
      $errors['placebirtherror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-place-currently-living'])) {
      $errors['livingerror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-current-occupation'])) {
      $errors['occupationerror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-time-period'])) {
      $errors['perioderror'] = __('This field is required.', 'your-language-domain');
    }
    if (empty($fields['yourstory-new-copyright-agreement'])) {
      $errors['copyrighterror'] = __('This field is required.', 'your-language-domain');
    }
    $data =array($fields,$errors);
  }
  return $data;
}
#904528

Check the example I showed above as a reference:
https://toolset.com/forums/topic/required-fields-3/#post-786594

The field slugs in the $fields array should be defined depending on the type of custom field. If the field is a custom field created in Types, then the slug is "wpcf-" plus the slug of the custom field from wp-admin. So if your field was created in Types, and has the slug 'placebirth' in wp-admin, then the field slug in the $fields array code should be 'wpcf-placebirth'. If the fields are generic fields, then you can eliminate the 'wpcf-' prefix.

The slugs in the $errors array should not use the 'wpcf-' prefix, but should otherwise match the slug in the $fields array.

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