How do I specify default values in a Symfony form

随声附和 提交于 2019-12-11 15:18:56

问题


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'
      ])
      // ...
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/45466928/how-do-i-specify-default-values-in-a-symfony-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!