Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
96f9b0a
javascript validation prototype
Dec 16, 2011
0fd424a
validation extension
DavidBadura Dec 16, 2011
c68f99c
javascript validator, jquery plugin, form and twig extension update
DavidBadura Dec 21, 2011
c69d093
validation twig extension update
DavidBadura Dec 21, 2011
a6c8b35
form type extension update
DavidBadura Dec 21, 2011
f556673
remove useless defaultValue in prototyped node
DavidBadura Dec 21, 2011
d76e0c9
maxLength constraint fix
DavidBadura Dec 21, 2011
a8e16cc
remove test file
DavidBadura Jan 4, 2012
35713e4
validation constraint update, validation componentsmoved in the same …
DavidBadura Jan 4, 2012
5c6a332
jquery validation plugin update
DavidBadura Jan 4, 2012
dbceb7f
move generator logic in a service, add a command to create a javascri…
DavidBadura Jan 5, 2012
1420a9d
Added missing update to the previous commit
DavidBadura Jan 5, 2012
6a8642b
refactor js generator service
DavidBadura Jan 5, 2012
a483cad
form erros passed as attr
DavidBadura Jan 12, 2012
e505c13
update js validation constraints
DavidBadura Jan 18, 2012
77ef8bd
change event callbacks to jquery events
DavidBadura Jan 27, 2012
99d15da
add not null constraint
DavidBadura Jan 27, 2012
c185eaf
add select support
DavidBadura Jan 27, 2012
7573694
fix breaks in ErrorAttrTypeExtension
DavidBadura Jan 31, 2012
f55317f
js validation make seconds optional by time constrain
DavidBadura Jan 31, 2012
7a029cf
fix validation constraints
DavidBadura Jan 31, 2012
2aca8c2
add support of comma as decimal point
DavidBadura Jan 31, 2012
fa0759b
translate validations
DavidBadura Feb 2, 2012
27e09a1
Merge origin/master
beberlei Feb 8, 2012
0d01a48
Add test for ClientValidation Configuration
beberlei Feb 8, 2012
ef4722d
Cleanups and renamings
beberlei Feb 8, 2012
1ec95ad
Add LICENSE
beberlei Feb 8, 2012
a9e17cf
Add Test for Generator
beberlei Feb 8, 2012
3f9220c
Remove FieldTypeExtension reference in form_extra.xml
beberlei Feb 23, 2012
d8ac294
fix js validation constraint (type integer)
DavidBadura Mar 6, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Command/GenerateJsValidationConstraintsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* SimpleThings FormExtraBundle
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/

namespace SimpleThings\FormExtraBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author david badura <badura@simplethings.de>
*/
class GenerateJsValidationConstraintsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('formextra:jsvalidation:generate')
->setDescription('Generates javascript validation constraints')
->addOption('target', null, InputOption::VALUE_OPTIONAL, 'The target directory', 'web/js')
->addOption('name', null, InputOption::VALUE_OPTIONAL, 'The file name', 'javascript-validation-constraints.js')
->addOption('variable', null, InputOption::VALUE_OPTIONAL, 'The javscript variable name', 'jsFormExtraValidationConstraints')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$target = rtrim($input->getOption('target'), '/');

if (!is_dir($target)) {
throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $target));
}

$objects = $this->getContainer()->getParameter('simple_things_form_extra.client_validation.objects');
$generator = $this->getContainer()->get('simple_things_form_extra.js_validation_constraints_generator');
$constraints = $generator->generate($objects);

$file = $target . '/' . $input->getOption('name');
$variable = $input->getOption('variable');

file_put_contents($file, sprintf('var %s = ', $variable).$constraints);
$output->writeln(sprintf('Generate javascript validation constraints in <comment>%s</comment>', $file));
$output->writeln(sprintf('The javascript variable name is <comment>%s</comment>', $variable));
}
}

19 changes: 19 additions & 0 deletions DependencyInjection/Configuration.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<?php
/**
* SimpleThings FormExtraBundle
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/

namespace SimpleThings\FormExtraBundle\DependencyInjection;

Expand All @@ -13,6 +24,14 @@ public function getConfigTreeBuilder()

return $builder->root('simple_things_form_extra')
->children()
->arrayNode('client_validation')
->canBeUnset()
->children()
->arrayNode('objects')
->prototype('scalar')->end()
->end()
->end()
->end()
->booleanNode('translation_domain_forward_compat')->defaultFalse()->end()
->booleanNode('help_extension')->defaultFalse()->end()
->arrayNode('recaptcha')
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/SimpleThingsFormExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function load(array $configs, ContainerBuilder $container)
$container->getDefinition('simple_things_form_extra.service.recaptcha')->replaceArgument(1, $config['recaptcha']['private_key']);
$container->getDefinition('simple_things_form_extra.form.type.recaptcha')->replaceArgument(1, $config['recaptcha']['public_key']);
}

if (isset($config['client_validation'])) {
$loader->load('client_validation.xml');
$container->setParameter('simple_things_form_extra.client_validation.objects', $config['client_validation']['objects']);
}
}

public function getAlias()
Expand Down
78 changes: 78 additions & 0 deletions Extension/ValidationExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* SimpleThings FormExtraBundle
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/

namespace SimpleThings\FormExtraBundle\Extension;

use SimpleThings\FormExtraBundle\Service\JsValidationConstraintsGenerator;

/**
* @author David Badura <badura@simplethings.de>
*/
class ValidationExtension extends \Twig_Extension
{
/**
*
* @var JsValidationConstraintsGenerator
*/
private $generator;

/**
*
* @var array
*/
private $objects;

/**
*
* @param ValidatorInterface $validator
* @param array $objects
*/
public function __construct(JsValidationConstraintsGenerator $generator, array $objects)
{
$this->generator = $generator;
$this->objects = $objects;
}

/**
* Returns a list of functions to add to the existing list.
*
* @return array An array of functions
*/
public function getFunctions()
{
return array(
'simplethings_formextra_validation' => new \Twig_Function_Method($this, 'getValidationConstraints', array('is_safe' => array('html'))),
);
}

/**
* Generates a JSON representation of the validation constraints that are
* exported to the client-side.
*
* @return string
*/
public function getValidationConstraints()
{
return $this->generator->generate($this->objects);
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'simplethings_formextra_validation';
}
}
49 changes: 49 additions & 0 deletions Form/Extension/ErrorAttrTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace SimpleThings\FormExtraBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Translation\Translator;

