Title: Conditional Required field
Last modified: October 16, 2019

---

# Conditional Required field

 *  Resolved [kpmediadesigner](https://wordpress.org/support/users/kpmediadesigner/)
 * (@kpmediadesigner)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/conditional-required-field/)
 * 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](https://wordpress.org/support/users/kpmediadesigner/)
 * (@kpmediadesigner)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/conditional-required-field/#post-12036653)
 * 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](https://wordpress.org/support/users/gwin/)
 * (@gwin)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/conditional-required-field/#post-12037835)
 * 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](https://wordpress.org/support/users/kpmediadesigner/)
 * (@kpmediadesigner)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/conditional-required-field/#post-12113062)
 * 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.

 * ![](https://ps.w.org/wpadverts/assets/icon-256x256.png?rev=2423472)
 * [WPAdverts - Classifieds Plugin](https://wordpress.org/plugins/wpadverts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wpadverts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wpadverts/)
 * [Active Topics](https://wordpress.org/support/plugin/wpadverts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wpadverts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wpadverts/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [kpmediadesigner](https://wordpress.org/support/users/kpmediadesigner/)
 * Last activity: [6 years, 5 months ago](https://wordpress.org/support/topic/conditional-required-field/#post-12113062)
 * Status: resolved