Search code examples
arrayssymfonyoopif-statementformbuilder

How can I use an if statement inside an array?


I try to make an if-statement inside an array:

  $formBuilder->add('getTimestamp', DateType::class, array(
              'widget' => 'single_text',
               if($target == "new"){
                  'data' => new \DateTime(),
                }
                'attr' => array('class' => 'form-control', 'style' => 'line-height: 20px;'), 'label' => $field['fieldName'],
               ));

But I get an error message

(1/1) ParseError

syntax error, unexpected 'if' (T_IF), expecting ')'


Solution

  • 'data' => $target == 'new' ? new \DateTime() : ''