简体   繁体   中英

Form configureOptions() setting data class

Assume the following code:

class MyInventory extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('uom')
            ->add('min')
            ->add('max')
            ->add('price')
            ->add('number')
            ->add('description')
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyCompany\MyBundle\Entity\Inventory'
        ));
    }
}

What is the purpose of setting the "data_class" to a string namespace/classname of the entity in question? Why not instantiate the entity and inject it? I assume the form class now magically does this for you - but could the same thing be achieved (albeit a bit clearer or according to Symfony spec) through services and DiC?

Just curious???

My guess is Symfony Forms will load class metadata, so it can perform validation and data copying later on. It doesn't really need an instance - just the metadata.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM