Aur*_*rel 1 symfony-forms symfony
我有一个带有字段的经典表单:'username','password'和'birthday'.
以下是我的验证断言(在我的用户实体中):
.....
/**
* @var string
* @Assert\NotBlank(message="username.error.blank")
* @Assert\MinLength(limit="2", message="username.error.short")
*/
protected $username;
/**
* @var string
* @Assert\NotBlank(message="password.error.blank")
* @Assert\MinLength(limit="4", message="password.error.short")
*/
protected $password;
/**
* @Assert\True(message="password.error.different")
*/
public function isPasswordLegal()
{
return ($this->username != $this->password);
}
Run Code Online (Sandbox Code Playgroud)
问题是当我提交表格时它完全是空的:
那么,2个问题:
谢谢你的帮助 :-)
斯坦
答案1:使用a GroupSequence.
/**
* @Assert\GroupSequence({"User", "Strict"})
*/
class User
{
/**
* @Assert\True(message="password.error.different", groups="Strict")
*/
public function isPasswordLegal()
{
return ($this->username != $this->password);
}
Run Code Online (Sandbox Code Playgroud)
这将首先验证"User"组中的所有约束.只有当该组中的所有约束都有效时,才会验证第二个组"Strict",我添加了您的自定义验证方法.
为了解释,为什么"用户"包含所有其他约束,我需要详细说明:
groups集的每个约束都属于"默认"组因此,组序列不能包含"默认"组(这将创建一个循环),但需要包含组"{ClassName}".
答案2:使用"error_mapping"选项(仅限最新的Symfony主机).
class UserType
{
public function getDefaultOptions()
{
return array(
'data_class' => '...\User',
'error_mapping' => array(
'passwordLegal' => 'password',
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |