Symfony2 setDefaultOptions(OptionsResolverInterface $ resolver)

时间:2016-06-01 20:33:04

标签: forms symfony

我在Symfony2中很新。我正在尝试开发一个有人可以注册/登录的网站。所以我试图创建注册表单,但它总是与setDefaultOptions(OptionsResolverInterface $ resolver)方法有关。

RegisterType.php:

<?php

namespace MyappBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegisterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder
            ->add('name', TextType::class, array(
                    'label' => 'Your Name',
                    'label_attr' => array(
                        'attr' => array(
                            'class' => 'control-label'
                        )
                    ),
                    'attr' => array(
                        'class' => 'form-control'
                    )
                ))
            ->add('email', EmailType::class, array(
                    'label' => 'Your Email',
                    'label_attr' => array(
                        'attr' => array(
                            'class' => 'control-label'
                        )
                    ),
                    'attr' => array(
                        'class' => 'form-control'
                    )
                ))
            ->add('password',RepeatedType::class, array(
                    'type' => PasswordType::class, array(
                        'first_options'  => array(
                            'label' => 'Your Password', array(
                                'label_attr' => array(
                                    'attr' =>array(
                                        'class'=> 'control-label'
                                    )
                                )
                            ),
                            'attr' => array(
                                'class' => 'form-control'
                            )
                        ),
                        'second_options' => array(
                            'label' => 'Repeat Password', array(
                                'label_attr' => array(
                                    'attr' =>array(
                                        'class'=> 'control-label'
                                    )
                                )
                            ),
                            'attr' => array(
                                'class' => 'form-control'
                            )
                        )


                )))
            ->add('tel', NumberType::class, array(
                    'label' => 'Your Telephone number',
                    'label_attr' => array(
                        'attr' => array(
                            'class' => 'control-label'
                        )
                    ),
                    'attr' => array(
                        'class' => 'form-control'
                    )
                ))
            ->add('register', 'submit', array(
                    'attr' => array(
                        'class' => 'btn btn-default'
                    )
                ));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(['data_class' => 'MyappBundle\Entity\Users']);
    }

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

RegisterControler.php

<?php

namespace MyappBundle\Controller;

use MyappBundle\Form\Type\RegisterType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use MyappBundle\Entity\Users;
use Symfony\Component\HttpFoundation\Request;

class RegisterController extends Controller
{
    public function indexAction(Request $request)
    {
        $user = new Users();

        $form = $this->createForm(new RegisterType($user), array(
                'action' => $this->generateUrl('myapp_register'),
                'method' => 'POST'
            ));


        if($form->isSubmitted() && $form->isValid()){
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();

            return $this->redirect($this->generateUrl('myapp_homepage'));
        }

        return $this->render('MyappBundle:Default:register.html.twig', array('form' => $form->createView(),));
    }
}

我得到的错误是:

CRITICAL - Uncaught PHP Exception Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException: "The option "0" does not exist. Defined options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "first_name", "first_options", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "max_length", "method", "options", "pattern", "post_max_size_message", "property_path", "read_only", "required", "second_name", "second_options", "translation_domain", "trim", "type", "validation_groups", "virtual"." at C:\Users\Vicky\Documents\My Documents\PROJECTS\VF-HOUSING_SYMFONY\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php line 760 

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

我总是使用configureOptions方法将我的实体映射到表单。所以试试这个:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        array(
            'data_class' => 'MyappBundle\Entity\Users',
        )
    );
}

答案 1 :(得分:0)

所以问题解决了。

在RegisterType.php中删除以下函数

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        array(
            'data_class' => 'MyappBundle\Entity\Users',
        )
    );
}

并按如下方式修改RegisterControler.php

$user = new User();

$form = $this->createForm(RegisterType::class,$user);

$form->handleRequest($request);

它解决了我提到的问题。 谢谢你的帮助!