Problem: I would like to use back-end validation to ensure that a User is not able to create a post with a numeric custom field value that is higher than a numeric custom field value set in its parent post.
Solution: Use the cred_form_validate hook to perform validation checks against the submitted value and the parent value:
add_filter('cred_form_validate','my_variation_validation',10,2); function my_variation_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; $forms = array( 12345, 23456, 34567 ); //validate if specific form if ( in_array( $form_data['id'], $forms )) { $ticketID = $fields['_wpcf_belongs_ticket_id']['value']; $ticketsAvailable = intval(get_post_meta($ticketID, 'wpcf-total-tickets-available', true)); if (intval($fields['wpcf-total-available-for-variation']['value']) > $ticketsAvailable) { //set error message for the total available field $errors['wpcf-total-available-for-variation']='A maximum of ' . $ticketsAvailable . ' variation(s) can be added.'; } } //return result return array($fields,$errors); }
Replace 12345, 23456, 34567 with a comma-separated list of forms where this validation should be applied.
Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
https://toolset.com/documentation/user-guides/many-to-many-post-relationship/
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 4 replies, has 2 voices.
Last updated by jonB-5 6 years, 6 months ago.
Assisted by: Christian Cox.