1

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'
      ])
      // ...
}
Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
  • 1
    What does your code look like? – Jim Wright Aug 02 '17 at 17:04
  • Sorry - I missed that :) – Alex.Barylski Aug 02 '17 at 17:37
  • If I set "data" explicitly then that is what always shows in the form essentially over writing the entity value. I am not understanding why the default value of the entity isn't used when the entity is constructed - and the same for when the entity is loaded from a DB – Alex.Barylski Aug 02 '17 at 17:43
  • Do you have access to the entity in `buildForm`? – Jim Wright Aug 02 '17 at 17:45
  • 1
    I do yes - I just realized my error though :o my bad. I have a form embed into another form and two entities for each. The entity I was instantiating was the outer most entity but the form I was testing was bound to the inner most entity. The entity literally did not exist until I submit the form. The work around was to create and set the inner most entity before binding the parent entity to the form. Sorry about this :) – Alex.Barylski Aug 02 '17 at 17:48
  • If you haven't solved it, this answer seems to be what you're looking for? https://stackoverflow.com/a/15529083/4878850 – Jim Wright Aug 02 '17 at 17:50

2 Answers2

2

http://symfony.com/doc/current/components/form.html#setting-default-values

If you need your form to load with some default values (or you're building an "edit" form), simply pass in the default data when creating your form builder.

$quoteItem = new QuoteItem();
$quoteItem->getQuoteFlight()->setSpecMajorSetupCharge(QuoteFlight::SPEC_MAJOR_SETUP_CHARGE).

$form = $this->createForm(QuoteItemType::class, $quoteItem);
// ...

Using data option is no good, because:

http://symfony.com/doc/current/reference/forms/types/form.html#data

The data option always overrides the value taken from the domain data (object) when rendering. This means the object value is also overriden when the form edits an already persisted object, causing it to lose it's persisted value when the form is submitted.

So the recomendation is to set explicitly the data in the underlined object on initialization, either in __constructor() or before bind the object to the form.

Community
  • 1
  • 1
yceruto
  • 9,230
  • 5
  • 38
  • 65
0

To answer my own question and avoid confusion for anyone in the future:

$quoteItem = new QuoteItem();

// THIS LINE WAS MISSING
$quoteItem->setQuoteFlight(new QuoteFlight()); 

$form = $this->createForm('UniflyteBundle\Form\QuoteItemType', $quoteItem, ['allow_extra_fields' => true]);
$form->add('QuoteFlight', QuoteFlightType::class);

Without the added line the QuoteFlight entity was NULL when the form rendered during create.

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68