• Resolved bshuchter

    (@bshuchter)


    Hi. I really like this plugin, but am having a problem combining Required fields with Conditional Logic. I’ve seen this question asked in the forum but could not find any answers.

    I have a radio field called “Role” with two choices: Teacher and Administrator. If the user chooses “Teacher” then some questions relating to teachers appear on the form, using conditional logic. (Otherwise, they remain hidden.) If the user chooses “Administrator” then there are different questions that appear for administrators, using conditional logic. (Otherwise, they remain hidden.) All this works as expected.

    I’d like to make all the Teacher questions and the Administrator questions Required. However, since one set of questions will always remain hidden AND required, the form will not submit.

    What’s needed is for required fields to be required only if they are being displayed. Is there a way to accomplish this?

    Thanks!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hi @bshuchter,

    I’m afraid it is not possible in the current version of the plugin, however, thanks for letting us know, I will discuss this option with the team and we will see if we can add this in the future updates.

    Regards.

    andrewfloatingbridge

    (@andrewfloatingbridgecomau)

    Damn! I would really like this functionality too

    Please add this in the future updates.

    Please, please, please :))!

    Thread Starter bshuchter

    (@bshuchter)

    For what it’s worth, I used Gravity Forms on another website, and when a field is hidden, they disable the test to see whether that field is required.

    Do you know how can i do this in Ultimate Member?

    Thread Starter bshuchter

    (@bshuchter)

    No, sorry, I don’t know how to do that in Ultimate Member.

    Tried to solve this on my own. I think I’ve solved it, but let me know if it doesn’t work for you (tell me if you’re using show or hide as your conditional logic). Note that I’ve only tested it for 1 conditional logic (ie. the field shows up when … or the field hides when … ), not multiple conditions

    You’ll need to edit the um-actions-form.php in /plugins/ultimate-member/includes/core/

    Find the line:
    $array = apply_filters( 'um_get_custom_field_array', $array, $fields );

    add the following line under it:
    $field_not_required_when_hidden_by_cond_logic = false;
    The above line of code creates a variable that we’ll use to keep track if the field is visible or hidden.
    The variable is initially set to false.
    When the variable is true, we ignore the required option for the field.
    When the variable is false, we allow validation to handle it if it was required and not filled out.

    Next, we’ll be adding in the following line multiple times:
    $field_not_required_when_hidden_by_cond_logic = true;

    Add this line right before each continue 2

    So it should look similar to this (make sure to do it for each of $op == )

    if ( $visibility == 'hide' ) {
         if ( $op == 'empty' ) {
            if ( empty( $cond_value ) ) {
    	   $field_not_required_when_hidden_by_cond_logic = true;
    	   continue 2;
    	}
         } elseif ( $op == 'not empty' ) {	
             if ( ! empty( $cond_value ) ) {						 
                 $field_not_required_when_hidden_by_cond_logic = true;
    	     continue 2;
    	  }
         ...
          elseif ( $visibility == 'show' ) {
    	if ( $op == 'empty' ) {
    	   if ( ! empty( $cond_value ) ) {							 
                    $field_not_required_when_hidden_by_cond_logic = true;
    	        continue 2;
    	    }
            } elseif ( $op == 'not empty' ) {
    	    if ( empty( $cond_value ) ) {						 
                   $field_not_required_when_hidden_by_cond_logic = true;
    	        continue 2;
    	    }
    	} 

    We’re not done yet. We’ve set the variable to be true when the field is hidden, but we need to stop the plugin from creating errors if the variable is true.

    So we add the following line:
    if ($field_not_required_when_hidden_by_cond_logic == false){
    That line goes right above these lines:
    if ( isset( $array['type'] ) && $array['type'] == 'checkbox' && isset( $array['required'] ) && $array['required'] == 1 && !isset( $args[$key] ) ) {

    and add } after the following lines:

    	         UM()->form()->add_error('role', __('Please specify account 
                     type.','ultimate-member') );
    	   }
         }

    Finally change the line
    if ( isset( $array['required'] ) && $array['required'] == 1 ) {
    to
    if ( isset( $array['required'] ) && $array['required'] == 1 && $field_not_required_when_hidden_by_cond_logic == false) {

    Make sure to save a copy of this file somewhere in case the plugin gets updated.

    • This reply was modified 6 years, 2 months ago by lumtiffany.

    Hello @lumtiffany

    I tested your code on several conditions but it’s not working.
    Here you have my field configuration :

    Profession Field
    — If profession 1,2,3 and 4 then show Hide Field 1
    — If profession 1,2 and 3 then show Hide Field 2

    Thanks

    Hi @spoozmy,

    What type is your profession field (radio button, checkboxes, textfield, etc.)?

    Also are these set as separate conditions or one single condition? ie. you have 4 different conditions for just your “If profession 1,2,3 and 4 then show Hide Field 1”.

    Also, is it supposed to show or hide? It can’t be both.

    Sorry for all the questions; just trying to figure out what your conditions are so I can recreate this problem and try to solve it.

    Thanks @lumtiffany

    The professional field is a dropdown field, in which there are 7 distinct professions.
    The condition is configured to display the hidden field.

    So,
    — if profession A, B, C and D then show Hide Field 1.
    — if profession E, F and G then show Hide Field 2.
    The hidden field 1 and 2 are textboxes that corresponds to a specific business code that the user must complete to create an account.

    Of course, all information is required.

    Thank you very much for your help.

    @spoozmy

    Might have solved it for your type of fields. Let me know if it works or not for you.

    Discard the previous changes I had made (the fastest way is to get the version from the plugin download).

    You’ll need to edit the um-actions-form.php in /plugins/ultimate-member/includes/core/

    1. Comment out the following line by adding // in front of it. This was the main cause of the problems as it turned off ‘required’ for a lot of the fields.
    $array = apply_filters( 'um_get_custom_field_array', $array, $fields );
    should become
    //$array = apply_filters( 'um_get_custom_field_array', $array, $fields );

    2. Create a variable to check if the parent field’s value meets the conditional logic (ie. Profession is A,B,C,D,E,F,G or empty). We will stop checking the other conditional logic if one of the requirements are met.

    if ( ! empty( $array['conditions'] ) ) {
    	$match_found_parent_value = false;
    	foreach ( $array['conditions'] as $condition ) {
    		if(!$match_found_parent_value){ // no match found yet

    3. Change the statements under visibility == ‘show’. Make sure to do this for all $op== ”. We set the variable to true when the current value of the parent (ie. Profession) matches one of the conditions we’re interested in. All the other times, it’s set to false.

    if ( $op == 'empty' ) {
    	if ( ! empty( $cond_value ) ) {
    		$match_found_parent_value = false;
    		continue ;
    	}
    	else{
    		$match_found_parent_value = true;
    		continue ;
    	}
    }

    4. We need to use the variable to ensure we’re sending out error messages only when the dependent field is shown. Add the following line
    && (empty($array['conditions']) || $match_found_parent_value)
    to each isset( $array['required'] ) && $array['required'] == 1

    Assuming that there is one parent field and one child field:
    This should work for :
    Parent Child/Dependent Operator Number of Conds.
    Textbox Textbox Equals To 1
    Textbox Number Equals To 1
    Textbox Radio Equals To 1
    Textbox Multiselect Equals To 1
    Textbox Textarea Equals To 1
    Dropdown Textbox Equals To 1
    Radio Textbox Equals To 1

    It currently doesn’t work for the following:
    Parent Child/Dependent Operator
    Multiselect Textbox Equals To 1

    Any one with Multiple Options as the parent as it’s difficult to interpret. For example, if I have a textbox that shows only when multiselect’s value is not equal to ‘red’, the textbox would still show up when the multiselect has other values such as ‘red’ and ‘blue’. This may work as intended for some, but not work for others’ needs. The conditional logic statements as they are now only allow them to be used as independent events for multiselect/checkbox parent fields.

    If anyone wishes to help out, let me know which conditional logics work/don’t work for you. Please send it in the format as shown above. I just got lazy to continue.

    • This reply was modified 6 years, 2 months ago by lumtiffany.

    Dear @lumtiffany,

    I applied your suggestion, but I made a mistake somewhere.

    The first step :

    //$array = apply_filters( 'um_get_custom_field_array', $array, $fields );
    
    			if ( ! empty( $array['conditions'] ) ) {
    				$match_found_parent_value = false;
    				foreach ( $array['conditions'] as $condition ) {
    					if(!$match_found_parent_value){ // no match found yet
    					list( $visibility, $parent_key, $op, $parent_value ) = $condition;
    
    					if ( ! isset( $args[ $parent_key ] ) ) {
    						continue;
    					}

    The second step :

    } elseif ( $visibility == 'show' ) {
    						if ( $op == 'empty' ) {
    							if ( ! empty( $cond_value ) ) {
    								$match_found_parent_value = false;
    								continue 2;
    							}
    							else{
    								$match_found_parent_value = true;
    								continue ;
    							}
    						} elseif ( $op == 'not empty' ) {
    							if ( empty( $cond_value ) ) {
    								$match_found_parent_value = false;
    								continue 2;
    							}
    							else{
    								$match_found_parent_value = true;
    								continue ;
    							}
    						} elseif ( $op == 'equals to' ) {
    							if ( $cond_value != $parent_value ) {
    								$match_found_parent_value = false;
    								continue 2;
    							}
    							else{
    								$match_found_parent_value = true;
    								continue ;
    							}
    						} elseif ( $op == 'not equals' ) {
    							if ( $cond_value == $parent_value ) {
    								$match_found_parent_value = false;
    								continue 2;
    							}
    							else{
    								$match_found_parent_value = true;
    								continue ;
    							}
    						} elseif ( $op == 'greater than' ) {
    							if ( $cond_value <= $parent_value ) {
    								$match_found_parent_value = false;
    								continue 2;
    							}
    							else{
    								$match_found_parent_value = true;
    								continue ;
    							}
    						} elseif ( $op == 'less than' ) {
    							if ( $cond_value >= $parent_value ) {
    								$match_found_parent_value = false;
    								continue 2;
    							}
    							else{
    								$match_found_parent_value = true;
    								continue ;
    							}
    						} elseif ( $op == 'contains' ) {
    							if ( ! strstr( $cond_value, $parent_value ) ) {
    								$match_found_parent_value = false;
    								continue 2;
    							}
    							else{
    								$match_found_parent_value = true;
    								continue ;
    							}
    						}
    					}

    And the last step :

    if ( isset( $array['type'] ) && $array['type'] == 'checkbox' && isset( $array['required'] ) && $array['required'] == 1 && (empty($array['conditions']) || $match_found_parent_value) && !isset( $args[$key] ) ) {
    
    if ( isset( $array['type'] ) && $array['type'] == 'radio' && isset( $array['required'] ) && $array['required'] == 1 && (empty($array['conditions']) || $match_found_parent_value) && !isset( $args[$key] ) && !in_array($key, array('role_radio','role_select') ) ) {
    
    if ( isset( $array['type'] ) && $array['type'] == 'multiselect' && isset( $array['required'] ) && $array['required'] == 1 && (empty($array['conditions']) || $match_found_parent_value) && !isset( $args[$key] ) && !in_array($key, array('role_radio','role_select') ) ) {
    
    if ( isset( $array['required'] ) && $array['required'] == 1 && (empty($array['conditions']) || $match_found_parent_value) && ( !isset( $args['role'] ) || empty( $args['role'] ) ) ) {

    And for UM Hook :

    do_action( 'um_add_error_on_form_submit_validation', $array, $key, $args );
    
    			if ( isset( $args[$key] ) ) {
    
    				if ( isset( $array['required'] ) && $array['required'] == 1 && (empty($array['conditions']) || $match_found_parent_value) ) {

    When I add the modified file, I make a 500 error. I think I should miss a tag somewhere.

    Thanks for your help.

    SpoOzmy

    @spoozmy

    Some things to fix/add:

    1. Add a } above if ( isset( $array['type'] ) && $array['type'] == 'checkbox' ... line. In other words there should be a total of 6 } between the last instance of continue; and if ( isset

    2. Make sure to change the continue 2; to continue;. This is for all the continues found in the ‘show’ visibility. Don’t edit anything in the ‘hide’ section.

    3. You should take out the && (empty($array['conditions']) || $match_found_parent_value) from the roles validation. Roles are validated differently.

    All the other changes look fine. Tell me if your code starts working or not, and if it solved your problem with the Profession field

    Dear @lumtiffany,

    Your solution work, it’s good for me.
    The error message is active when the hidden field is not filled.

    thank you very much for your help.

    SpoOzmy

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How can I have required fields that are hidden by conditional logic?’ is closed to new replies.