refactor how to create Form
This commit is contained in:
@@ -32,30 +32,43 @@ use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
|
||||
class BaseForm {
|
||||
|
||||
abstract class BaseForm {
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return \Symfony\Component\Form\FormFactoryInterface
|
||||
* @var \Symfony\Component\Form\FormFactoryInterface
|
||||
*/
|
||||
public static function getFormFactory(Request $request, $secret = null)
|
||||
protected $form;
|
||||
|
||||
public function __construct(Request $request, $type= "form", $data = array(), $options = array())
|
||||
{
|
||||
$validator = Validation::createValidator();
|
||||
|
||||
$form = Forms::createFormFactoryBuilder()
|
||||
$this->form = Forms::createFormFactoryBuilder()
|
||||
->addExtension(new HttpFoundationExtension())
|
||||
->addExtension(
|
||||
new CsrfExtension(
|
||||
new SessionCsrfProvider(
|
||||
$request->getSession(),
|
||||
$secret ?: ConfigQuery::read("form.secret", md5(__DIR__))
|
||||
isset($option["secret"]) ? $option["secret"] : ConfigQuery::read("form.secret", md5(__DIR__))
|
||||
)
|
||||
)
|
||||
)
|
||||
->addExtension(new ValidatorExtension($validator))
|
||||
->getFormFactory();
|
||||
->getFormFactory()
|
||||
->createBuilder($type, $data, $options);
|
||||
;
|
||||
|
||||
return $form;
|
||||
$this->buildForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Symfony\Component\Form\Form
|
||||
*/
|
||||
public function getForm()
|
||||
{
|
||||
return $this->form->getForm();
|
||||
}
|
||||
|
||||
abstract protected function buildForm();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -24,31 +24,25 @@ namespace Thelia\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
|
||||
|
||||
class CustomerCreation extends AbstractType
|
||||
class CustomerCreation extends BaseForm
|
||||
{
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
public function buildForm()
|
||||
{
|
||||
return $builder->add("name", "text")
|
||||
$this->form->add("name", "text")
|
||||
->add("email", "email", array(
|
||||
"attr" => array(
|
||||
"class" => "field"
|
||||
),
|
||||
"label" => "email"
|
||||
"label" => "email",
|
||||
"constraints" => array(
|
||||
new NotBlank()
|
||||
)
|
||||
)
|
||||
)
|
||||
->add('age', 'integer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of this type.
|
||||
*
|
||||
* @return string The name of this type
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return "customer creation";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user