Refactored Form system to get simple error handling and a clearer
interface
This commit is contained in:
@@ -23,7 +23,6 @@
|
||||
namespace Thelia\Form;
|
||||
|
||||
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Constraints\Choice;
|
||||
@@ -32,7 +31,7 @@ class AdminLogin extends BaseForm {
|
||||
|
||||
protected function buildForm()
|
||||
{
|
||||
$this->form
|
||||
$this->formBuilder
|
||||
->add("username", "text", array(
|
||||
"constraints" => array(
|
||||
new NotBlank(),
|
||||
@@ -44,12 +43,15 @@ class AdminLogin extends BaseForm {
|
||||
new NotBlank()
|
||||
)
|
||||
))
|
||||
->add("remember_me", "checkbox")
|
||||
->add("remember_me", "checkbox", array(
|
||||
'value' => 'yes'
|
||||
))
|
||||
->add("success_url", "text")
|
||||
;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return "admin_login";
|
||||
return "adminLogin";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,9 +36,14 @@ abstract class BaseForm {
|
||||
/**
|
||||
* @var \Symfony\Component\Form\FormFactoryInterface
|
||||
*/
|
||||
protected $formBuilder;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Form\Form
|
||||
*/
|
||||
protected $form;
|
||||
|
||||
public $name;
|
||||
private $view = null;
|
||||
|
||||
public function __construct(Request $request, $type= "form", $data = array(), $options = array())
|
||||
{
|
||||
@@ -48,7 +53,7 @@ abstract class BaseForm {
|
||||
$options["attr"]["thelia_name"] = $this->getName();
|
||||
}
|
||||
|
||||
$this->form = Forms::createFormFactoryBuilder()
|
||||
$this->formBuilder = Forms::createFormFactoryBuilder()
|
||||
->addExtension(new HttpFoundationExtension())
|
||||
->addExtension(
|
||||
new CsrfExtension(
|
||||
@@ -63,9 +68,19 @@ abstract class BaseForm {
|
||||
->createNamedBuilder($this->getName(), $type, $data, $options);
|
||||
;
|
||||
|
||||
$this->buildForm();
|
||||
|
||||
$this->form = $this->formBuilder->getForm();
|
||||
}
|
||||
|
||||
$this->buildForm();
|
||||
public function createView() {
|
||||
$this->view = $this->form->createView();
|
||||
}
|
||||
|
||||
public function getView() {
|
||||
if ($this->view === null) throw new \LogicException("View was not created. Please call BaseForm::createView() first.");
|
||||
|
||||
return $this->view;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,13 +88,13 @@ abstract class BaseForm {
|
||||
*/
|
||||
public function getForm()
|
||||
{
|
||||
return $this->form->getForm();
|
||||
return $this->form;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* in this function you add all the fields you need for your Form.
|
||||
* Form this you have to call add method on $this->form attribute :
|
||||
* Form this you have to call add method on $this->formBuilder attribute :
|
||||
*
|
||||
* $this->form->add("name", "text")
|
||||
* ->add("email", "email", array(
|
||||
|
||||
@@ -33,7 +33,7 @@ class CustomerCreation extends BaseForm
|
||||
|
||||
protected function buildForm()
|
||||
{
|
||||
$this->form
|
||||
$this->formBuilder
|
||||
->add("firstname", "text", array(
|
||||
"constraints" => array(
|
||||
new Constraints\NotBlank()
|
||||
|
||||
@@ -32,7 +32,7 @@ class CustomerLogin extends BaseForm {
|
||||
|
||||
protected function buildForm()
|
||||
{
|
||||
$this->form
|
||||
$this->formBuilder
|
||||
->add("username", "text", array(
|
||||
"constraints" => array(
|
||||
new NotBlank(),
|
||||
|
||||
@@ -52,7 +52,7 @@ class CustomerModification extends BaseForm {
|
||||
protected function buildForm()
|
||||
{
|
||||
|
||||
$this->form
|
||||
$this->formBuilder
|
||||
->add("firstname", "text", array(
|
||||
"constraints" => array(
|
||||
new Constraints\NotBlank()
|
||||
|
||||
Reference in New Issue
Block a user