我有一个表单生成器集来创建用户信息表单:
$form = $this->createFormBuilder($userSettings)
->add('newName', TextType::class, array(
'required' => false,
'label' => "Change UserName",
'attr' => array(
'placeholder' => $userData[0]->getUsername()
)
))
->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => $userData[0]->getEmail()
)
))
->add('newPassword', PasswordType::class, array(
'required' => false,
'label' => "Change Password"
))
->add('currentPassword', PasswordType::class, array(
'label' => "Current Password"
))
->add('save', SubmitType::class, array('label' => 'Update Information!'))
->getForm();
现在提交表单后,我有一个处理程序,可以检查邮件或用户名是否已被删除
// Check if name or mail not occupied
if($user_manager->findUserByEmail($mail)){
}
elseif($user_manager->findUserByUsername($name)){
}
我的目标是,在已填写的邮件/用户名上再次打印表单,只有这一次,占用的值会有占位符说"此值已被占用"
现在我尝试添加另一个"添加"进入if语句,如
$form->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => "EMAIL TAKEN"
)
));
不幸的是,这让我回答错误
您无法将儿童添加到提交的表单
现在我的问题是: 有没有一个好的和正确的方法呢?
B侧:
我可以触发浏览器默认验证,例如。在chrome中,如果需要输入并且你没有把东西放在那里,那么该框会变成红色,你会收到一条错误信息。
我想知道我是否可以在这里做同样的事情,所以对于这个项目,所以在这种情况下,我会得到一个红色输入框,提醒和#34;电子邮件采取"或类似的东西
答案 0 :(得分:0)
也许是这样的?
$form = $this->createFormBuilder($userSettings)
->add('newName', TextType::class, array(
'required' => false,
'label' => "Change UserName",
'attr' => array(
'placeholder' => $userData[0]->getUsername()
)
))
->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => $userData[0]->getEmail()
)
))
->add('newPassword', PasswordType::class, array(
'required' => false,
'label' => "Change Password"
))
->add('currentPassword', PasswordType::class, array(
'label' => "Current Password"
))
->add('save', SubmitType::class, array('label' => 'Update Information!'));
if(isset($mail) && $user_manager->findUserByEmail($mail)){
$form->remove('newMail')
->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => 'EMAIL TAKEN'
)
));
}
elseif(isset($name) && $user_manager->findUserByUsername($name)){
// just duplicate the above
}
$form->getForm();