Conditional Logic with Validation

Support MB Conditional Logic Conditional Logic with Validation

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #16190
    CreativeCreative
    Participant

    I'm having an issue with a my post updating when I have a required field that is hidden Conditional Logic. The field shows when a choice is selected in dropdown. How can I get this to work?

    Here is how it works:
    I have a required select_advanced field called Status with 3 options: Available, Under Contract and Sold. I have another required field called Sold Price and only shows if "Sold" is selected in the Status field. Since my Sold Price field is required and hidden, I can't update my post without having validation issues.

    Code:

    
    // Start Fields
    'fields' => array(
    
    // SELECT ADVANCED - Status
      array (
        'id' => $prefix . 'status',
        'name'          => 'Status',
        'type'          => 'select_advanced',
        'placeholder'   => 'Select Status',
        'options'       => array(
          'Available'     => 'Available',
          'UnderContract' => 'Under Contract',
          'Sold'          => 'Sold',
        ),
      ),
    
    // TEXT - Sold Price
      array (
        'id'      => $prefix . 'sold_price',
        'type'    => 'text',
        'name'    => 'Sold Price',
        'prepend' => '$',
        'hidden'  => array( $prefix . 'status', '!=', 'Sold' )
      ),
    ), // End Fields
    
    // Start Validation
    'validation' => array(
      'rules'  => array(
        $prefix . 'staus' => array(
          'required'  => true,
        ),
        $prefix . 'sold_price' => array(
          'required'  => true,
        ),
      ),
    
    ) // End Validation
    
    #16237
    Anh TranAnh Tran
    Keymaster

    Hi,

    This is a common problem. Logically, when a field is not needed (hidden), it can't be required. In this case it's the price field. Unfortunately, there's no built-in solution for toggling validation rules based field visibility.

    #17575
    SIWSIW
    Participant

    I think it should be possible to exclude hidden inputs using the ignore options of jQuery Validation.

    I've tested this by changing:

    ignore: ':not([class|="rwmb"])',

    in wp-content\plugins\meta-box\js\validate.js to

    ignore: ':not([class|="rwmb"]), [data-visible="hidden"] .rwmb-input input',

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.