From b2ee3c3ef8c6e1de9e3e3995be4883bf8122936c Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 25 Jun 2013 15:30:29 +0200 Subject: [PATCH] Use Validator component for form Validation --- core/lib/Thelia/Form/AdminLogin.php | 9 ++++++++- core/lib/Thelia/Form/BaseForm.php | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/lib/Thelia/Form/AdminLogin.php b/core/lib/Thelia/Form/AdminLogin.php index 2284cba91..2bd2debac 100644 --- a/core/lib/Thelia/Form/AdminLogin.php +++ b/core/lib/Thelia/Form/AdminLogin.php @@ -25,13 +25,20 @@ namespace Thelia\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Validator\Constraints\Length; +use Symfony\Component\Validator\Constraints\NotBlank; class AdminLogin extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { return $builder - ->add("username", "text") + ->add("username", "text", array( + "constraints" => array( + new NotBlank(), + new Length(array("min" => 3)) + ) + )) ->add("password", "password"); } diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index 1f3f15d79..da6689f24 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -22,12 +22,14 @@ /*************************************************************************************/ namespace Thelia\Form; +use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Forms; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; use Symfony\Component\Form\Extension\Csrf\CsrfExtension; use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider; +use Symfony\Component\Validator\Validation; use Thelia\Model\ConfigQuery; class BaseForm { @@ -38,6 +40,8 @@ class BaseForm { */ public static function getFormFactory(Request $request, $secret = null) { + $validator = Validation::createValidator(); + $form = Forms::createFormFactoryBuilder() ->addExtension(new HttpFoundationExtension()) ->addExtension( @@ -47,7 +51,9 @@ class BaseForm { $secret ?: ConfigQuery::read("form.secret", md5(__DIR__)) ) ) - )->getFormFactory(); + ) + ->addExtension(new ValidatorExtension($validator)) + ->getFormFactory(); return $form; }