Mopa bootstrap example error forms
The CSRF token is invalid. Please try to resubmit the form.
This form should not contain extra fields.
This value is too short. It should have 5 characters or more.
Annother Invalid Message address
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
@
This value should not be blank.
This value should not be blank.
.00
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.

Note: Labels surround all the options for much larger click areas and a more usable form.

This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
This value should not be blank.
Prefix Text This value should not be blank.
Suffix Text This value should not be blank.
This value should not be blank.
%
This value should not be blank.
This value should not be blank.
This value should not be blank.

The action to generate this page simulates the state after a form submission

The fields contain different error styles. Which get applied

FormType Code

// ExampleErrorsFormType.php

<?php
namespace Mopa\Bundle\BootstrapSandboxBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ExampleErrorsFormType extends AbstractType
{
    private $formErrors;

    public function __construct($formErrors = false)
    {
        $this->formErrors = $formErrors;
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('textfield1', 'text', array(
                'label' => 'Form sizes',
                'constraints' =>  new Assert\Length(array('min' => 5)),
                'attr' => array(
                    'class' => 'input-mini',
                    'placeholder' => 'input-mini',
                )
            ))
            ->add('textarea', 'textarea', array(
                'label_render' => false,
                'error_type' => "block",
                'constraints' =>  new Assert\Email(array('message' => 'Annother Invalid Message address')),
                'attr' => array(
                    'class' => 'input-large',
                    'placeholder' => 'input-large',
                )
            ))
            ->add("subform", new ExampleExtendedFormType(), array(
                    "label_render" => false,
                    "widget_form_group" => false,
                    "horizontal_input_wrapper_class" => "",
                    "horizontal_label_offset_class" => "",
            ))
        ;
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
             // 'error_type' => "block" # uncomment this to change all of the error messages in this form to block
            'errors_on_forms' => $this->formErrors,
        ));
    }
    // ->setAttribute()
    public function getDefaultOptions(array $options)
    {
        return array(
                'csrf_protection' => false // just for demo purpose!
        );
    }
    public function getName()
    {
        return 'mopa_bootstrap_example_error_forms_' . ( ($this->formErrors) ? "form" : "" );
    }
}

Template Code

{# MopaBootstrapSandboxBundle:Examples:errors.html.twig #}

{% extends "MopaBootstrapSandboxBundle::layout.html.twig" %}
{% from 'MopaBootstrapBundle::flash.html.twig' import session_flash %}

{% block headline %}Errors in forms{% endblock headline %}

{% block content %}
<div class="col-lg-6">
    <form class="form-horizontal" id="your_form_name" method="POST">
        {{ form_widget(form) }}
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <button type="reset" class="btn">Cancel</button>
        </div>
    </form>
</div>
<div class="col-lg-6">
    <h3>The action to generate this page simulates the state after a form submission</h3>
    <p>The fields contain different error styles. Which get applied</p>
</div>
{% endblock content %}

{% set showTemplate = _self %}{% set showForm = formType %}
Fork me on GitHub