Symfony2:处理表单提交

时间:2013-07-14 19:22:32

标签: symfony

这是我第一次尝试处理表单,而我正在关注官方documentation for Symfony 2.3。显示表单已经解决了,但我无法处理它。

我收到以下错误:

  

可捕获的致命错误:参数2传递给   的Symfony \组件\验证\映射\ ClassMetadata :: addPropertyConstraint()   必须是Symfony \ Component \ Validator \ Constraint,array的实例   给予,召唤   /home/torob/lampstack-5.4.16-0/apache2/htdocs/A/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php   在第90行并在中定义   /home/torob/lampstack-5.4.16-0/apache2/htdocs/A/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/ClassMetadata.php   第193行

     

500内部服务器错误 - ContextErrorException

这是我的控制器:

public function newIdeaPostAction(Request $request){
    $idea = new Idea();

    $form = $this->createFormBuilder($idea)
        ->add('title', 'text')
        ->add('shortDescription', 'text')
        ->add('valueDescription', 'text')
        ->add('description', 'textarea')
        ->add('Next', 'submit')
        ->getForm();

    $form->handleRequest($request);

    if($form->isValid()){
        return $this->redirect($this->generateUrl('ideside_idea_success'));
    }
}

我知道这是创建错误的方法调用$form->handleRequest($request)。我也尝试从2.1文档中做“旧方法”(他们说handleRequest方法是新的):

if ($request->isMethod('POST')) {
    $form->bind($request);
    if ($form->isValid()) {
        // perform some action, such as saving the task to the database
        return $this->redirect($this->generateUrl('task_success'));
    }
}

这给了我同样的错误。

额外信息:

这是路线:

ideside_newidea_post:
  path: /post/idea
  defaults:  { _controller: IdesideIdeaBundle:Default:newIdeaPost }
  methods:  [POST]

这是stacktrace(... nameofproject / vendor / symfony / symfony / src / Symfony / Component / Validator / Mapping / ClassMetadata.php):

 *
 * @return ClassMetadata This object
 */
public function addPropertyConstraint($property, Constraint $constraint)
{
    if (!isset($this->properties[$property])) {
        $this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);

这是我的validation.yml(虽然我不知道它是否相关,因为错误发生在我的控制器中调用isValid函数之前):

Ideside\IdeaBundle\Entity\Idea:
    properties:
        title:
            - NotBlank: {message: "blabla"}
        shortDescription:
            - NotBlank: {message: "blabla"}
            - Length: {max: 115, maxMessage: "blabla", min: 6, minMessage: "blabla"}
        valueDescription:
            -Length: {max: 115, maxMessage: "blabla", min: 5, minMessage: "blabla"}
        description:
            - Length: {max: 5000, maxMessage: "blabla"}

很抱歉打扰你们,如果事实证明这是某种愚蠢的错误。 如果你们中的任何一个人能够帮助我解决这个问题,你们会给我一个伟大的青睐(如果我们的项目按照预期的方式运作,也可能是世界的。)

1 个答案:

答案 0 :(得分:5)

哇,这个花了我一段时间才弄明白......你只是在你的valueDescription长度属性中错过了短划线和“L”之间的空格。

Ideside\IdeaBundle\Entity\Idea:
    properties:
        title:
            - NotBlank: {message: "blabla"}
        shortDescription:
            - NotBlank: {message: "blabla"}
            - Length: {max: 115, maxMessage: "blabla", min: 6, minMessage: "blabla"}
        valueDescription:
            // This is the original line
            // You do not have a space between the dash and Length
            // -Length: {max: 115, maxMessage: "blabla", min: 5, minMessage: "blabla"}
            // It should be this
            - Length: {max: 115, maxMessage: "blabla", min: 5, minMessage: "blabla"}
        description:
            - Length: {max: 5000, maxMessage: "blabla"}