I am trying to specify a default value in a form so when creating an entity the form field has a value (not null or empty). However when the entity is being edited it should obviously show the stored value and not the default.
My entity initializes itself as part of the construction - so when an entity is new and not yet persisted these values should be set.
How do I inform the FormType to use the default value over the persisted state? Everything I try seems to suggest it's one or the other not both?
How is this done Symfony 3.2+???
EDIT |
controller:
public function newAction (Request $request)
{
$quoteItem = new QuoteItem();
$form = $this->createForm('UniflyteBundle\Form\QuoteItemType', $quoteItem, ['allow_extra_fields' => true]);
$form->add('QuoteFlight', QuoteFlightType::class);
}
Form type:
public function configureOptions (OptionsResolver $resolver)
{
$resolver->setDefaults([
//'data' => new \UniflyteBundle\Entity\QuoteFlight()
'data_class' => QuoteFlight::class
]);
}
public function buildForm (FormBuilderInterface $builder, array $options)
{
$builder
->add('specMajorSetupCharge', null, [
//'empty_data' => QuoteFlight::SPEC_MAJOR_SETUP_CHARGE,
'data' => QuoteFlight::SPEC_MAJOR_SETUP_CHARGE,
'label' => '* Setups Charge'
])
// ...
}