Mike Thrussell
Mike Thrussell

Reputation: 4515

Adding array key dynamically PHP

I'm creating a dynamic form in Silex that alters depending on need.

If $disabled = 'true'

How would I change:

$form = $app['form.factory']->createBuilder('form')

->add('email', 'email', array(
    'data' => $from
))

to

$form = $app['form.factory']->createBuilder('form')

->add('email', 'email', array(
    'disabled' => true,
    'data' => $from
))

Upvotes: 3

Views: 65

Answers (1)

CharliePrynn
CharliePrynn

Reputation: 3080

You could accomplish it like this:

$form = $app['form.factory']->createBuilder('form');

$options = array(
    'data' => $from
);

if($disabled == 'true'){
    $options['disabled'] = true;
}

$form->add('email', 'email', $options)

Upvotes: 2

Related Questions