Explorar o código

updated for the latest Symfony

Fabien Potencier %!s(int64=14) %!d(string=hai) anos
pai
achega
a70ed83ca1

+ 14 - 11
Controller/ConfiguratorController.php

@@ -30,25 +30,28 @@ class ConfiguratorController extends ContainerAware
         $configurator = $this->container->get('symfony.webconfigurator');
 
         $step = $configurator->getStep($index);
-        $form = $step->getForm($this->container->get('form.context'));
+        $form = $this->container->get('form.factory')->create($step->getFormType());
+        $form->setData($step);
 
-        $form->bind($this->container->get('request'), $step);
+        $request = $this->container->get('request');
+        if ('POST' === $request->getMethod()) {
+            $form->bindRequest($request);
+            if ($form->isValid()) {
+                $configurator->mergeParameters($step->update($form->getData()));
+                $configurator->write();
 
-        if ($form->isValid()) {
-            $configurator->mergeParameters($step->update($form->getData()));
-            $configurator->write();
+                $index++;
 
-            $index++;
+                if ($index < $configurator->getStepCount()) {
+                    return new RedirectResponse($this->container->get('router')->generate('_configurator_step', array('index' => $index)));
+                }
 
-            if ($index < $configurator->getStepCount()) {
-                return new RedirectResponse($this->container->get('router')->generate('_configurator_step', array('index' => $index)));
+                return new RedirectResponse($this->container->get('router')->generate('_configurator_final'));
             }
-
-            return new RedirectResponse($this->container->get('router')->generate('_configurator_final'));
         }
 
         return $this->container->get('templating')->renderResponse($step->getTemplate(), array(
-            'form'    => $form,
+            'form'    => $form->createView(),
             'index'   => $index,
             'count'   => $configurator->getStepCount(),
             'version' => file_get_contents($this->container->getParameter('kernel.root_dir').'/../VERSION'),

+ 0 - 28
Form/CsrfForm.php

@@ -1,28 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Bundle\WebConfiguratorBundle\Form;
-
-use Symfony\Component\Form\Form;
-use Symfony\Component\Form\TextField;
-
-/**
- * CSRF Form.
- *
- * @author Marc Weistroff <marc.weistroff@sensio.com>
- */
-class CsrfForm extends Form
-{
-    public function configure()
-    {
-        $this->add(new TextField('csrf_secret'));
-    }
-}

+ 35 - 0
Form/CsrfType.php

@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\WebConfiguratorBundle\Form;
+
+use Symfony\Component\Form\Type\AbstractType;
+use Symfony\Component\Form\FormBuilder;
+
+/**
+ * CSRF Form Type.
+ *
+ * @author Marc Weistroff <marc.weistroff@sensio.com>
+ */
+class CsrfType extends AbstractType
+{
+    public function buildForm(FormBuilder $builder, array $options)
+    {
+        $builder->add('csrf_secret', 'text');
+    }
+
+    public function getDefaultOptions(array $options)
+    {
+        return array(
+            'data_class' => 'Symfony\Bundle\WebConfiguratorBundle\Step\CsrfStep',
+        );
+    }
+}

+ 0 - 37
Form/DoctrineForm.php

@@ -1,37 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Bundle\WebConfiguratorBundle\Form;
-
-use Symfony\Bundle\WebConfiguratorBundle\Step\DoctrineStep;
-use Symfony\Component\Form\Form;
-use Symfony\Component\Form\TextField;
-use Symfony\Component\Form\ChoiceField;
-use Symfony\Component\Form\PasswordField;
-use Symfony\Component\Form\RepeatedField;
-use Symfony\Component\Form\HiddenField;
-
-/**
- * Doctrine Form.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
-class DoctrineForm extends Form
-{
-    public function configure()
-    {
-        $this->add(new ChoiceField('driver', array('choices' => DoctrineStep::getDrivers())));
-        $this->add(new TextField('name'));
-        $this->add(new TextField('host'));
-        $this->add(new TextField('user'));
-        $this->add(new RepeatedField(new PasswordField('password', array('required' => false)), array('required' => false, 'first_key' => 'Password', 'second_key' => 'Again')));
-    }
-}

+ 47 - 0
Form/DoctrineType.php

@@ -0,0 +1,47 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\WebConfiguratorBundle\Form;
+
+use Symfony\Bundle\WebConfiguratorBundle\Step\DoctrineStep;
+use Symfony\Component\Form\Type\AbstractType;
+use Symfony\Component\Form\FormBuilder;
+
+/**
+ * Doctrine Form Type.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class DoctrineType extends AbstractType
+{
+    public function buildForm(FormBuilder $builder, array $options)
+    {
+        $builder
+            ->add('driver', 'choice', array('choices' => DoctrineStep::getDrivers()))
+            ->add('name', 'text')
+            ->add('host', 'text')
+            ->add('user', 'text')
+            ->add('password', 'repeated', array(
+                'required' => false,
+                'type' => 'password',
+                'first_name' => 'Password',
+                'second_name' => 'Password again',
+            ))
+        ;
+    }
+
+    public function getDefaultOptions(array $options)
+    {
+        return array(
+            'data_class' => 'Symfony\Bundle\WebConfiguratorBundle\Step\DoctrineStep',
+        );
+    }
+}

+ 4 - 5
Resources/views/Step/csrf.html.twig

@@ -11,12 +11,10 @@
 
     {{ form_errors(form) }}
     <form action="{{ path('_configurator_step', { 'index': index }) }} " method="POST">
-        {{ form_hidden(form) }}
-
         <div class="symfony-form-row">
             {{ form_label(form.csrf_secret) }}
             <div class="symfony-form-field">
-                {{ form_field(form.csrf_secret) }}
+                {{ form_widget(form.csrf_secret) }}
                 <a class="symfony-button-grey" href="#" onclick="generateCsrf(); return false;">Generate</a>
                 <div class="symfony-form-errors">
                     {{ form_errors(form.csrf_secret) }}
@@ -24,6 +22,8 @@
             </div>
         </div>
 
+        {{ form_rest(form) }}
+
         <div class="symfony-form-footer">
             <p><input type="submit" value="Next Step" class="symfony-button-grey" /></p>
             <p>* mandatory fields</p>
@@ -38,8 +38,7 @@
             for (i=0; i < 32; i++) {
                 result += Math.round(Math.random()*16).toString(16);
             }
-            document.getElementById('config_csrf_secret').value = result;
+            document.getElementById('csrf_csrf_secret').value = result;
         }
     </script>
-
 {% endblock %}

+ 3 - 5
Resources/views/Step/doctrine.html.twig

@@ -11,22 +11,20 @@
 
     {{ form_errors(form) }}
     <form action="{{ path('_configurator_step', { 'index': index }) }}" method="POST">
-        {{ form_hidden(form) }}
-
         {{ form_row(form.driver) }}
         {{ form_row(form.name) }}
         {{ form_row(form.host) }}
         {{ form_row(form.user) }}
         <div class="symfony-form-row">
-            {{ form_label(form.password) }}
             <div class="symfony-form-field">
-                {{ form_field(form.password.Password) }}
+                {{ form_widget(form.password) }}
                 <div class="symfony-form-errors">
                     {{ form_errors(form.password) }}
                 </div>
             </div>
         </div>
-        {{ form_row(form.password.Again) }}
+
+        {{ form_rest(form) }}
 
         <div class="symfony-form-footer">
             <p><input type="submit" value="Next Step" class="symfony-button-grey" /></p>

+ 9 - 33
Resources/views/form.html.twig

@@ -1,46 +1,22 @@
-{% extends "TwigBundle::form.html.twig" %}
+{% extends "TwigBundle:Form:div_layout.html.twig" %}
 
 {% block field_row %}
     <div class="symfony-form-row">
-        {{ form_label(child) }}
+        {{ form_label(form) }}
         <div class="symfony-form-field">
-            {{ form_field(child) }}
+            {{ form_widget(form) }}
             <div class="symfony-form-errors">
-                {{ form_errors(child) }}
+                {{ form_errors(form) }}
             </div>
         </div>
     </div>
 {% endblock %}
 
-{% block label %}
-    <label for="{{ field.id }}">
-        {% trans label %}
-        {% if field.required %}
+{% block field_label %}
+    <label for="{{ id }}">
+        {{ label|trans }}
+        {% if required %}
             <span class="symfony-form-required" title="This field is required">*</span>
         {% endif %}
     </label>
-{% endblock label %}
-
-{% block errors %}
-    {% if field.hasErrors %}
-        <ul>
-        {% for error in field.errors %}
-            <li>{% trans error.messageTemplate with error.messageParameters from 'validators' %}</li>
-        {% endfor %}
-        </ul>
-    {% endif %}
-{% endblock errors %}
-
-{% block text_field %}
-    {% if attr.type is defined and attr.type != "text" %}
-        <input {{ block('field_attributes') }} value="{{ field.displayedData }}" />
-    {% else %}
-        {% set attr = attr|merge({ 'maxlength': attr.maxlength|default(field.maxlength) }) %}
-        <input type="text" {{ block('field_attributes') }} value="{{ field.displayedData }}" />
-    {% endif %}
-{% endblock text_field %}
-
-{% block password_field %}
-    {% set attr = attr|merge({ 'maxlength': attr.maxlength|default(field.maxlength) }) %}
-    <input type="password" {{ block('field_attributes') }} class="medium_txt" value="{{ field.displayedData }}" />
-{% endblock password_field %}
+{% endblock %}

+ 4 - 5
Step/CsrfStep.php

@@ -11,9 +11,8 @@
 
 namespace Symfony\Bundle\WebConfiguratorBundle\Step;
 
-use Symfony\Component\Form\FormContext;
-use Symfony\Bundle\WebConfiguratorBundle\Form\CsrfForm;
 use Symfony\Bundle\WebConfiguratorBundle\Exception\StepRequirementException;
+use Symfony\Bundle\WebConfiguratorBundle\Form\CsrfType;
 
 /**
  * Csrf Step.
@@ -23,7 +22,7 @@ use Symfony\Bundle\WebConfiguratorBundle\Exception\StepRequirementException;
 class CsrfStep implements StepInterface
 {
     /**
-     * @validation:NotBlank
+     * @assert:NotBlank
      */
     public $csrf_secret;
 
@@ -35,9 +34,9 @@ class CsrfStep implements StepInterface
     /**
      * @see StepInterface
      */
-    public function getForm(FormContext $context)
+    public function getFormType()
     {
-        return CsrfForm::create($context, 'config');
+        return new CsrfType();
     }
 
     /**

+ 7 - 8
Step/DoctrineStep.php

@@ -11,9 +11,8 @@
 
 namespace Symfony\Bundle\WebConfiguratorBundle\Step;
 
-use Symfony\Component\Form\FormContext;
-use Symfony\Bundle\WebConfiguratorBundle\Form\DoctrineForm;
 use Symfony\Bundle\WebConfiguratorBundle\Exception\StepRequirementException;
+use Symfony\Bundle\WebConfiguratorBundle\Form\DoctrineType;
 
 /**
  * Doctrine Step.
@@ -23,22 +22,22 @@ use Symfony\Bundle\WebConfiguratorBundle\Exception\StepRequirementException;
 class DoctrineStep implements StepInterface
 {
     /**
-     * @validation:Choice(callback="getDriverKeys")
+     * @assert:Choice(callback="getDriverKeys")
      */
     public $driver;
 
     /**
-     * @validation:NotBlank
+     * @assert:NotBlank
      */
     public $host;
 
     /**
-     * @validation:NotBlank
+     * @assert:NotBlank
      */
     public $name;
 
     /**
-     * @validation:NotBlank
+     * @assert:NotBlank
      */
     public $user;
 
@@ -58,9 +57,9 @@ class DoctrineStep implements StepInterface
     /**
      * @see StepInterface
      */
-    public function getForm(FormContext $context)
+    public function getFormType()
     {
-        return DoctrineForm::create($context, 'config');
+        return new DoctrineType();
     }
 
     /**

+ 3 - 4
Step/StepInterface.php

@@ -11,7 +11,7 @@
 
 namespace Symfony\Bundle\WebConfiguratorBundle\Step;
 
-use Symfony\Component\Form\FormContext;
+use Symfony\Component\Form\Type\FormTypeInterface;
 
 /**
  * StepInterface.
@@ -30,10 +30,9 @@ interface StepInterface
     /**
      * Returns the form used for configuration.
      *
-     * @param FormContext $context
-     * @return Symfony\Component\Form\Form
+     * @return FormTypeInterface
      */
-    function getForm(FormContext $context);
+    function getFormType();
 
     /**
      * Checks for requirements.