Symfony 2:多步骤表单与CraueFormFlowBundle和FOSUserBundle



对于我的应用程序,我必须创建一个多步骤的表单来注册新用户,所以我已经安装了CraueFormFlowBundle,我也使用FOSUserBundle。

对于注册表单的第一步,我想用FOSUserBundle注册用户的信息(登录名,密码…),然后用户将被重定向到表单的下一步。

所以我嵌入了一个表单类型的集合在单一类型称为"RegistrationEtablissementForm"。

但是当我尝试渲染我的多步骤表单的视图时,我有这个错误:

警告:缺少参数1安全系数 UserBundle 类型 RegistrationFormType形式::__construct()调用在C: wamp www likabee_3 src AppBundle RegistrationEtablissementForm.php形式在第14行定义了

类型RegistrationEtablissementForm:

namespace AppBundleForm;

// AppBundle/Form/RegistrationEtablissementForm.php
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
class RegistrationEtablissementForm extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        switch ($options['flow_step']) {
            case 1:
                $builder->add('registration', array('type' => new RegistrationFormType()));
                break;
            case 2:
                $builder->add('etablissement', array('type' => new EtablissementType()));
                break;
            case 3:
                $builder->add('chambres', 'collection', array(
                            'type'         => new ChambreType(),
                            'allow_add'    => true,
                            'allow_delete' => true
                          ));
                break;
        }
    }
    public function getName() {
        return 'registrationEtablissement';
    }
}

第一种形式是:

namespace AppBundleForm;
use SymfonyComponentFormFormBuilderInterface;
use FOSUserBundleFormTypeRegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        // add your custom field
        $builder
            ->add('nom', 'text')
            ->add('prenom', 'text')
            ->add('telephonePortable', 'text')
            ->add('telephoneFixe', 'text', array('required' => false))
            ->add('fax', 'text', array('required' => false))
            ->add('Civilite', 'text')
        ;
    }
    public function getName()
    {
        return 'homes_user_registration';
    }
}

第二个:

class EtablissementType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nombre_place_total','choice', array('choices' => array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11', '12' => '12', '13' => '13', '14' => '14', '15' => '15')))
            ->add('tarif_min', 'text')
            ->add('tarif_max', 'text')
            ->add('description', 'textarea')
            ->add('nom_etablissement', 'text')
        ;
    }
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundleEntityEtablissement'
        ));
    }
    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_etablissement';
    }
}

和第三个:

namespace AppBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
class ChambreType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom_chambre', 'text')
            ->add('nombre_place','choice', array('choices' => array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10')))
            ->add('nombre_lit','choice', array('choices' => array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6')))
            ->add('superficie', 'text')
            ->add('tarif_1', 'text')
            ->add('tarif_2', 'text')
            ->add('tarif_3', 'text')
            ->add('tarif_4', 'text')
            ->add('tarif_5', 'text')
            ->add('tarif_6', 'text')
            //->add('etablissement')
        ;
    }
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundleEntityChambre'
        ));
    }
    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_chambre';
    }
}
在我的服务。在FOSUserBundle和multi-steps-form中启用了service:
services:
#    service_name:
#        class: AppBundleDirectoryClassName
#        arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
    homes_user.registration.form.type:
        class: AppBundleFormRegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: homes_user_registration }
    likabee.form.registrationEtablissement:
        class: AppBundleFormRegistrationEtablissementForm
        tags:
            - { name: form.type, alias: registrationEtablissement }
    likabee.form.flow.registrationEtablissement:
        class: AppBundleFormRegistrationEtablissementFlow
        parent: craue.form.flow
        scope: request
        calls:
            - [ setFormType, [ "@likabee.form.registrationEtablissement" ] ]

在我的控制器中,我创建了这个函数,用于在视图中呈现表单:

/**
     * @Route("/registration-etablissement", name="registrationEtablissement")
     */
    public function registrtationEtablissementAction() {
        $formData = new RegistrationEtablissementForm(); // Your form data class. Has to be an object, won't work properly with an array.
        $flow = $this->get('likabee.form.flow.registrationEtablissement'); // must match the flow's service id
        $flow->bind($formData);
        // form of the current step
        $form = $flow->createForm();
        if ($flow->isValid($form))
        {
            $flow->saveCurrentStepData($form);
            if ($flow->nextStep()) {
                // form for the next step
                $form = $flow->createForm();
            } else {
                // flow finished
                $em = $this->getDoctrine()->getManager();
                $em->persist($formData);
                $em->flush();
                $flow->reset(); // remove step data from the session
                return $this->redirect($this->generateUrl('index')); // redirect when done
            }
        }
        return $this->render('registrationEtablissement.html.twig', array(
                'form' => $form->createView(),
                'flow' => $flow,
        ));
    }

我找了几天解决办法,但我没有找到…

谢谢你的帮助,对不起我的英语;)

在您的类RegistrationEtablissementForm第14行,您正在调用FOSUserBundleFormTypeRegistrationFormType构造函数。如果你检查FOSUserBundle中该类的代码,你可以看到构造函数需要用户类名作为属性:

class RegistrationFormType extends AbstractType
{
    private $class;
    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;
    }
    // ...
}

在调用构造函数时添加用户类应该可以解决这个问题。

相关内容

  • 没有找到相关文章

最新更新