2013-07-14 158 views
1

这是我第一次尝试处理表单,我遵循官方documentation for Symfony 2.3。显示表单已经解决了,但我一直无法处理它。Symfony2:提交表单提交

我得到以下错误:

Catchable Fatal Error: Argument 2 passed to Symfony\Component\Validator\Mapping\ClassMetadata::addPropertyConstraint() must be an instance of Symfony\Component\Validator\Constraint, array given, called in /home/torob/lampstack-5.4.16-0/apache2/htdocs/A/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php on line 90 and defined in /home/torob/lampstack-5.4.16-0/apache2/htdocs/A/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/ClassMetadata.php line 193

500 Internal Server Error - 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] 

这里是堆栈跟踪(... nameofproject /供应商/ symfony中/ symfony中/ src目录/ Symfony的/分量/识别/制图/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(虽然我不知道这是否是相关的,因为isVal之前发生的错误标识功能被称为在我的控制器):

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"} 

很抱歉打扰你们,如果这被证明是某种noobish错误的。 如果你们中的任何一个人能够帮助我解决这个问题,你会为我做一个很棒的赞成(如果我们的项目按预期进行的话,也可能是世界)。

+0

'bind()的'被称为'提交()'因为Symfony的2.2,我认为 – ferdynator

+0

谢谢你的提示比亚夫,ferdy! – Tor

+0

我尝试用$ form-> submit($ request)替换$ form-> bind($ request),当重试旧的方式时,但仍然出现同样的错误。 (顺便说一句,如果你觉得它很有趣:看起来他们仍然在2.2文档中使用bind(),但是现在当我在Php-storm中键入bind()时,我被告知它已折旧。) – Tor

回答

4

哇,这一个花了我一段时间弄清楚......你只是在你的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"} 
+0

谢谢!这解决了问题。那么,我一定再也不会犯那个错误了。不知道如果没有像你这样的好人的帮助,我会做什么。如果你曾经在特隆赫姆,想要一杯啤酒,请告诉我。 – Tor

+0

没问题,总是乐于帮忙! –