Is it possible to control the “Required” status of 2 fields based on whether a file has been added to a “file upload field”? I see the instructions for use of validation to control conditionally required fields, but I’m not very adept w/PHP nor GF, so if this is possible, please also include the code in the answer.
The form # is 106. The file upload field is 33, and the 2 fields I want to control are fields 8 and 9.
Thanks much!
You can completely replace fields with the desired type using:
If you are comfortable with jQuery, it makes it easy to .replaceWith the new data.
While I can add code to my WP files, I’m don’t write PHP nor JQ. Could you provide the code based on the form field #s in the original post?
Thanks.
Unfortunately, can’t do everything for you. But, I just tried to slap it together and it didn’t work like I thought. This jQuery removes all markers in the HTML field, but the form will still throw back an error so keep playing with it and see if you can get it to work:
jQuery(document).ready(function($) {
$( document ).on( 'mousemove', function(){
$( '#field_42_12' ).replaceWith( '<li id="field_42_12" class="gfield field_sublabel_below field_description_below gfield_visibility_visible"><label class="gfield_label" for="input_42_12">Untitled</label><div class="ginput_container ginput_container_text"><input name="input_12" id="input_42_12" type="text" value="" class="medium" aria-invalid="false"></div></li>' );
});
});
This PHP is incomplete, but you can play with it and see if it will get you where you where you want to be.
add_filter( 'gform_field_input_42_12', 'map_input_42_12', 10, 5 );
function map_input_42_12( $input, $field, $value, $lead_id, $form_id ) {
if ( $form_id == 42 && $field->id == 12 ) {
$user_id = get_current_user_id();
$user_data = get_userdata($user_id);
$capabilities = wp_capabilities;
$get_role = get_user_meta( $user_id, $capabilities, true );
$user_role = key($get_role);
if ( $user_role == 'administrator' ) {
$input = '';
}
return $input;
}
Otherwise, just make two fields (one required, one not), and then add a conditional statement to hide one or the other due to what you are wanting.
Thanks for the effort! I don’t have the skills to play around with the code.
A hidden field that is required will still prevent the form from being submitted.
Also, the test in this case is to see if a file has been added to an upload field, doubling the challenge
Once I get the itch… I can’t help but scratch. Here is the answer you want. Replace 42 with your Form_id and 12 with your Field_id:
add_filter( 'gform_pre_render_42', 'validation_42' );
add_filter( 'gform_pre_validation_42', 'validation_42' );
function validation_42( $form ) {
$user_id = get_current_user_id();
$user_data = get_userdata($user_id);
$capabilities = wp_capabilities;
$get_role = get_user_meta( $user_id, $capabilities, true );
$user_role = key($get_role);
if ( $user_role == 'administrator' ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 12 ) {
$field->isRequired = false;
}
}
}
return $form;
}
I don’t think that’s quite done.
The form # is 106. Field 2 is a file upload field.
Fields 8 & 9 are required by default, but if there is an upload in Field 2, I want to make them not required.
In psuedo code, this is what has to happen:
If (field 2 has an upload attached) {
[$field8 and $field9]->isRequired = false;
}
To function like that, you needs it to be an AJAX. You might need a programmer to hack it for you. The start of the road looks something like this in your page header:
jQuery(document).ready(function($) {
var data = {
'action': 'validation_42',
'dataType': 'json',
'type' : 'json'
};
var json = jQuery.post(ajaxurl, data , function(response) {
}).done(function(boom){
//STUFF
}
});
Even then, there is so much that it might go wrong… Honestly, start simple, learn, and improve as you go.