Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.3 - Symfony 2.7 & 2.8 #106

Merged
merged 1 commit into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
language: php

php: [5.3,5.4,5.5,5.6]
php: [5.3,5.4,5.5,5.6,7.0]

env:
- SF_VERSION='~2.3.0,>=2.3.19'
- SF_VERSION='~2.4.0,>=2.4.9'
- SF_VERSION='~2.5.0,>=2.5.3'
- SF_VERSION='~2.6.0,>=2.6.2'
- SF_VERSION='~2.7.0'
- SF_VERSION='~2.8.0'

before_script:
- export WEB_FIXTURES_HOST=http://localhost/index.php
Expand All @@ -15,7 +13,6 @@ before_script:
- export DISPLAY=:99.0
- sleep 4

- Tests/app/switch_sf_version.sh "$SF_VERSION"
- curl -sS https://getcomposer.org/installer | php
- php -d memory_limit=-1 composer.phar require "symfony/symfony:${SF_VERSION}"
- php -d memory_limit=-1 composer.phar install -n
Expand All @@ -32,6 +29,7 @@ before_script:
- sudo apt-get update > /dev/null
- sudo apt-get install -y --force-yes apache2 libapache2-mod-fastcgi > /dev/null
# enable php-fpm
- if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/www.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/www.conf; fi
- sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
- sudo a2enmod rewrite actions fastcgi alias
- echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
Expand Down
43 changes: 35 additions & 8 deletions Factory/JsFormValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
use Fp\JsFormValidatorBundle\Form\Constraint\UniqueEntity;
use Fp\JsFormValidatorBundle\Model\JsConfig;
use Fp\JsFormValidatorBundle\Model\JsFormElement;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToBooleanArrayTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToBooleanArrayTransformer;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\GetterMetadata;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* This factory uses to parse a form to a tree of JsFormElement's
Expand Down Expand Up @@ -91,7 +93,7 @@ public function __construct(
*/
protected function getMetadataFor($className)
{
return $this->validator->getMetadataFactory()->getMetadataFor($className);
return $this->validator->getMetadataFor($className);
}

/**
Expand Down Expand Up @@ -247,7 +249,7 @@ public function createJsModel(Form $form)
$conf->getOption('invalid_message'),
$conf->getOption('invalid_message_parameters')
);
$model->transformers = $this->parseTransformers($form->getConfig()->getViewTransformers());
$model->transformers = $this->parseTransformers($this->getViewTransformers($form));
$model->cascade = $conf->getOption('cascade_validation');
$model->bubbling = $conf->getOption('error_bubbling');
$model->data = $this->getValidationData($form);
Expand Down Expand Up @@ -315,8 +317,8 @@ protected function getValidationData(Form $form)
$parent = $form->getParent();
if ($parent && null !== $parent->getConfig()->getDataClass()) {
$classMetadata = $metadata = $this->getMetadataFor($parent->getConfig()->getDataClass());
if ($classMetadata->hasMemberMetadatas($form->getName())) {
$metadata = $classMetadata->getMemberMetadatas($form->getName());
if ($classMetadata->hasPropertyMetadata($form->getName())) {
$metadata = $classMetadata->getPropertyMetadata($form->getName());
/** @var PropertyMetadata $item */
foreach ($metadata as $item) {
$this->composeValidationData(
Expand Down Expand Up @@ -448,6 +450,32 @@ protected function isProcessableElement($element)
&& ('hidden' !== $element->getConfig()->getType()->getName());
}

/**
* Gets view transformers from the given form.
* Merges in an extra Choice(s)ToBooleanArrayTransformer transformer in case of expanded choice.
*
* @param FormInterface $form
*
* @return array
*/
protected function getViewTransformers(FormInterface $form)
{
$config = $form->getConfig();
$type = $config->getType()->getInnerType()->getName();
$viewTransformers = $config->getViewTransformers();

// Choice(s)ToBooleanArrayTransformer was deprecated in SF2.7 in favor of CheckboxListMapper and RadioListMapper
if ($type === 'choice' && $config->getOption('expanded')) {
$choiceList = $config->getOption('choice_list');
$transformer = $config->getOption('multiple')
? @new ChoicesToBooleanArrayTransformer($choiceList)
: @new ChoiceToBooleanArrayTransformer($choiceList, $config->getOption('placeholder'));
array_unshift($viewTransformers, $transformer);
}

return $viewTransformers;
}

/**
* Convert transformers objects to data arrays
*
Expand All @@ -471,7 +499,6 @@ protected function parseTransformers(array $transformers)

$result[] = $item;
}

return $result;
}

Expand All @@ -495,7 +522,7 @@ protected function getTransformerParam(DataTransformerInterface $transformer, $p
} elseif (is_scalar($value) || is_array($value)) {
$result = $value;
} elseif ($value instanceof ChoiceListInterface) {
$result = $value->getChoices();
$result = array_values($value->getChoices());
}

return $result;
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# FpJsFormValidatorBundle
[![Build Status](https://travis-ci.org/formapro/JsFormValidatorBundle.svg?branch=1.3)](https://travis-ci.org/formapro/JsFormValidatorBundle)
[![Build Status](https://travis-ci.org/formapro/JsFormValidatorBundle.svg?branch=master)](https://travis-ci.org/formapro/JsFormValidatorBundle)
[![Total Downloads](https://poser.pugx.org/fp/jsformvalidator-bundle/downloads.png)](https://packagist.org/packages/fp/jsformvalidator-bundle)

This module enables validation of the Symfony2 forms on the JavaScript side.
This module enables validation of the Symfony 2.7+ forms on the JavaScript side.
It converts form type constraints into JavaScript validation rules.

If you have Symfony 2.6.* or less* - you need to use [Version 1.2.*](https://github.com/formapro/JsFormValidatorBundle/tree/1.2)

## 1 Installation<a name="p_1"></a>

Expand All @@ -14,7 +15,7 @@ It converts form type constraints into JavaScript validation rules.

Run in terminal:
```bash
$ php composer.phar require "fp/jsformvalidator-bundle":"v1.2.*"
$ php composer.phar require "fp/jsformvalidator-bundle":"dev-master"
```
### 1.2 Enable the bundle<a name="p_1_2"></a>

Expand Down
10 changes: 4 additions & 6 deletions Resources/public/js/constraints/Choice.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ function SymfonyComponentValidatorConstraintsChoice() {

if (this.multiple) {
if (invalidCnt) {
while (invalidCnt--) {
errors.push(this.multipleMessage.replace(
'{{ value }}',
FpJsBaseConstraint.formatValue(invalidList[invalidCnt])
));
}
errors.push(this.multipleMessage.replace(
'{{ value }}',
FpJsBaseConstraint.formatValue(invalidList[0])
));
}
if (!isNaN(this.min) && value.length < this.min) {
errors.push(this.minMessage);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function setName($name)

/**
* @return bool
* @Assert\True(message = "wrong_name")
* @Assert\IsTrue(message = "wrong_name")
*/
public function isNameLegal()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EqualTo;
use Symfony\Component\Validator\Constraints\False;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\IdenticalTo;
use Symfony\Component\Validator\Constraints\Ip;
use Symfony\Component\Validator\Constraints\IsFalse;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\LessThan;
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
Expand All @@ -37,7 +38,6 @@
use Symfony\Component\Validator\Constraints\NotIdenticalTo;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Time;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\Url;

Expand Down Expand Up @@ -293,10 +293,10 @@ public function transformersAction(Request $request, $isValid, $js)
'checkbox',
array(
'constraints' => array(
new True(array(
new IsTrue(array(
'message' => 'checkbox_false'
)),
new False(array(
new IsFalse(array(
'message' => 'checkbox_true'
))
)
Expand All @@ -307,10 +307,10 @@ public function transformersAction(Request $request, $isValid, $js)
'radio',
array(
'constraints' => array(
new True(array(
new IsTrue(array(
'message' => 'radio_false'
)),
new False(array(
new IsFalse(array(
'message' => 'radio_true'
))
)
Expand Down Expand Up @@ -469,7 +469,7 @@ public function emptyAction(
'constraints' => array(
new Email(array('message' => 'wrong_email')),
new EqualTo(array('value' => 'asdf', 'message' => 'wrong_equal_to')),
new False(array('message' => 'wrong_false')),
new IsFalse(array('message' => 'wrong_false')),
new GreaterThan(array('value' => 5, 'message' => 'wrong_greater_than')),
new GreaterThanOrEqual(array('value' => 5, 'message' => 'wrong_greater_than_or_equal')),
new IdenticalTo(array('value' => 5, 'message' => 'wrong_identical_to')),
Expand All @@ -491,7 +491,7 @@ public function emptyAction(
new Time(array('message' => 'wrong_time')),
new Date(array('message' => 'wrong_date')),
new DateTime(array('message' => 'wrong_date_time')),
new True(array('message' => 'wrong_true')),
new IsTrue(array('message' => 'wrong_true')),
new Type(array('type' => 'integer', 'message' => 'wrong_type')),
new Url(array('message' => 'wrong_url')),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function getDatetime()

/**
* @return bool
* @Assert\True(message="true_value")
* @Assert\IsTrue(message="true_value")
*/
public function isTrue()
{
Expand All @@ -361,7 +361,7 @@ public function isTrue()

/**
* @return bool
* @Assert\False(message="false_value")
* @Assert\IsFalse(message="false_value")
*/
public function isFalse()
{
Expand All @@ -370,7 +370,7 @@ public function isFalse()

/**
* @return bool
* @Assert\Null(message="null_{{ value }}")
* @Assert\IsNull(message="null_{{ value }}")
*/
public function isNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function setEmail($email)
/**
* @return bool
*
* @Assert\True(
* @Assert\IsTrue(
* message="getter_message",
* groups={"groups_callback"}
* )
Expand Down
4 changes: 2 additions & 2 deletions Tests/TestBundles/DefaultTestBundle/Entity/TestEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public function setEmail($email)

/**
* @return bool
* @Assert\True(
* @Assert\IsTrue(
* message="getter_groups_array_message",
* groups={"groups_array"}
* )
* @Assert\True(
* @Assert\IsTrue(
* message="getter_no_groups_message"
* )
*/
Expand Down
6 changes: 3 additions & 3 deletions Tests/TestBundles/DefaultTestBundle/Entity/TestSubEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public function getName()

/**
* @return bool
* @Assert\True(
* @Assert\IsTrue(
* message="sub_entity_getter_groups_child_message",
* groups={"groups_child"}
* )
* @Assert\True(
* @Assert\IsTrue(
* message="sub_entity_getter_groups_array_message",
* groups={"groups_array"}
* )
* @Assert\True(
* @Assert\IsTrue(
* message="sub_entity_getter_no_groups_message"
* )
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
class TestTwigExtension extends \Twig_Extension
{
/** @var \Twig_Environment */
protected $env;

/**
* @var Kernel
*/
Expand All @@ -25,21 +22,13 @@ public function __construct($kernel)
$this->kernel = $kernel;
}

/**
* @param \Twig_Environment $environment
*/
public function initRuntime(\Twig_Environment $environment)
{
$this->env = $environment;
}

/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
'update_js_lib' => new \Twig_Function_Method($this, 'updateJsLib'),
new \Twig_SimpleFunction('update_js_lib', array($this, 'updateJsLib')),
);
}

Expand Down
7 changes: 0 additions & 7 deletions Tests/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ public function registerContainerConfiguration(LoaderInterface $loader)
$loader->load(__DIR__.'/Resources/config.php');
}

/**
* An empty init function
*/
public function init()
{
}

/**
* @param string $name
* @param string $extension
Expand Down
4 changes: 1 addition & 3 deletions Tests/app/Resources/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@
$container->loadFromExtension('twig', array(
'debug' => true,
'strict_variables' => true,
'form' => array(
'resources' => array('DefaultTestBundle::form_theme.html.twig')
),
'form_themes' => array('DefaultTestBundle::form_theme.html.twig')
));
$container->loadFromExtension('doctrine', array(
'orm' => array(
Expand Down
Loading