• Resolved kpmediadesigner

    (@kpmediadesigner)


    Hello,

    So I have one more question for you (thank you for your help on the location requirement post, gave you a 5star review too). I am trying to make a field conditionally required, as in depending on a radio button (let us call the options A and B ), if they select A then they don’t have to fill in other fields, but if they pick B those fields are in fact required. I have validators working, and I want to force default values if they picked A ( which is why its not required because the values are automatically filled in by a validator setting $_POST[‘some_value’] = $a_default.
    I’m very happy with the plugin so far, and a lot of the custom functionality I need is almost done already.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter kpmediadesigner

    (@kpmediadesigner)

    Ok new plan. I’m going to make each have a validator to check post for the value of the Select field and make my own is_required validator. I’ll post code once I get it working for anyone else who it may help.

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    thanks for the 5-star review :).

    If you will be having problem with the customization, please post the code snippet you are working on i should be able to help you with that.

    Creating a custom is_required validator which would be conditional based on the value selected in the dropdown seems like a good solution.

    One other approach you can try is making all fields optional and in adverts_form_load checking the submitted value and if needed making the fields required.

    It would be something like this

    
    add_filter( "adverts_form_load", function( $form ) {
      if( $form["name"] != "advert" ) {
        return $form;
      }
      if( $_POST['select-to-check'] != 'make-fields-required' ) {
        // leave the fields as optional
        return $form;
      }
      $make_required = array( "field_1", "field_2" );
      foreach( $form["field"] as $k => $field ) {
        if( in_array( $field["name"], $make_required" ) ) {
          $form["field"][$k]["is_required"] = true;
          $form["field"][$k]["validator"][] = array( "name" => "is_required" );
        }
      }
      return $form;
    } );
    

    Where ‘select-to-check’ is actual name of dropdown and ‘make-fields-required’ is actual value which makes fields named “field_1”, “field_2” required, so you would need to adjust the values to your needs.

    Thread Starter kpmediadesigner

    (@kpmediadesigner)

    That’s way better than what I was going to do. Perfect! Sorry, I took so long to respond. I have been working on this major project constantly for a couple of months now. Big help thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional Required field’ is closed to new replies.