0

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 ')'

1

2 Answers 2

1
'data' => $target == 'new' ? new \DateTime() : ''
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is the if inside your array. This is not valid php syntax. You could move this outside.

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

$formBuilder->add('getTimestamp', DateType::class, $options);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.