简体   繁体   中英

Symfony2 and FormBuilder: How to get the elemetns number added in the builder

I have a formbuilder where I am adding some values from an entity:

$builder->add('affiliation', 'entity', array(
    'class' => 'SciForumVersion2Bundle:UserAffiliation',
    'multiple' => true,
    'expanded' => true,
    'query_builder' => function(EntityRepository $er) use ($author,$user) {
     return $er->createQueryBuilder('ua')
        ->where("ua.user_id = {$user->getId()}")
        ->andWhere("ua.affiliation_id not in ( select pa.affiliation_id FROM SciForumVersion2Bundle:PersonAffiliation pa where pa.person_id = {$author->getPersonId()} )");
     },
     'required'  => true,
));

In my controller, I would like to check if there is something in my form. If there is something, I will display one view, if there is nothing, I will display another view.

Is this possible and if so, how?

Thank you.

Simply try it:

$data = $form->getData()

function getData() documentation Book

If you want to get the current data (just after rendering the form) in your form type you can use the builder supplied in each form type by standard.

It works exactly as a normal form response, so you can use:

$builder->getData();

and use if clauses to add different fields depending on what you want to generate.

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