Reputation: 2057
Update: since I'm not getting any answers, I've rewritten the entire post using a much simpler example. Hopefully this helps expose the problem.
I'm having trouble with form validation. I can get the NotBlank() assertion to work, but Type() does not work for me. First, here's the code:
/* ...\Entity\LineItem.php */
<?php
namespace Rialto\ExperimentBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class LineItem
{
/**
* @var integer
* @Assert\NotBlank()
* @Assert\Type(type="integer")
*/
private $quantity = 0;
public function getQuantity()
{
return $this->quantity;
}
public function setQuantity($quantity)
{
$this->quantity = $quantity;
}
}
/* ...\Controller\DefaultController.php */
<?php
namespace Rialto\ExperimentBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Rialto\ExperimentBundle\Entity\LineItem;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->testValidation();
}
private function testValidation()
{
$item = new LineItem();
$form = $this->createFormBuilder($item)
->add('quantity', 'integer')
->getForm();
$request = $this->getRequest();
if ( $request->getMethod() == 'POST') {
$form->bindRequest($request);
if ( $form->isValid() ) {
return new Response('Form is valid.');
}
}
return $this->render('RialtoCoreBundle:Form:basicForm.html.twig', array(
'form' => $form->createView(),
));
}
}
When I leave the input blank, I get an error message, as expected. But when I type "adsf" into the input, I see the output "Form is valid". I've tried the same thing using YAML and PHP validation. Can anyone see what I've done wrong?
Thanks, - Ian
Upvotes: 0
Views: 10689
Reputation: 548
The reason this isn't working as expected is because of a bug in Symfony's stub implementation of NumberFormatter. The stub implementation will be used if you do not have the PHP intl extension installed.
Basically, the number formatter tries to parse the value and returns false when it finds that it starts with a non-numeric character. It should set an error code which would then trigger an exception in the transformer but, since it doesn't, the boolean false value is used and gets typecast to an integer (which is itself a bug, too). So, your "adsf" input ends up as an integer 0 and passes the type assertion.
I found an issue report about this and I've sent pull requests against it for the two bugs.
You can use those patches to fix the issue or you could work around it for now by adding a Min assertion with the limit set to 1.
Upvotes: 8
Reputation: 2899
May be you should change this:
$this->isPost( $request )
For this:
$request->isPost()
It's just a guess..But I hope it helps!
Upvotes: 0