How can i know if the field has been filled?

I’m trying to require some fields only if one of these have been filled but i can’t figure out how to get the value of the field.

add_filter( 'gform_pre_render', 'gw_conditional_requirement' );
add_filter( 'gform_pre_validation', 'gw_conditional_requirement' );
function gw_conditional_requirement( $form ) {
	$var = False;
	foreach($form['fields'] as &$field){
		
		if( ( ( $field['id'] >= 78 && $field['id'] <= 100 ) || $field['id'] == 253)){
			if(rgpost($field->adminLabel) != ''){
            	$var = True;
			}
        }
	}
	foreach($form['fields'] as &$field){
		
		if( ( ( $field['id'] >= 78 && $field['id'] <= 100 ) || $field['id'] == 253)){
			if($var == True){
            	$field['isRequired'] = true;
			}
        }
	}

    return $form;
}

Hi Ángel, it seems that you’re trying to modify the example from our documentation to fit your needs gform_pre_validation - Gravity Forms Documentation

On the example you can see how to get the value for a field:

$value = rgpost( 'input_2' );

The above would get the value for a single input field with an id of 2 (e.g. a single line text field or number field).

So using that example, if you want to know if the field was filled, you can simply check if the field is not empty using PHP’s is_empty() function. Example:

if ( ! empty( rgpost( 'input_2' ) ){
	// Field is not empty.
}
1 Like

Hi, sorry for the delay. Thanks for the answer!
In my example i use rgpost($field->adminLabel) to get the name of the field, is this correct or should i use other parameter?

The function rgpost() is only to get field values from the POST request.

$field->adminLabel has no relation with the field value, that’s for getting the Field Admin Label (not the regular field label, that would be $field->label ). If you really want to check that, then you would use $field->adminLabel without the rg_post() function, example:

if ( ! empty( $field->adminLabel  ){
	// Field Admin Label is not empty.
}

You can find all the properties for the $field object explained here: Field Object - Gravity Forms Documentation

1 Like