Add SleepnessUberFrontendValidationBundle by running the command:
$ php composer.phar require sleepness/uber-frontend-validation-bundle "@dev"
Composer will install the bundle to your project's vendor/sleepness
directory.
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Sleepness\UberFrontendValidationBundle\SleepnessUberFrontendValidationBundle(),
);
}
Configure Twig for proper form theming and checking existence of constraint files:
twig:
# some other options
form_themes:
- 'SleepnessUberFrontendValidationBundle:Form:fields.html.twig'
In main layout enable forms validation:
{{ validation_init() }}
If you want to use some assets minifier to validation resources, you need to pass an argument into validation_init()
function.
For now available:
{{ validation_init('yui') }} // for YUI compressor
{{ validation_init('uglify') }} //for Uglify compressor
How to install and configure YUI Compressor read here. How to install and configure UglifyJS(UglifyCSS) read here.
Then your form may look like this:
<form method="post" {{ form_enctype(form) }} >
{{ form_widget(form) }}
<button type="submit" class="form_submit_button">Save</button>
</form>
Add jQuery
to your project in one of the suitable ways.
-
If you want for some reasons to disable client side validation you need only to remove
form_themes
property from Twig configuration. -
If you want to use some custom styles, or scripts, you have to override
SleepnessUberFrontendValidation::form_validation.html.twig
template. -
Forms loaded by ajax will be validated too, just make sure that you enable
{{ validation_init() }}
function in template where loaded form is. -
Validation groups of objects also supported in your forms.
-
If you create submit button using form builder, - you are safe, else, - add
class="form_submit_button"
attribute to your form's submitting field. -
DateTime validator works only if you choose
single_text
widget for it, e.g. in your FormType->add('date', 'datetime', ['widget' => 'single_text'])
(for Date validator this rule works too).