Symfony:无法在形式上处理请求 [英] Symfony: unable to handle request in form

查看:152
本文介绍了Symfony:无法在形式上处理请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种表单类型( SearchForm - SearchField - SearchFieldType ),每一种都包含下面这样的内容:

SearchFormType

 <?php 

命名空间AppBundle \形成;

使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilderInterface;
使用Symfony \ Component\OptionsResolver\OptionsResolverInterface;

class SearchFormType extends AbstractType
{
/ **
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('fields','collection',array('type'= > new SearchFieldType(),
'allow_add'=> true,
'allow_delete'=> true,
'by_reference'=> false))
- > ; add('submit','submit',array('label'=>Buscar))
;

$ b $ **
* @param OptionsResolverInterface $ resolver
* /
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>'AppBundle \Entity\SearchForm',
'allow_extra_fields'=> true,
'csrf_protection '=> false,
'validation_groups'=> false,
));

$ b / **
* @return string
* /
public function getName()
{
return' appbundle_searchform;


code
$ b $ p $ SearchFieldType

 <?php 

namespace AppBundle \Form;

使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilderInterface;
使用Symfony \ Component\OptionsResolver\OptionsResolverInterface;

class SearchFieldType extends AbstractType
{
/ **
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('name','hidden')
- > ; add('slug','hidden')
- > add('value')
- > add('choices')
- > add('type',新的SearchFieldTypeType())
- > add('actionFilter')
- > add('actionHighlight')
- > add('actionShow')
;

$ b $ **
* @param OptionsResolverInterface $ resolver
* /
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>'AppBundle \Entity\SearchField'
));

$ b / **
* @return string
* /
public function getName()
{
return' appbundle_searchfield;


SearchFieldTypeType

 <?php 

namespace AppBundle \Form;

使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilderInterface;
使用Symfony \Component\Form\FormEvent;
使用Symfony \Component\Form\FormEvents;
使用Symfony \ Component\OptionsResolver\OptionsResolverInterface;

使用AppBundle \Entity \SearchOperator;

class SearchFieldTypeType extends AbstractType
{
/ **
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ entity = $ builder-> getData();

uilder
- > add('name','hidden')
- > add('operators','entity',array('class'= >'AppBundle:SearchOperator',
'multiple'=> false,
'expanded'=> false))
;

$ b $ **
* @param OptionsResolverInterface $ resolver
* /
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>'AppBundle \Entity\SearchFieldType'
));

$ b / **
* @return string
* /
public function getName()
{
return' appbundle_searchfieldtype;
}
}

表单呈现正常,但是当我提交并尝试 $ form-> handleRequest($ request)我得到一个异常:


既不存在属性运算符,也不存在方法addOperator()/removeOperator(),setOperators(),operators(),__set()或__call()在类AppBundle \Entity\SearchFieldType中有公共访问权


实际上并非如此,因为这些方法存在且正常工作:



AppBundle \Entity\SearchFieldType

 <?php 

命名空间AppBundle \Entity;

使用Doctrine \ORM\Mapping作为ORM;
使用AppBundle \Entity \SearchOperator;
/ **
* SearchField
*
* @ ORM\Table()
* @ ORM\Entity
* /
class SearchFieldType
{
/ **
* @var integer
*
* @ ORM \Column(name =id,type =integer)
* @ ORM \Id
* @ ORM\GeneratedValue(strategy =AUTO)
* /
private $ id;

/ **
* @var字符串
*
* @ ORM \Column(name =name,type =string,length = 100 ,nullable = true)
* /
private $ name;
$ b / **
* @ ORM\ManyToMany(targetEntity =SearchOperator,cascade = {persist,remove})
* @ ORM\JoinTable (
* joinColumns = {@ ORM\JoinColumn(name =type_id,referencedColumnName =id)},
* inverseJoinColumns = {@ ORM\JoinColumn(name =operator_id,referencedColumnName =id)}
*)
** /
private $ operators;
$ b $ **
*构造函数
* /
公共函数__construct()
{
$ this->运算符= new \\ \\Doctrine\Common\Collections\ArrayCollection();

$ b $ **
*获取id
*
* @return整数
* /
public function getId( )
{
return $ this-> id;

$ b / **
*名称
*
* @param字符串$名称
* @return SearchFieldType
* /
public function setName($ name)
{
$ this-> name = $ name;

返回$ this;

$ b / **
*获取名称
*
* @return字符串
* /
public function getName )
{
return $ this-> name;
}

/ **
*添加操作符
*
* @param SearchOperator $操作符
*
* @return SearchFieldType
* /
public function addOperator(SearchOperator $ operator)
{
$ this-> operators [] = $ operator;

返回$ this;
}
$ b $ / **
*删除操作符
*
* @param SearchOperator $操作符
* /
公共函数removeOperator(SearchOperator $运算符)
{
$ this->运算符 - > removeElement($运算符);
}

/ **
*获得运算符
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getOperators()
{
return $ this-> operators;
}

public function __toString()
{
return $ this-> name;
}

}

堆栈在vendor / symfony / symfony / src / Symfony / Component / PropertyAccess / PropertyAccessor中跟踪

  php在460行+ 
PropertyAccessor - > writeProperty(object(SearchFieldType),'operators',object(SearchOperator))
在vendor / symfony / symfony / src / Symfony / Component / PropertyAccess / PropertyAccessor (第104行).php在PropertyAccessor - > setValue(对象(SearchFieldType),对象(PropertyPath),对象(SearchOperator))
在vendor / symfony / symfony / src / Symfony / Component / Form /Extension/Core/DataMapper/PropertyPathMapper.php在93行+
在PropertyPathMapper - > mapFormsToData(object(RecursiveIteratorIterator),object(SearchFieldType))
在vendor / symfony / symfony / src / Symfony /第633行的Component / Form / Form.php在Form - > submit(array('operators'=&'156','name'=>'string'),true)
在vendor / symfony / symfony / src / Symfony / Compon中第577行的+ ent / Form / Form.php + Form $&b $ b> submit(array('type'=>数组('operators'=>'156','name'=>'string'),'value'=> 'felipe','name'=> 'Nombre','slug'=> 'nombre'),true)
在vendor / symfony / symfony / src / Symfony / Component / Form / Form.php在第577行


$ b

编辑

控制器代码

  $ searchFormEntity = new SearchForm(); 
$ searchFormWithValues = $ this-> createForm(new SearchFormType(),$ searchFormEntity,array(
'action'=> $ this-> generateUrl('candidato'),
'method'=>'POST'
));
$ searchFormWithValues-> add('submit','submit',array('label'=>'Buscar'));
$ searchFormWithValues-> handleRequest($ request);


解决方案

那么你有一个ManyToMany关系,所以它会使感觉运营商字段是一个集合。然而,你将它定义为一个实体,所以现在表单希望将 setOperators getOperators 方法设置为 entity 意味着ManyToOne或OneToOne关系。
我认为您需要更改类 SearchFieldTypeType 中添加运算符属性的语句如果您想保留ManyToMany关系,则与字段 SearchFormType 中所做的一样。


I have 3 form types (SearchForm - SearchField - SearchFieldType), each one including next like this:

SearchFormType:

<?php

namespace AppBundle\Form;

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

class SearchFormType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('fields', 'collection', array('type' => new SearchFieldType(),
                                                  'allow_add'    => true,
                                                  'allow_delete' => true,
                                                  'by_reference' => false))
            ->add('submit', 'submit', array('label' => "Buscar"))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\SearchForm',
            'allow_extra_fields' => true,
            'csrf_protection' => false,
            'validation_groups' => false,
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_searchform';
    }
}

