[英]Symfony form validate selected members of entity
我使用symfony表單來驗證登錄數據,所以我把它綁定到實體類(它為所有成員定義了驗證),我需要表單只驗證電子郵件和密碼部分。 所以在表單類中,我不會將電子郵件和密碼添加到表單中。 但是,在提交數據時,它仍然會驗證它們並在表單頂部顯示錯誤
如何在不更改實體類的情況下驗證其他成員(城市,性別等)。
所以我的實體課有:姓名,電子郵件,性別,密碼,城市。 所有領域都需要
登錄表單,包含電子郵件和密碼 我仍然得到其他兩個錯誤
使用驗證組......
實體類:
// src/Acme/BlogBundle/Entity/User.php
namespace Acme\BlogBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
class User implements UserInterface
{
/**
* @Assert\Email(groups={"registration"})
*/
private $email;
/**
* @Assert\NotBlank(groups={"registration"})
* @Assert\Length(min=7, groups={"registration"})
*/
private $password;
/**
* @Assert\Length(min = "2")
*/
private $city;
}
然后,當您創建表單時:
$form = $this->createFormBuilder($users, array(
'validation_groups' => array('registration'),
))->add(...);
這將只驗證email
和password
字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.