无法从Controller

时间:2016-01-19 16:06:26

标签: php forms symfony

我试图将一个网址分配给"动作"来自Controller的表单generateUrl

RegistrationController.php

$form = $this->createForm('app_user_registration', 
                                    $entity, 
                                    array('action' => $this->generateUrl('fos_user_registration_register', 
                                            array('type' => 'agent')),
                                            'validation_groups' => 'Registration'));

路由/ registration.yml

fos_user_registration_register:
    pattern: /{type}
    defaults : { _controller :FOSUserBundle:Registration:register }
    requirements:
        _method: GET|POST
        type : agent|controleur|superviseur

services.yml

# app/config/services.yml

services:
    bissap.form.registration:
        class: BISSAP\UserBundle\Form\Type\RegistrationFormType
        arguments: [ %fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: bissap_user_registration }

    app.form.registration:
        class: BISSAP\UserBundle\Form\RegistrationType
        tags:
            - { name: form.type, alias: app_user_registration }

RegistrationFormType.php

<?php

namespace BISSAP\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegistrationFormType extends AbstractType
{

    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;

    }


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email', array('required' => false,'validation_groups' => array('registration'), 
                        'label' => 'form.email',
                        'translation_domain' => 'FOSUserBundle',
                        'attr' => array('class' => 'input__registerbox')))
            ->setMethod('POST')
            ->add('username', null, array('required' => false, 'validation_groups' => array('registration'),
                        'label' => 'form.username',
                        'translation_domain' => 'FOSUserBundle',
                        'attr' => array('class' => 'input__registerbox')))
            ->add('plainPassword', 'repeated', array(
        'required' => false,
        'validation_groups' => array('registration'),
                'type' => 'password',
                'options' => array('translation_domain' => 'FOSUserBundle'),
                'first_options' => array('label' => 'form.password', 'attr' => array('class' => 'input__registerbox')),
                'second_options' => array('label' => 'form.password_confirmation', 'attr' => array('class' => 'input__registerbox')),
                'invalid_message' => 'fos_user.password.mismatch',
            ))
            ->add('Enregistrer', 'submit', array(
                'attr' => array(
                'class' => 'registerbox__submit bc-btn')))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

    // BC for SF < 2.7
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $this->configureOptions($resolver);
    }

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

RegitrationType.php

<?php
// src/UserBundle/Form/RegistrationType.php

namespace BISSAP\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name','text',array('required' => false,'validation_groups' => array('Registration'), 
                        'attr' => array('class' => 'input__registerbox')));
        $builder->add('firstname','text',array('required' => false,'validation_groups' => array('Registration'), 
                        'attr' => array('class' => 'input__registerbox')));
        //$builder->setAction($this->router->generate('fos_user_registration_register', array('type' => 'agent')));

    }

    public function configureOptions(OptionsResolver $resolver) 
    { 
        $resolver->setDefaults(array( 'data_class' => $this->class, 'intention' => 'Registration', 'validation_groups' => array('Registration'))); 
    }

    public function getParent()
    {
        return 'bissap_user_registration';
    }

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

但是我收到了这个错误:

Attempted to call method "generateUrl" on class "BISSAP\UserBundle\Form\Type\RegistrationFormType". 

那么我在哪里以及如何使用generateUrl将URL分配给操作?

0 个答案:

没有答案