SearchFieldType:

<?php

namespace AppBundle\Form;

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

class SearchFieldType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'hidden')
            ->add('slug', 'hidden')
            ->add('value')
            ->add('choices')
            ->add('type', new SearchFieldTypeType())
            ->add('actionFilter')
            ->add('actionHighlight')
            ->add('actionShow')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\SearchField'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_searchfield';
    }
}

SearchFieldTypeType:

<?php

namespace AppBundle\Form;

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

use AppBundle\Entity\SearchOperator;

class SearchFieldTypeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $entity = $builder->getData();

        $builder
            ->add('name', 'hidden')
            ->add('operators', 'entity', array('class' => 'AppBundle:SearchOperator',
                                               'multiple' => false,
                                               'expanded' => false))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\SearchFieldType'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_searchfieldtype';
    }
}

The form renders properly, but when I submit and try to do $form->handleRequest($request) I get an exception:

Neither the property "operators" nor one of the methods "addOperator()"/"removeOperator()", "setOperators()", "operators()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\SearchFieldType"

That's not true actually, as those methods exist and work correctly:

AppBundle\Entity\SearchFieldType:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\SearchOperator;
/**
 * SearchField
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class SearchFieldType
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100, nullable=true)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="SearchOperator", cascade={"persist", "remove"})
     * @ORM\JoinTable(
     *      joinColumns={@ORM\JoinColumn(name="type_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="operator_id", referencedColumnName="id")}
     *      )
     **/
   private $operators;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->operators = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return SearchFieldType
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Add operator
     *
     * @param SearchOperator $operator
     *
     * @return SearchFieldType
     */
    public function addOperator(SearchOperator $operator)
    {
        $this->operators[] = $operator;

        return $this;
    }

    /**
     * Remove operator
     *
     * @param SearchOperator $operator
    */
    public function removeOperator(SearchOperator $operator)
    {
        $this->operators->removeElement($operator);
    }

    /**
     * Get operator
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getOperators()
    {
        return $this->operators;
    }

    public function __toString()
    {
        return $this->name;
    }

}

Stack trace:

 in vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php at line 460  +
at PropertyAccessor ->writeProperty (object(SearchFieldType), 'operators', object(SearchOperator))
in vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php at line 104  +
at PropertyAccessor ->setValue (object(SearchFieldType), object(PropertyPath), object(SearchOperator))
in vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 93  +
at PropertyPathMapper ->mapFormsToData (object(RecursiveIteratorIterator), object(SearchFieldType))
in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 633  +
at Form ->submit (array('operators' => '156', 'name' => 'string'), true)
in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 577  +
at Form ->submit (array('type' => array('operators' => '156', 'name' => 'string'), 'value' => 'felipe', 'name' => 'Nombre', 'slug' => 'nombre'), true)
in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 577

EDIT:

Controller Code :

        $searchFormEntity = new SearchForm();
        $searchFormWithValues = $this->createForm(new SearchFormType(), $searchFormEntity, array(
                                        'action' => $this->generateUrl('candidato'),
                                        'method' => 'POST'
                                    ));
        $searchFormWithValues->add('submit', 'submit', array('label' => 'Buscar'));
        $searchFormWithValues->handleRequest($request);

解决方案

Well you have a ManyToMany relation, so it would make sense to have the operators field be a collection. However you defined it as an entity, so now the form expects to have the setOperators and getOperators methods as entity implies a ManyToOne or OneToOne relationship. I think you need to change the statement in the class SearchFieldTypeType where adding the operators attribute to be the same as what you did before for fields in SearchFormType if you want to keep the ManyToMany relationship.

这篇关于Symfony:无法在形式上处理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