2017 © Pedro Peláez
 

library symfony-standalone-forms

image

victormacko/symfony-standalone-forms

  • Wednesday, October 18, 2017
  • by victormacko
  • Repository
  • 2 Watchers
  • 1 Stars
  • 262 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 38 % Grown

The README.md

Symfony standalone forms

Implementation of Symfony2 forms component with different templating engines, (*1)

The 'views/Form' directory contains a bootstrap implementation (horizontal) of the symfony2 bootstrap created in Twig (https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig & https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_horizontal_layout.html.twig), (*2)

The 'ControllerSymfonyFormTrait.php' file contains a trait to include in your base controller, which adds the 'createForm' and 'createFormBuilder' functions, as detailed here; http://symfony.com/doc/current/forms.html, (*3)

Smarty

web/index.php contains a 'demo' controller to pull together the various components, create a Smarty instance and then output it., (*4)

Volt

Volt support is included, (*5)

In the register-engine block (in services.php), include the following line;, (*6)

$view->registerEngines(array(
    '.volt' => function ($view, $di) use ($config) {

        $volt = new VoltEngine($view, $di);

        $volt->setOptions(array(
            'compiledPath' => $config->application->cacheDir,
            'compiledSeparator' => '_',
            'compileAlways' => true
        ));

        $compiler = $volt->getCompiler();
        SymfonyFormHelper::registerFormPluginsWithVolt($compiler);

        return $volt;
    },
    '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));

Within your controller, to create the form, you just need to include the following;, (*7)

$form = $this
    ->createFormBuilder()
    ->add('testField', \SymfonyStandaloneForms\Type\TextType::class, [
        'constraints' => [
            new \Symfony\Component\Validator\Constraints\Length(['min' => 2])
        ]
    ])
    ->add('submit', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class)
    ->getForm();

$form->handleRequest(\Symfony\Component\HttpFoundation\Request::createFromGlobals());
if($form->isSubmitted()) {
    if($form->isValid()) {
        // get data from field;
        $data = $form->get('testField')->getData();
    }
}

$this->view->form2 = $form->createView();

Your volt code will then need to include the standard template code to render forms - eg;, (*8)

{{ form_start(form, {'attr': {'novalidate': 'novalidate'} }) }}
{{ form_row(form['testField']) }}
{{ form_rest(form) }}
{{ form_end(form) }}

Plain PHP

There's also a plain php form rendering option also if smarty isn't your thing., (*9)

The Versions