简体   繁体   中英

Symfony2 FOSUserBundle - additional form

I want to add an extra step to register. I have my own FormType "Firma" that tries to get in my additional method in registerController. When calling this action in controller gets an error:

Fatal error: Call to undefined method My\FrontendBundle\Form\Type\FirmaType::createView() in /var/www/Budowlanka/src/My/FrontendBundle/Controller/RegistrationController.php on line 107

public function nextStepAction($token) {

    $user = $this->container->get('fos_user.user_manager')->findUserByConfirmationToken($token);

    if (null === $user) {
        throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
    }

    $this->container->get('fos_user.user_manager')->updateUser($user);

    $form = $this->container->get('my_user.firma.form.type');
    .
    . 
    .
    return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.' . $this->getEngine(), array(
                'form' => $form->createView(),
                'theme' => $this->container->getParameter('fos_user.template.theme'),
    ));

<service id="my_user.firma.form.type" class="My\FrontendBundle\Form\Type\FirmaType">
    <tag name="form.type" alias="my_user_firma" />
    <argument>My\FrontendBundle\Entity\Firma</argument>
</service>

FirmaType:

namespace My\FrontendBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class FirmaType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('nazwa')
        ->add('ulica')
        ->add('nr_budynku')
        ->add('nr_lokalu')
        ->add('miasto')
        ->add('kod_pocztowy')
        ->add('poczta')
        ->add('telefon_1')
        ->add('telefon_2')
        ->add('email')
        ->add('www')
        ->add('liczba_opinii')
        ->add('nip')
        ->add('imie')
        ->add('nazwisko')
        ->add('haslo')
        ->add('logo')
        ->add('opis_uslug')
        ->add('data_dodania')
        ->add('data_modyfikacji')
        ->add('slug')
        ->add('specjalizacje')
    ;
}

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

EDIT:

It works great but now I have a problem with labels from manytomany relationship field.

          ->add('specjalizacja', 'entity', array(
                'label' => 'Specjalizacje',
                'multiple' => true, 
                'expanded' => true, 
                'property' => 'nazwa', 
                'class' => 'My\FrontendBundle\Entity\Specjalizacja',
                'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
                    $qb = $er->createQueryBuilder('g');

                    return $qb->orderBy('g.nazwa', 'DESC');
                }
            )

It displays 'my_user_firma_form_specjalizacja_110' label (110 is the id of record) instead Nazwa field of Entity. I have a __toString() method in Specjalizacja Entity class

You should use the formFactory to create your form.

<service id="my_user.firma.form" factory-method="createNamed" factory-service="form.factory" class="Symfony\Component\Form\Form">
    <argument>my_user_firma</argument>
    <argument>'my_user_firma_form'</argument>
</service>

<service id="my_user.firma.form.type" class="My\FrontendBundle\Form\Type\FirmaType">
    <tag name="form.type" alias="my_user_firma" />
    <argument>My\FrontendBundle\Entity\Firma</argument>
</service>

And then call..

$form = $this->container->get('my_user.firma.form');

From here you should be able to use $form->createView() .

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