我有以下情況: 在我FormType類:驗證在Symfony2的數字字段
...
$builder->add('fieldToValidate', 'number', array('required' => false))
...
在模型類:
...
use Symfony\Component\Validator\Constraints as Assert;
...
/**
* @var float $fieldToValidate
*
* @ORM\Column(name="fieldToValidate", type="float", nullable=true)
* @Assert\Type(type="float", message="Must be a number.")
*/
private $fieldToValidate;
在動作控制器:
$entity = $this->getService('my_service')->findMyEntity($entityId);
$editForm = $this->createForm(new MyEntityType(), $entity, $myOptions);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()){
當我提交表單中的一些無效數據在fieldToValidate(例如「富」)時得到一個異常: Expected argument of type "numeric", "boolean" given
在控制器中調試我的代碼我注意到它提高了行 $editForm->bindRequest($request);
excegction我怎樣才能使用$editForm->isValid()
來控制異常。 是不是爲了這個我在模型和形式驗證?
我們可以把整個動作加上表格課程嗎?謝謝 – Flukey
對代碼太多了,幾乎對這種情況並不重要,請告訴我你在想什麼可能是錯的 – Oriam
@Oriam:你有沒有找到解決方案呢?我面臨同樣的問題! –