我不知道为什么断言验证仍然适用于我删除的小部件!
我创建了一个表单
$form = $this->createForm(UserType::class, $doctor);
然后我从中删除了一些字段
$form ->remove("picture");
$form ->remove("phone");
事实上,这些字段已从视图中删除!但当我点击提交时,我仍然会收到"@AssertNotBlank()
错误消息!在电话里!
在控制器上我做了
$doctor = new User();
$form = $this->createForm(UserType::class, $doctor);
$form ->remove("picture");
$form ->remove("phone");
这是用户实体的代码(我删除了getter和setter(
<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* @ORMEntity(repositoryClass="AppRepositoryUserRepository")
* @UniqueEntity(
* fields={"username"},
* message="The username is already used."
* )
* @UniqueEntity(
* fields={"email"},
* message="The Email is already used."
* )
*/
class User implements UserInterface
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255 , unique=true)
* @AssertNotBlank()
*/
private $username;
/**
* @ORMColumn(type="string", length=255 , unique=true)
* @AssertNotBlank()
* @AssertEmail(message="This Email is not valid ")
*/
private $email;
/**
* @ORMColumn(type="string", length=255 )
* @AssertNotBlank()
* @AssertLength(
* min = 8, max = 30,
* minMessage = "Your password must be between 8 and 30 characters.")
* @AssertEqualTo(propertyPath = "password2", message="Passwords does not match!" )
*/
private $password;
/**
* @AssertNotBlank()
* @AssertEqualTo(propertyPath = "password", message="Passwords does not match!" )
*/
private $password2;
/**
* @ORMColumn(type="string", length=50, nullable=true),
* @AssertNotBlank(),
* @AssertRegex(
* pattern="/[0-9]{8}/",
* message="Invalid Phone number"
* )
*/
private $phone;
/**
* @ORMColumn(type="string", length=255)
*/
private $picture;
/**
* @ORMColumn(type="string", length=255)
*/
private $braclet_id;
/**
* @ORMColumn(type="boolean")
*/
private $is_doctor;
/**
* @AssertLength(min=5, max=100)
* @ORMColumn(type="string", length=255)
* @AssertNotBlank()
*/
private $Full_Name;
/**
* @ORMColumn(type="boolean")
*/
private $isActive;
}
这是视图的代码:
{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}
{{ form_widget(form.Full_Name, {'attr': {'class': 'form-control', 'placeholder': 'Full Name','type': 'text'} }) }}
{{ form_errors(form.Full_Name) }}
{{ form_widget(form.username, {'attr': {'class': 'form-control', 'placeholder': 'Username'} }) }}
{{ form_errors(form.username) }}
{{ form_widget(form.email, {'attr': {'class': 'form-control', 'placeholder': 'Email Adress'} }) }}
{{ form_errors(form.email) }}
{{ form_widget(form.password, {'attr': {'class': 'form-control', 'placeholder': 'Password','type': 'Password'} }) }}
{{ form_errors(form.password) }}
{{ form_widget(form.password2, {'attr': {'class': 'form-control', 'placeholder': 'Confirm Password','type': 'Password'} }) }}
{{ form_errors(form.password2) }}
<button class="btn btn-raised g-bg-cyan waves-effect">SIGN UP</button>
{{ form_rest(form) }}
{{ form_end(form) }}
这是因为无论添加或删除哪些字段,都要验证实体。在这些情况下,您可以使用验证组:
https://symfony.com/doc/current/validation/groups.html
因此,对于您的案例,您可以在"默认"旁边创建一个新组,例如"医生"。
对于所有需要在两者中验证的字段,请添加:
* @AssertNotBlank(groups={"Default", "doctor"})
对于电话字段:
* @AssertNotBlank(groups={"Default"})
不要忘记在表单中设置正确的验证组:https://symfony.com/doc/current/form/validation_groups.html