Use Validator component for form Validation
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user