gpt4 book ai didi

Symfony2 : Handling form submissions

转载 作者:行者123 更新时间:2023-12-02 04:57:52 25 4
gpt4 key购买 nike

第一次尝试处理表单,关注官方documentation for Symfony 2.3 .显示表单已经成功,但我无法处理它。

我收到以下错误:

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

这是我的 Controller :

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/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(尽管我不知道它是否相关,因为错误发生在我的 Controller 中调用 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"}

很抱歉打扰了你们,如果这是一个低级错误。如果你们中的任何人可以帮助我解决这个问题,那么您就是在帮我一个很大的忙(如果我们的项目按预期进行,也可能是整个世界)。

最佳答案

哇,我花了一段时间才弄明白...您只是缺少 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"}

关于Symfony2 : Handling form submissions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com