From 7b6c77140f1f5899546528c338e88932dfab4c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0t=C3=ADpek?= Date: Sat, 26 Mar 2016 17:43:16 +0100 Subject: [PATCH] - Symfony 2.8 WIP - PHP7 compatibility - Normalized multi choice behavior with 2.5.5+ - Minimum symfony version set to 2.7 - Updated JsFormValidatorFactory to return Choice(s)ToBooleanArrayTransformer transformer in case of expanded choice. --- .travis.yml | 10 ++--- Factory/JsFormValidatorFactory.php | 43 +++++++++++++++---- README.md | 7 +-- Resources/public/js/constraints/Choice.js | 10 ++--- Tests/Fixtures/Entity.php | 2 +- .../Controller/FunctionalTestsController.php | 16 +++---- .../Entity/BasicConstraintsEntity.php | 6 +-- .../Entity/CustomizationEntity.php | 2 +- .../DefaultTestBundle/Entity/TestEntity.php | 4 +- .../Entity/TestSubEntity.php | 6 +-- .../Twig/Extension/TestTwigExtension.php | 13 +----- Tests/app/AppKernel.php | 7 --- Tests/app/Resources/config.php | 4 +- .../JsFormValidatorTwigExtension.php | 19 +++----- composer.json | 12 +++--- 15 files changed, 79 insertions(+), 82 deletions(-) diff --git a/.travis.yml b/.travis.yml index ed920c3..98d0468 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -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 @@ -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 diff --git a/Factory/JsFormValidatorFactory.php b/Factory/JsFormValidatorFactory.php index a34a665..218b5e5 100644 --- a/Factory/JsFormValidatorFactory.php +++ b/Factory/JsFormValidatorFactory.php @@ -5,8 +5,10 @@ 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; @@ -14,7 +16,7 @@ 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 @@ -91,7 +93,7 @@ public function __construct( */ protected function getMetadataFor($className) { - return $this->validator->getMetadataFactory()->getMetadataFor($className); + return $this->validator->getMetadataFor($className); } /** @@ -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); @@ -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( @@ -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 * @@ -471,7 +499,6 @@ protected function parseTransformers(array $transformers) $result[] = $item; } - return $result; } @@ -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; diff --git a/README.md b/README.md index 5b92cd0..5046d2d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/Resources/public/js/constraints/Choice.js b/Resources/public/js/constraints/Choice.js index fade18b..f2ce4eb 100644 --- a/Resources/public/js/constraints/Choice.js +++ b/Resources/public/js/constraints/Choice.js @@ -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); diff --git a/Tests/Fixtures/Entity.php b/Tests/Fixtures/Entity.php index 7729800..5ccf618 100644 --- a/Tests/Fixtures/Entity.php +++ b/Tests/Fixtures/Entity.php @@ -74,7 +74,7 @@ public function setName($name) /** * @return bool - * @Assert\True(message = "wrong_name") + * @Assert\IsTrue(message = "wrong_name") */ public function isNameLegal() { diff --git a/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php b/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php index aaca67c..3a0bed1 100644 --- a/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php +++ b/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php @@ -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; @@ -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; @@ -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' )) ) @@ -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' )) ) @@ -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')), @@ -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')), ) diff --git a/Tests/TestBundles/DefaultTestBundle/Entity/BasicConstraintsEntity.php b/Tests/TestBundles/DefaultTestBundle/Entity/BasicConstraintsEntity.php index 729efbb..8219c0a 100644 --- a/Tests/TestBundles/DefaultTestBundle/Entity/BasicConstraintsEntity.php +++ b/Tests/TestBundles/DefaultTestBundle/Entity/BasicConstraintsEntity.php @@ -352,7 +352,7 @@ public function getDatetime() /** * @return bool - * @Assert\True(message="true_value") + * @Assert\IsTrue(message="true_value") */ public function isTrue() { @@ -361,7 +361,7 @@ public function isTrue() /** * @return bool - * @Assert\False(message="false_value") + * @Assert\IsFalse(message="false_value") */ public function isFalse() { @@ -370,7 +370,7 @@ public function isFalse() /** * @return bool - * @Assert\Null(message="null_{{ value }}") + * @Assert\IsNull(message="null_{{ value }}") */ public function isNull() { diff --git a/Tests/TestBundles/DefaultTestBundle/Entity/CustomizationEntity.php b/Tests/TestBundles/DefaultTestBundle/Entity/CustomizationEntity.php index 9b8ba1f..ff09310 100644 --- a/Tests/TestBundles/DefaultTestBundle/Entity/CustomizationEntity.php +++ b/Tests/TestBundles/DefaultTestBundle/Entity/CustomizationEntity.php @@ -189,7 +189,7 @@ public function setEmail($email) /** * @return bool * - * @Assert\True( + * @Assert\IsTrue( * message="getter_message", * groups={"groups_callback"} * ) diff --git a/Tests/TestBundles/DefaultTestBundle/Entity/TestEntity.php b/Tests/TestBundles/DefaultTestBundle/Entity/TestEntity.php index bad2909..3b9aec7 100644 --- a/Tests/TestBundles/DefaultTestBundle/Entity/TestEntity.php +++ b/Tests/TestBundles/DefaultTestBundle/Entity/TestEntity.php @@ -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" * ) */ diff --git a/Tests/TestBundles/DefaultTestBundle/Entity/TestSubEntity.php b/Tests/TestBundles/DefaultTestBundle/Entity/TestSubEntity.php index a0a6329..2d7c52e 100644 --- a/Tests/TestBundles/DefaultTestBundle/Entity/TestSubEntity.php +++ b/Tests/TestBundles/DefaultTestBundle/Entity/TestSubEntity.php @@ -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" * ) */ diff --git a/Tests/TestBundles/DefaultTestBundle/Twig/Extension/TestTwigExtension.php b/Tests/TestBundles/DefaultTestBundle/Twig/Extension/TestTwigExtension.php index 0ede9fe..deac8a4 100644 --- a/Tests/TestBundles/DefaultTestBundle/Twig/Extension/TestTwigExtension.php +++ b/Tests/TestBundles/DefaultTestBundle/Twig/Extension/TestTwigExtension.php @@ -12,9 +12,6 @@ */ class TestTwigExtension extends \Twig_Extension { - /** @var \Twig_Environment */ - protected $env; - /** * @var Kernel */ @@ -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')), ); } diff --git a/Tests/app/AppKernel.php b/Tests/app/AppKernel.php index 710d0a5..92712ad 100644 --- a/Tests/app/AppKernel.php +++ b/Tests/app/AppKernel.php @@ -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 diff --git a/Tests/app/Resources/config.php b/Tests/app/Resources/config.php index 684628b..a2b5161 100644 --- a/Tests/app/Resources/config.php +++ b/Tests/app/Resources/config.php @@ -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( diff --git a/Twig/Extension/JsFormValidatorTwigExtension.php b/Twig/Extension/JsFormValidatorTwigExtension.php index 4950216..a1f16fa 100644 --- a/Twig/Extension/JsFormValidatorTwigExtension.php +++ b/Twig/Extension/JsFormValidatorTwigExtension.php @@ -12,17 +12,6 @@ */ class JsFormValidatorTwigExtension extends \Twig_Extension { - /** @var \Twig_Environment */ - protected $env; - - /** - * @param \Twig_Environment $environment - */ - public function initRuntime(\Twig_Environment $environment) - { - $this->env = $environment; - } - /** * @var JsFormValidatorFactory */ @@ -53,8 +42,12 @@ public function __construct(JsFormValidatorFactory $factory) public function getFunctions() { return array( - 'init_js_validation' => new \Twig_Function_Method($this, 'getJsValidator', array('is_safe' => array('html'))), - 'js_validator_config' => new \Twig_Function_Method($this, 'getConfig', array('is_safe' => array('html'))), + new \Twig_SimpleFunction('init_js_validation', array($this, 'getJsValidator'), array( + 'is_safe' => array('html') + )), + new \Twig_SimpleFunction('js_validator_config', array($this, 'getConfig'), array( + 'is_safe' => array('html') + )), ); } diff --git a/composer.json b/composer.json index feeee2d..f15740a 100644 --- a/composer.json +++ b/composer.json @@ -19,15 +19,15 @@ "require": { "php": ">=5.3.2", - "symfony/form": ">=2.3,<2.7", - "symfony/validator": "~2.3.0,>=2.3.19||~2.4.0,>=2.4.9||>=2.5.3,<2.7", - "symfony/symfony": "~2.3.0,>=2.3.19||^2.4,<2.6||~2.6.2" + "symfony/form": "^2.7", + "symfony/validator": "^2.7" }, "require-dev": { "doctrine/orm": ">=2.2.3", - "doctrine/doctrine-bundle": "~1.2", - "symfony/assetic-bundle": "^2.3", + "doctrine/doctrine-bundle": "~1.4", + "symfony/symfony": "^2.7", + "symfony/assetic-bundle": ">=2.5", "phpunit/phpunit": "3.7.*", "behat/mink-bundle": "dev-master", "behat/mink-selenium2-driver": "1.1.0", @@ -51,7 +51,7 @@ "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } } }