Skip Navigation

[Resolved] Limit Numerical Field Value in Child to the Value of Numerical Field in Parent

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

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 support ticket is created 7 years, 1 month 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.

Our next available supporter will start replying to tickets in about 1.79 hours from now. 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)

Author
Posts
#579644

I have 3 Custom Post Types with the following names and setup...

Ticket is Parent of Ticket Variation, which is Parent of Booking

When a Ticket is created, a value is entered to a numerical custom field titled total-available-for-ticket
Ticket Variations have a numerical custom field titled total-available-for-variation
Bookings have a numerical custom field titled total-places-booked

There are two instances where I need to use CRED Form Validation to control what values can be entered, but have been unable to understand the code required to do so. Any help with the following would be appreciated, especially if you could provide code examples!

1. When a Variation is created as a Child of a Ticket (in CRED), the user can enter a value for how many of that Variation are available in the field total-available-for-variation. I need the potential value the user can enter to be limited so that it cannot be higher than the value of the field total-available-for-ticket in the Parent Ticket.

2. When a Booking is created as a Child of a Variation (in CRED), the user can enter a value for how many places have been Booked in the field total-places-booked. I need the potential value the user can enter to be limited so that it cannot be higher than the value of the field total-available-for-variation in the Parent Variation.

#579689

Okay here is an example that checks the parent Ticket's custom field value and compares that to the number of Variations being added, and returns an error accordingly:

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;
    //validate if specific form
    if ($form_data['id']==12345)
    {
        $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);
}

Modify 12345 to match your new Variation CRED form ID, and adjust the slugs and text as needed.

#579900

This has worked - I do have a follow up question though...

Do I need to repeat the code for each individual form (New Post and Edit Post) or is there a more efficient way?
I ask because of the comment line in the snippet below

    //validate if specific form
    if ($form_data['id']==12345)
#579990

If you need to apply the code to more than one form, you can use this modified version:

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);
}

You can add or remove form IDs in a comma-separated list in the $forms variable.

#580040

Looks like it's all working as hoped - Thanks!