1
我正在使用Silex進行一個小型項目,但我不確定如何驗證兩個匹配的密碼字段,還要檢查使用數據庫連接的電子郵件的唯一性。我無法在SF2文檔中找到它。驗證匹配和唯一使用Symfony驗證程序
可能有人可以給我一個提示或樣本?
在此先感謝
if ('POST' === $user->getMethod()) {
$constraint = new Assert\Collection(array(
'name' => array(new Assert\NotBlank(array('message' => 'Name shouldnt be blank'))),
'username' => array(new Assert\NotBlank(), new Assert\MinLength(3)),
'email' => array(new Assert\NotBlank(), new Assert\Email()),
'password' => array(new Assert\NotBlank(), new Assert\MinLength(6)),
'password2' => array(new Assert\NotBlank(), new Assert\MinLength(6)),
'terms' => array(new Assert\True()),
));
$errors = $app['validator']->validateValue($user->request->all(), $constraint);
if (!count($errors)) {
//do something
}
}
我自己不使用Silex,但是在S2中,唯一的電子郵件有一個UniqueEntity約束(http://symfony.com/doc/current/reference/constraints/UniqueEntity.html),密碼使用重複字段(http ://symfony.com/doc/current/reference/forms/types/repeated.html) – Cerad
切換到使用Symfony形式,因爲它增加了一些功能:)仍然懷疑如何工作的唯一性tho。 – Kristian