Merge branch 'admin' of https://github.com/thelia/thelia into admin

This commit is contained in:
franck
2013-07-02 15:54:24 +02:00
7 changed files with 81 additions and 46 deletions

View File

@@ -25,13 +25,30 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\ActionEvent; use Thelia\Core\Event\ActionEvent;
use Thelia\Form\BaseForm;
use Thelia\Form\CustomerCreation;
class Customer implements EventSubscriberInterface class Customer implements EventSubscriberInterface
{ {
public function create(ActionEvent $event) public function create(ActionEvent $event)
{ {
$request = $event->getRequest();
$customerForm = new CustomerCreation($request);
$form = $customerForm->getForm();
if ($request->isMethod("post")) {
$form->bind($request);
if ($form->isValid()) {
echo "ok"; exit;
} else {
echo "ko"; exit;
}
}
} }
public function modify(ActionEvent $event) public function modify(ActionEvent $event)

View File

@@ -65,11 +65,9 @@ class AdminController extends BaseAdminController {
protected function getLoginForm() protected function getLoginForm()
{ {
$form = $this->getFormBuilder(); $adminLogin = new AdminLogin($this->getRequest());
$adminLogin = new AdminLogin(); return $adminLogin->getForm();
return $adminLogin->buildForm($form, array())->getForm();
} }
public function lostAction() public function lostAction()

View File

@@ -30,6 +30,30 @@ use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
use Thelia\Core\Template\Smarty\SmartyPluginInterface; use Thelia\Core\Template\Smarty\SmartyPluginInterface;
use Thelia\Log\Tlog; use Thelia\Log\Tlog;
/**
*
* Plugin for smarty defining blocks and functions for using Form display.
*
* blocks :
* - {form name="myForm"} ... {/form} => find form named myForm,
* create an instance and assign this instanciation into smarty variable. Form must be declare into
* config using <forms> tag
*
* - {form_field form=$form.fieldName} {/form_field} This block find info into the Form field containing by
* the form paramter. This field must be an instance of FormView. fieldName is the name of your field. This block
* can output these info :
* * $name => name of yout input
* * $value => value for your input
* * $label => label for your input
* * $error => boolean for know if there is error for this field
* * $attr => all your attribute for your input (define when you construct programmatically you form)
*
* - {form_error form=$form.fieldName} ... {/form_error} Display this block if there are errors on this field.
* fieldName is the name of your field
*
* Class Form
* @package Thelia\Core\Template\Smarty\Plugins
*/
class Form implements SmartyPluginInterface class Form implements SmartyPluginInterface
{ {
@@ -63,11 +87,7 @@ class Form implements SmartyPluginInterface
throw new \InvalidArgumentException("Missing 'name' parameter in form arguments"); throw new \InvalidArgumentException("Missing 'name' parameter in form arguments");
} }
$form = BaseForm::getFormFactory($this->request);
$formBuilder = $form->createBuilder('form');
$instance = $this->getInstance($params['name']); $instance = $this->getInstance($params['name']);
$instance = $instance->buildForm($formBuilder, array());
$template->assign("form", $instance->getForm()->createView()); $template->assign("form", $instance->getForm()->createView());
} else { } else {
@@ -179,8 +199,10 @@ class Form implements SmartyPluginInterface
throw new ElementNotFoundException(sprintf("%s form does not exists", $name)); throw new ElementNotFoundException(sprintf("%s form does not exists", $name));
} }
$class = new \ReflectionClass($this->formDefinition[$name]);
return new $this->formDefinition[$name];
return $class->newInstance($this->request);
} }
/** /**

View File

@@ -23,17 +23,16 @@
namespace Thelia\Form; namespace Thelia\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Choice; use Symfony\Component\Validator\Constraints\Choice;
class AdminLogin extends AbstractType { class AdminLogin extends BaseForm {
public function buildForm(FormBuilderInterface $builder, array $options) protected function buildForm()
{ {
return $builder $this->form
->add("username", "text", array( ->add("username", "text", array(
"constraints" => array( "constraints" => array(
new NotBlank(), new NotBlank(),
@@ -48,13 +47,4 @@ class AdminLogin extends AbstractType {
->add("remember_me", "checkbox"); ->add("remember_me", "checkbox");
} }
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return "admin_login";
}
} }

View File

@@ -32,30 +32,43 @@ use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Validation;
use Thelia\Model\ConfigQuery; use Thelia\Model\ConfigQuery;
class BaseForm { abstract class BaseForm {
/** /**
* @param Request $request * @var \Symfony\Component\Form\FormFactoryInterface
* @return \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(); $validator = Validation::createValidator();
$form = Forms::createFormFactoryBuilder() $this->form = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension()) ->addExtension(new HttpFoundationExtension())
->addExtension( ->addExtension(
new CsrfExtension( new CsrfExtension(
new SessionCsrfProvider( new SessionCsrfProvider(
$request->getSession(), $request->getSession(),
$secret ?: ConfigQuery::read("form.secret", md5(__DIR__)) isset($option["secret"]) ? $option["secret"] : ConfigQuery::read("form.secret", md5(__DIR__))
) )
) )
) )
->addExtension(new ValidatorExtension($validator)) ->addExtension(new ValidatorExtension($validator))
->getFormFactory(); ->getFormFactory()
->createBuilder($type, $data, $options);
;
$this->buildForm();
}
/**
* @return \Symfony\Component\Form\Form
*/
public function getForm()
{
return $this->form->getForm();
}
abstract protected function buildForm();
return $form;
}
} }

View File

@@ -24,31 +24,25 @@ namespace Thelia\Form;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; 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) protected function buildForm()
{ {
return $builder->add("name", "text") $this->form->add("name", "text")
->add("email", "email", array( ->add("email", "email", array(
"attr" => array( "attr" => array(
"class" => "field" "class" => "field"
), ),
"label" => "email" "label" => "email",
"constraints" => array(
new NotBlank()
)
) )
) )
->add('age', 'integer'); ->add('age', 'integer');
} }
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return "customer creation";
}
} }

View File

@@ -8,7 +8,7 @@ An image from asset directory :
{intl l='An internationalized string'} {intl l='An internationalized string'}
</div> </div>
{form name="thelia.customer.creation"} {form name="thelia.customer.creation"}
<form method="post" action="index_dev.php" {form_enctype form=$form} > <form method="post" action="index_dev.php?action=createCustomer" {form_enctype form=$form} >
{form_field_hidden form=$form} {form_field_hidden form=$form}
{form_field form=$form.email} {form_field form=$form.email}
@@ -21,6 +21,7 @@ An image from asset directory :
{form_field form=$form.age} {form_field form=$form.age}
{intl l='age'} : <input type="text" name="{$name}"> {intl l='age'} : <input type="text" name="{$name}">
{/form_field} {/form_field}
<input type="submit" value="valider">
</form> </form>
{/form} {/form}
<div> <div>