Forms 字符串的Symfony表单类型约束验证整数值
我有一个包含三个字段的简单实体:一个自动生成的ID、一个字符串和一个整数。我已经为后两个设置了约束,与integer字段相对应的约束可以完美地工作。如果我发送的整数不在范围内,或者我应该发送除整数以外的任何内容,则返回一个错误 但是,字符串字段不会出现这种情况。如果我发送一个整数,表单将正确验证。为什么会这样 我用于测试的代码是:Forms 字符串的Symfony表单类型约束验证整数值,forms,symfony,validation,fosrestbundle,Forms,Symfony,Validation,Fosrestbundle,我有一个包含三个字段的简单实体:一个自动生成的ID、一个字符串和一个整数。我已经为后两个设置了约束,与integer字段相对应的约束可以完美地工作。如果我发送的整数不在范围内,或者我应该发送除整数以外的任何内容,则返回一个错误 但是,字符串字段不会出现这种情况。如果我发送一个整数,表单将正确验证。为什么会这样 我用于测试的代码是: curl -H "Content-Type: application/json" -X POST -d '{"fieldOne": 9000, "fieldTwo":
curl -H "Content-Type: application/json" -X POST -d '{"fieldOne": 9000, "fieldTwo": 5}' http://localhost:8000/foos
FooController.php
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\FooBar;
use AppBundle\Form\FooBarType;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FooController extends FOSRestController
{
public function getFooAction()
{
$view = new View();
$data = array(
'foo' => 'bar'
);
$view->setStatusCode(Response::HTTP_OK);
$view->setData($data);
return $view;
}
public function postFooAction(Request $request)
{
$foobar = new FooBar();
$form = $this->createForm(FooBarType::class, $foobar);
$data = json_decode($request->getContent(), true);
$form->submit($data);
if ($form->isValid()) {
//$foobar = $form->getData();
$response = new Response('All good');
$response->setStatusCode(Response::HTTP_OK);
return $response;
}
return View::create($form, Response::HTTP_BAD_REQUEST);
}
}
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class FooBarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('fieldOne')
->add('fieldTwo')
;
}
}
id;
}
/**
*现场直播
*
*@param string$fieldOne
*
*@returnfoobar
*/
公共功能setFieldOne($fieldOne)
{
$this->fieldOne=$fieldOne;
退还$this;
}
/**
*打一号
*
*@返回字符串
*/
公共函数getFieldOne()
{
返回$this->fieldOne;
}
/**
*第二场
*
*@param int$fieldTwo
*
*@returnfoobar
*/
公共函数setFieldTwo($fieldTwo)
{
$this->fieldTwo=$fieldTwo;
退还$this;
}
/**
*打一号
*
*@返回字符串
*/
公共函数getFieldTwo()
{
返回$this->fieldTwo;
}
}
问题发生在表单提交方法的类型转换过程中,如下所示:
原因是为了保持与标准HTTP请求的兼容性,始终以字符串形式提交值,并允许Symfony将值强制转换为所需的类型
请记住,父窗体是复合的,每个字段都是子窗体元素,这会导致执行类型转换
例如:
$builder->add('a', Form\TextType::class, [
'constraints' => [
new Assert\Type(['type' => 'string'])
]
]);
$form = $builder->getForm();
$form->submit(['a' => 1]);
dump($form->getData);
dump($form->isValid());
结果:
array:1 [▼
"a" => "1"
]
true
在本例中,TextType
不会将值强制转换为任何内容,但submit已经将值强制转换为字符串
但是,如果使用new Assert\Type(['Type'=>'int'])
约束,并且字段类型不是Form\IntegerType
,则验证将失败
而范围约束仅测试满足最小值和最大值的数值,这也适用于数值字符串
例如:
感谢您的彻底澄清和花时间撰写。
$builder->add('a', Form\TextType::class, [
'constraints' => [
new Assert\Type(['type' => 'string'])
]
]);
$form = $builder->getForm();
$form->submit(['a' => 1]);
dump($form->getData);
dump($form->isValid());
array:1 [▼
"a" => "1"
]
true
$min = 1;
$max = 10;
$a = "5";
var_dump($a < $max && $a > $min); //true
$min = 1;
$max = 10;
$a = "c";
var_dump($a < $max && $a > $min); //false
class FooBar
{
//...
/**
* @var string
*
* @Assert\Type("string")
* @Assert\Expression("this.isNotNumeric()")
* @ORM\Column(name="field_one", type="string", length=255)
*/
private $fieldOne;
/**
* @return bool
*/
public function isNotNumeric()
{
return !is_numeric($this->fieldOne);
}
}