想要在不存在时通过表单输入字段加载用户实体向字段添加错误消息
$builder->setMethod('get');
$builder->add('username', 'text'); // add error to this field when username dont exist
$builder->add('token', 'text') // add error on this field when token dont match with current token
// transformer should set token to null
->addModelTransformer(new CallbackTransformer(
function ($original) {
return $original;
},
function ($submitted) {
return null;
}
));
$builder->add('submit', 'submit');
答案 0 :(得分:1)
// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class User
{
/**
* @Assert\NotBlank(message="Name is empty")
*/
protected $name;
}
http://symfony.com/doc/current/reference/constraints.html
我不确定您在检查token
的位置。不要忘记在$form->isValid()
$form->handleReqest($reqest)