2017 © Pedro Peláez
 

symfony-bundle quickforms

A Symfony bundle to allow the creation of forms from YML files.

image

binaryspanner/quickforms

A Symfony bundle to allow the creation of forms from YML files.

  • Monday, May 22, 2017
  • by binaryspanner
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Build Status Test Coverage Code Climate Issue Count, (*1)

Quick Forms

A Symfony bundle to allow the creation of forms from YML files., (*2)

Installation

The bundle can be installed using Composer:, (*3)

composer require binaryspanner/quickforms

Use

First the bundle needs registered in the Symfony app's kernel:, (*4)

// app/AppKernel.php
public function registerBundles()
{
    $bundles = [
        // ...
        new BinarySpanner\QuickForms\QuickFormsBundle(),
    ];

    // ...
}

When using the default settings the bundle will search for form files in Resources/forms in the kernel's root directory (normally "path-to-Symfony-project"/app/)., (*5)

To load form files the filenames have to be added to the bundle's settings in config.yml:, (*6)

# app/config/config.yml
# ...
quick_forms:
    file_names: [ 'file1.yml', 'file2.yml', ...]

An example form file can be found in the bundle's Resources/forms directory. This form file will be loaded by default when the bundle's file_names property is unset in config.yml., (*7)

The directories the bundle searches in can be changed by modifying the root_paths and/or directory_paths bundle settings in config.yml:, (*8)

# app/config/config.yml
# ...
quick_forms:
    root_paths: [ '%kernel.root_dir%', '%kernel.root_dir%/../src' ]
    directory_paths: [ 'custom/forms' ]
    file_names: [ 'file1.yml', 'file2.yml', ...]

Note that absolute paths can be used in the directory_paths setting., (*9)

Once the form layout files have been setup a view template needs to be created - if you're using Twig you can create Symfony forms as normal:, (*10)

{# app/Resources/views/quickform/form.html.twig #}
{% extends 'base.html.twig' %}

{% block body %}
    Form One:
    {% form_theme forms.example_form_one.view forms.example_form_one.theme %}
    {{ form_start(forms.example_form_one.view) }}
    {{ form_widget(forms.example_form_one.view) }}
    {{ form_end(forms.example_form_one.view) }}

    Form Two:
    {% form_theme forms.example_form_two.view forms.example_form_two.theme %}
    {{ form_start(forms.example_form_two.view) }}
    {{ form_widget(forms.example_form_two.view) }}
    {{ form_end(forms.example_form_two.view) }}
{% endblock %}

Finally the views need to be loaded in the controller:, (*11)

// src/AppBundle/Controller/DefaultController.php
// ...
/**
 * @Route("/quickforms", name="quickforms")
 */
public function quickForms()
{
    $quickForms = $this->get('quick_forms');

    $forms = $quickForms->loadForms();

    return $this->render('quickform/form.html.twig', array(
        'forms' => $forms
    ));
}

The Versions