2
votes

i have the following form where i would like to pass some objects to the inner forms in order to populate them with data when being edited:

        public function __construct( $em, $id )
        {
            $this->_em = $em;
        }

        public function buildForm( \Symfony\Component\Form\FormBuilderInterface $builder, array $options )
        {        
            $builder->add( 'accessInfo', new AccessInfoType( $this->_em, $options[ 'entities' ][ 'user' ] ) , array(
                                                                'attr'  => array( 'class' => 'input-medium' ),
                                                                'required'      => false,
                                                                'label'         => false
                                                             )
            );
            $builder->add( 'profileInfo', new ProfileInfoType( $this->_em, $options[ 'entities' ][ 'profile' ] ) , array(
                                                                'required'      => false,
                                                                'label'         => false
                                                             )
            );
        }

        public function setDefaultOptions( \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver )
        {
            $resolver->setDefaults( $this->getDefaultOptions( array() ) );
            return $resolver->setDefaults( array( ) );
        }

        /**
         * {@inheritDoc}
         */
        public function getDefaultOptions( array $options )
        {
            $options = parent::getDefaultOptions( $options );
            $options[ 'entities' ]   = array();

            return $options;
        }

        public function getName()
        {
            return 'UserType';
        }

which i instantiate with the following code:

$form = $this->createForm( new UserType( $em ), null, array( 'entities' => array( 'user' => $userObj, 'profile' => $profileObj ) ) );  

Once i get, via the constructor, the object containing the needed data does anyone know how could i bind that object to the form?

class ProfileInfoType extends AbstractType
{
    private $_em;

    public function __construct( $em, $dataObj )
    {
        $this->_em = $em;
        $this->_dataObj = $dataObj;
    }

Thanks in advanced!

3

3 Answers

1
votes

I was having the same issue and fixed this with inherit_data

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'inherit_data' => true,
    ));
}

See also http://symfony.com/doc/current/cookbook/form/inherit_data_option.html

0
votes

Inside your controller yo should get the request data

$request = $this->getRequest();

or request it through the method parameters

public function newAction(Request $request)

and then bind it to the form

$form->bind($request);

For further details have a look at http://symfony.com/doc/2.1/book/forms.html#handling-form-submissions

0
votes

this works well add an attr for use the html attribute 'value' depends of the form type, maybe this can help you.

Twig

{{ form_label(blogpostform.title) }}
{{ form_widget(blogpostform.title, {'attr': {'value': titleView }}) }}
{{ form_errors(blogpostform.title) }}