adb
adb

Reputation: 153

Gravity Forms How to set the value of a simple text field at page render

I want to set the value of a simple text field based on the state of another field in a previous page of a form. I am selecting a hosting provider of 'aws' and later in the form, I am asking for the user name.

For aws hosting, I want to force ec2-user to be shown in the text box, so I am using the filter of

add_filter( 'gform_pre_render_4', 'populate_deployment_user' );
function populate_deployment_user( $form )
{
    $hosting_provider = rgpost('input_23');
    if (strcmp($hosting_provider, "aws") == 0)
    {
        foreach ( $form['fields'] as &$field ) {
            if ( $field->id == 42) {
                $field->text = "ec2-user";
            }
        }
    }
}

but the $field->text is not correct. I can't use the gform_field_value_$field_name as that's only called at the start of the form, and not after my other field 23 has been selected.

I'm a newbie in forms, JS and PHP, so floundering somewhat, although I've tried for a couple of days to get a solution.

Upvotes: 2

Views: 1947

Answers (1)

adb
adb

Reputation: 153

Solution is to set

$field->defaultValue = "ec2-user";

Upvotes: 3

Related Questions