Reputation: 284
This is my first time trying to handle a form, and I'm following the official documentation for Symfony 2.3. Displaying the form has worked out, but I have not been able to handle it.
I get the following error:
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
Here is my 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'));
}
}
I know that it's the method-call $form->handleRequest($request)
that creates the error. I also tried to do it "the old way" from the 2.1-documentation (they say that the handleRequest-method is new):
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'));
}
}
This gave me the same error.
Extra info:
Here is the route:
ideside_newidea_post:
path: /post/idea
defaults: { _controller: IdesideIdeaBundle:Default:newIdeaPost }
methods: [POST]
Here is the 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);
Here is my validation.yml (although I don't know if it's relevant, since the error occurs before the isValid-function is called in my controller):
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"}
Sorry to have bothered you guys if this turns out to be some kind of noobish mistake. If any of you could help me out with this you would be doing me a great favour (and potentially also the world if our project works out as intended).
Upvotes: 2
Views: 6464
Reputation: 2748
Wow, this one took me awhile to figure out... You're just missing the space between the dash and the "L" in your valueDescription length attribute.
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"}
Upvotes: 5