2

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 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

Thanks Michi, maybe i missexplained or you didn't understand the question correctly. I'm trying to bind an object to an inner/embedded form in order to let Symfony2 populate the form at template printing time.
@user846226 I think I don't really understand... could you explain exactly what you're trying to achieve... you might be talking about collections (symfony.com/doc/2.1/cookbook/form/form_collections.html).
Hi Michi, no i'm not talking about collections. I've created a parent form type which embeds other child form types, as i want Symfony2 to populate those nested forms with data i'm looking for a way to pass the data to child forms, which right now i'm doing via constructor. Once the child gets the object to populate the form with i need to make some kind of call to get the form populated. Right now i'm trying to accomplish it via closure symfony.com/doc/current/cookbook/form/use_empty_data.html
0

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) }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.