如何在已删除的小部件上禁用 @Assert 验证
Posted
技术标签:
【中文标题】如何在已删除的小部件上禁用 @Assert 验证【英文标题】:How to disable @Assert validation on removed widgets 【发布时间】:2020-08-23 18:32:00 【问题描述】:我不知道为什么断言验证仍然适用于我删除的小部件!
我创建一个表单
$form = $this->createForm(UserType::class, $doctor);
然后我从中删除了一些字段
$form ->remove("picture");
$form ->remove("phone");
确实从视图中删除了这些字段!但是当我点击提交时,我仍然收到"@Assert\NotBlank()
错误消息!打电话!
在我做的控制器上
$doctor = new User();
$form = $this->createForm(UserType::class, $doctor);
$form ->remove("picture");
$form ->remove("phone");
这是用户实体的代码(我删除了getter和setter)
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(
* fields="username",
* message="The username is already used."
* )
* @UniqueEntity(
* fields="email",
* message="The Email is already used."
* )
*/
class User implements UserInterface
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
*/
private $username;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
* @Assert\Email(message="This Email is not valid ")
*/
private $email;
/**
* @ORM\Column(type="string", length=255 )
* @Assert\NotBlank()
* @Assert\Length(
* min = 8, max = 30,
* minMessage = "Your password must be between 8 and 30 characters.")
* @Assert\EqualTo(propertyPath = "password2", message="Passwords does not match!" )
*/
private $password;
/**
* @Assert\NotBlank()
* @Assert\EqualTo(propertyPath = "password", message="Passwords does not match!" )
*/
private $password2;
/**
* @ORM\Column(type="string", length=50, nullable=true),
* @Assert\NotBlank(),
* @Assert\Regex(
* pattern="/[0-9]8/",
* message="Invalid Phone number"
* )
*/
private $phone;
/**
* @ORM\Column(type="string", length=255)
*/
private $picture;
/**
* @ORM\Column(type="string", length=255)
*/
private $braclet_id;
/**
* @ORM\Column(type="boolean")
*/
private $is_doctor;
/**
* @Assert\Length(min=5, max=100)
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
private $Full_Name;
/**
* @ORM\Column(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)
【问题讨论】:
【参考方案1】:这是因为无论添加或删除的字段如何,您都在验证您的实体。在这些情况下,您可以使用验证组:
https://symfony.com/doc/current/validation/groups.html
因此,对于您的情况,您可以在“默认”旁边创建一个新组,例如“医生”。
对于所有需要验证的字段都添加:
* @Assert\NotBlank(groups="Default", "doctor")
对于电话字段:
* @Assert\NotBlank(groups="Default")
不要忘记在表单中设置正确的验证组: https://symfony.com/doc/current/form/validation_groups.html
【讨论】:
但是在这种情况下,我需要为我拥有的每个组制作一个新表格吗? 您可以在创建表单时传递验证组或在表单本身中更改它们。以上是关于如何在已删除的小部件上禁用 @Assert 验证的主要内容,如果未能解决你的问题,请参考以下文章