clicktrend/frontend-validation-bundle

jQuery frontend validation as symfony2 bundle

Installs: 11

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Language:JavaScript

Type:symfony-bundle

dev-master 2013-11-28 21:13 UTC

This package is not auto-updated.

Last update: 2024-04-08 12:53:41 UTC


README

jQuery Validation using Symfony2 Constraints and Twitter Bootstrap.

Installation

Install this bundle using composer

composer.phar require clicktrend/frontend-validation-bundle:dev-master

Register the bundle in àpp/AppKernel.php`

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Clicktrend\Bundle\FrontendValidationBundle\ClicktrendFrontendValidationBundle(),
    );
}

Add assets files to assetic bundle in app/config/config.yml

assetic:
    assets:
        jquery_validation:
            inputs:
                - %kernel.root_dir%/../vendor/reactive-raven/jq-bootstrap-validation/src/jqBootstrapValidation.js
            output: js/jqBootstrapValidation.js

Add js/jqBootstrapValidation.js to project

Usage

Example for annotation usage

/**
 * @Assert\NotBlank(groups={"registration_finish"})
 * @Assert\Length(
 *      min="4", 
 *      max="12",
 *      minMessage = "form.username.min_length",
 *      maxMessage = "form.username.max_length",
 *      groups={"registration_finish"})
 */
protected $username;

Actually only four constraints are implemented: Email, Length, NotBlank and Regex.

Example HTML form

<form class="form-horizontal" action="{{ path('registration') }}" {{ form_enctype(form) }} method="POST" novalidate>
    {{ form_widget(form)}}
    <!-- detailed example
    <div class="row-fluid">
        <div class="control-group">
            <div class="controls-row">
                {{ form_widget(form.email, { 'attr': {'id': 'email-field', 'class': 'span12', 'placeholder': 'form.email.label'|trans(), 'data-validation-required-message': 'form.email.not_blank'|trans(), 'data-validation-email-message': 'form.email.not_valid'|trans()} }) }}
                <div class="help-block"></div>
                <small> {{ 'form.email.help'|trans() }} {{ form_errors(form.email) }}</small>
            </div>
        </div><!-- End .control-group  -->
    </div><!-- End .row-fluid  -->
    -->
</form>

Tip

To validate NotBlank with validation script add "novalidate" attribute to form element to skip HTML5 validation.