如何验证未绑定字段 [英] How to validate unbound fields
问题描述
我有一个表格ChallengeType".其中有这些字段:
I have a form 'ChallengeType'. Which has these fields:
1) 嵌入式表单 team1
输入字段用户名
1) Embedded form team1
input field username
2) 嵌入表单 team2
输入字段用户名
2) Embedded form team2
input field username
3) 地点
一个挑战领域
3) Place
a challenge field
4) 日期
一个挑战领域
4) Date
a challenge field
每个团队的用户名字段在 TeamType 类中设置如下:
The username field for each team has been set like this in the TeamType class:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('player_one', 'fos_user_username', array(
'property_path' => false,
'attr' => array('class' => 'player_autocomplete')
))
;
}
因为property_path 设置为false,所以这个用户名字段是未绑定的.
Because the property_path is set to false, this username field is unbound.
我想验证此字段,以确保输入的用户名是现有用户名.请问我该怎么做?
I would like to validate this field, to make sure that the inputted username is an existing one. How can I do that please?
推荐答案
您可以通过以下方式进行
You can do it by following way
$builder-> addValidator(new CallbackValidator(function(FormInterface $form){
$player_one = $form['player_one'];
//validate
//validation failed?
$form['player_one']->addError(new FormError("error message"));
});
这篇关于如何验证未绑定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!