/**
*
* @author David Badura <badura@simplethings.de>
*/
class ErrorAttrTypeExtension extends AbstractTypeExtension
{

protected $translator;

public function __construct(Translator $translator)
{
$this->translator = $translator;
}

/**
* @return string
*/
public function getExtendedType()
{
return 'field';
}

public function buildView(FormView $view, FormInterface $form)
{
$errors = array();
$fieldErrors = $form->getErrors();
foreach ($fieldErrors as $fieldError) {
$errors[] = $this->translator->trans($fieldError->getMessageTemplate(), $fieldError->getMessageParameters(), 'validators');
}

if($errors) {
$attr = $view->get('attr');
if(!isset($attr['data-error'])) {
$attr['data-error'] = implode("<br>", $errors);
$view->set('attr', $attr);
}
}
}

}
46 changes: 46 additions & 0 deletions Form/Extension/ValidationTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace SimpleThings\FormExtraBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormBuilder;

/**
*
* @author David Badura <badura@simplethings.de>
*/
class ValidationTypeExtension extends AbstractTypeExtension
{
private $validatedObjects = array();

public function __construct($validatedObjects)
{
$this->validatedObjects = array_flip($validatedObjects);
}

/**
* @return string
*/
public function getExtendedType()
{
return 'form';
}

/**
* @param FormBuilder $builder
* @param array $options
*/
public function buildForm(FormBuilder $builder, array $options)
{
if (isset($this->validatedObjects[$options['data_class']])) {
$attr = $builder->getAttribute('attr');
if(!isset($attr['data-simplethings-validation-class'])) {
$attr['data-simplethings-validation-class'] = $options['data_class'];
$builder->setAttribute('attr', $attr);
}
}
}

}
10 changes: 10 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Copyright (c) 2011-2012, SimpleThings GmbH
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

38 changes: 38 additions & 0 deletions Resources/config/client_validation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="simple_things_form_extra.js_validation_constraints_generator.class">SimpleThings\FormExtraBundle\Service\JsValidationConstraintsGenerator</parameter>
<parameter key="simple_things_form_extra.form.extension.validation.class">SimpleThings\FormExtraBundle\Form\Extension\ValidationTypeExtension</parameter>
<parameter key="simple_things_form_extra.form.extension.error_attr.class">SimpleThings\FormExtraBundle\Form\Extension\ErrorAttrTypeExtension</parameter>
<parameter key="simple_things_form_extra.twig.extension.validation.class">SimpleThings\FormExtraBundle\Extension\ValidationExtension</parameter>
<parameter key="simple_things_form_extra.client_validation.objects" type="collection" />
</parameters>

<services>
<service id="simple_things_form_extra.form.extension.validation" class="%simple_things_form_extra.form.extension.validation.class%">
<tag name="form.type_extension" alias="form" />
<argument>%simple_things_form_extra.client_validation.objects%</argument>
</service>

<service id="simple_things_form_extra.form.extension.error_attr" class="%simple_things_form_extra.form.extension.error_attr.class%">
<tag name="form.type_extension" alias="field" />
<argument type="service" id="translator" />
</service>

<service id="simple_things_form_extra.twig.extension.validation" class="%simple_things_form_extra.twig.extension.validation.class%">
<tag name="twig.extension" alias="form" />
<argument type="service" id="simple_things_form_extra.js_validation_constraints_generator" />
<argument>%simple_things_form_extra.client_validation.objects%</argument>
</service>

<service id="simple_things_form_extra.js_validation_constraints_generator" class="%simple_things_form_extra.js_validation_constraints_generator.class%">
<argument type="service" id="validator.mapping.class_metadata_factory" />
<argument type="service" id="translator" on-invalid="null" />
<argument>%kernel.default_locale%</argument>
</service>
</services>
</container>
53 changes: 53 additions & 0 deletions Resources/public/js/jquery.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @author David Badura <badura@simplethings.de>
*/
(function($, jQuery) {
jQuery.fn.simpleThingsFormExtraValidation = function (options) {
options = jQuery.extend({}, jQuery.fn.simpleThingsFormExtraValidation.defaults, options);

return $(this).each(function() {

var objectName = $(this).data('simplethings-validation-class');

if(typeof options.constraints[objectName] != 'undefined') {
$(this).find('input, select').each(function() {

var $this = $(this);

var name = $this.attr('name');
name = name.substr(name.indexOf("[") + 1, name.indexOf("]") - name.indexOf("[") - 1);

options.onCreate($this);

$this.bind('change', function() {
if(typeof options.constraints[objectName][name] != 'undefined') {
if(options.validator.isValid($this.val(), options.constraints[objectName][name])) {
$this.trigger({
type: 'validationSuccess'
});
} else {
$this.trigger({
type: 'validationError',
violations: options.validator.violations
});
}
}
});

$this.bind('blur', function() {
$(this).trigger('change');
});

});
}

});
};

jQuery.fn.simpleThingsFormExtraValidation.defaults = {
validator: null,
constraints: null,
onCreate: function(object) {}
};

})(jQuery, jQuery);
Loading