Skip to content

Commit a4a758c

Browse files
committed
Merge pull request #106 from vaniocz/1.3
1.3 - Symfony 2.7 & 2.8
2 parents 40aafd5 + 7b6c771 commit a4a758c

15 files changed

Lines changed: 79 additions & 82 deletions

File tree

.travis.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
language: php
22

3-
php: [5.3,5.4,5.5,5.6]
3+
php: [5.3,5.4,5.5,5.6,7.0]
44

55
env:
6-
- SF_VERSION='~2.3.0,>=2.3.19'
7-
- SF_VERSION='~2.4.0,>=2.4.9'
8-
- SF_VERSION='~2.5.0,>=2.5.3'
9-
- SF_VERSION='~2.6.0,>=2.6.2'
6+
- SF_VERSION='~2.7.0'
7+
- SF_VERSION='~2.8.0'
108

119
before_script:
1210
- export WEB_FIXTURES_HOST=http://localhost/index.php
@@ -15,7 +13,6 @@ before_script:
1513
- export DISPLAY=:99.0
1614
- sleep 4
1715

18-
- Tests/app/switch_sf_version.sh "$SF_VERSION"
1916
- curl -sS https://getcomposer.org/installer | php
2017
- php -d memory_limit=-1 composer.phar require "symfony/symfony:${SF_VERSION}"
2118
- php -d memory_limit=-1 composer.phar install -n
@@ -32,6 +29,7 @@ before_script:
3229
- sudo apt-get update > /dev/null
3330
- sudo apt-get install -y --force-yes apache2 libapache2-mod-fastcgi > /dev/null
3431
# enable php-fpm
32+
- 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
3533
- sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
3634
- sudo a2enmod rewrite actions fastcgi alias
3735
- echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

Factory/JsFormValidatorFactory.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
use Fp\JsFormValidatorBundle\Form\Constraint\UniqueEntity;
66
use Fp\JsFormValidatorBundle\Model\JsConfig;
77
use Fp\JsFormValidatorBundle\Model\JsFormElement;
8+
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
89
use Symfony\Component\Form\DataTransformerInterface;
9-
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
10+
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToBooleanArrayTransformer;
11+
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToBooleanArrayTransformer;
1012
use Symfony\Component\Form\Form;
1113
use Symfony\Component\Form\FormInterface;
1214
use Symfony\Component\Translation\TranslatorInterface;
1315
use Symfony\Component\Validator\Constraint;
1416
use Symfony\Component\Validator\Mapping\ClassMetadata;
1517
use Symfony\Component\Validator\Mapping\GetterMetadata;
1618
use Symfony\Component\Validator\Mapping\PropertyMetadata;
17-
use Symfony\Component\Validator\ValidatorInterface;
19+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1820

1921
/**
2022
* This factory uses to parse a form to a tree of JsFormElement's
@@ -91,7 +93,7 @@ public function __construct(
9193
*/
9294
protected function getMetadataFor($className)
9395
{
94-
return $this->validator->getMetadataFactory()->getMetadataFor($className);
96+
return $this->validator->getMetadataFor($className);
9597
}
9698

9799
/**
@@ -247,7 +249,7 @@ public function createJsModel(Form $form)
247249
$conf->getOption('invalid_message'),
248250
$conf->getOption('invalid_message_parameters')
249251
);
250-
$model->transformers = $this->parseTransformers($form->getConfig()->getViewTransformers());
252+
$model->transformers = $this->parseTransformers($this->getViewTransformers($form));
251253
$model->cascade = $conf->getOption('cascade_validation');
252254
$model->bubbling = $conf->getOption('error_bubbling');
253255
$model->data = $this->getValidationData($form);
@@ -315,8 +317,8 @@ protected function getValidationData(Form $form)
315317
$parent = $form->getParent();
316318
if ($parent && null !== $parent->getConfig()->getDataClass()) {
317319
$classMetadata = $metadata = $this->getMetadataFor($parent->getConfig()->getDataClass());
318-
if ($classMetadata->hasMemberMetadatas($form->getName())) {
319-
$metadata = $classMetadata->getMemberMetadatas($form->getName());
320+
if ($classMetadata->hasPropertyMetadata($form->getName())) {
321+
$metadata = $classMetadata->getPropertyMetadata($form->getName());
320322
/** @var PropertyMetadata $item */
321323
foreach ($metadata as $item) {
322324
$this->composeValidationData(
@@ -448,6 +450,32 @@ protected function isProcessableElement($element)
448450
&& ('hidden' !== $element->getConfig()->getType()->getName());
449451
}
450452

453+
/**
454+
* Gets view transformers from the given form.
455+
* Merges in an extra Choice(s)ToBooleanArrayTransformer transformer in case of expanded choice.
456+
*
457+
* @param FormInterface $form
458+
*
459+
* @return array
460+
*/
461+
protected function getViewTransformers(FormInterface $form)
462+
{
463+
$config = $form->getConfig();
464+
$type = $config->getType()->getInnerType()->getName();
465+
$viewTransformers = $config->getViewTransformers();
466+
467+
// Choice(s)ToBooleanArrayTransformer was deprecated in SF2.7 in favor of CheckboxListMapper and RadioListMapper
468+
if ($type === 'choice' && $config->getOption('expanded')) {
469+
$choiceList = $config->getOption('choice_list');
470+
$transformer = $config->getOption('multiple')
471+
? @new ChoicesToBooleanArrayTransformer($choiceList)
472+
: @new ChoiceToBooleanArrayTransformer($choiceList, $config->getOption('placeholder'));
473+
array_unshift($viewTransformers, $transformer);
474+
}
475+
476+
return $viewTransformers;
477+
}
478+
451479
/**
452480
* Convert transformers objects to data arrays
453481
*
@@ -471,7 +499,6 @@ protected function parseTransformers(array $transformers)
471499

472500
$result[] = $item;
473501
}
474-
475502
return $result;
476503
}
477504

@@ -495,7 +522,7 @@ protected function getTransformerParam(DataTransformerInterface $transformer, $p
495522
} elseif (is_scalar($value) || is_array($value)) {
496523
$result = $value;
497524
} elseif ($value instanceof ChoiceListInterface) {
498-
$result = $value->getChoices();
525+
$result = array_values($value->getChoices());
499526
}
500527

501528
return $result;

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# FpJsFormValidatorBundle
2-
[![Build Status](https://travis-ci.org/formapro/JsFormValidatorBundle.svg?branch=1.3)](https://travis-ci.org/formapro/JsFormValidatorBundle)
2+
[![Build Status](https://travis-ci.org/formapro/JsFormValidatorBundle.svg?branch=master)](https://travis-ci.org/formapro/JsFormValidatorBundle)
33
[![Total Downloads](https://poser.pugx.org/fp/jsformvalidator-bundle/downloads.png)](https://packagist.org/packages/fp/jsformvalidator-bundle)
44

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

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

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

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

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

Resources/public/js/constraints/Choice.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ function SymfonyComponentValidatorConstraintsChoice() {
2828

2929
if (this.multiple) {
3030
if (invalidCnt) {
31-
while (invalidCnt--) {
32-
errors.push(this.multipleMessage.replace(
33-
'{{ value }}',
34-
FpJsBaseConstraint.formatValue(invalidList[invalidCnt])
35-
));
36-
}
31+
errors.push(this.multipleMessage.replace(
32+
'{{ value }}',
33+
FpJsBaseConstraint.formatValue(invalidList[0])
34+
));
3735
}
3836
if (!isNaN(this.min) && value.length < this.min) {
3937
errors.push(this.minMessage);

Tests/Fixtures/Entity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function setName($name)
7474

7575
/**
7676
* @return bool
77-
* @Assert\True(message = "wrong_name")
77+
* @Assert\IsTrue(message = "wrong_name")
7878
*/
7979
public function isNameLegal()
8080
{

Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
use Symfony\Component\Validator\Constraints\DateTime;
2525
use Symfony\Component\Validator\Constraints\Email;
2626
use Symfony\Component\Validator\Constraints\EqualTo;
27-
use Symfony\Component\Validator\Constraints\False;
2827
use Symfony\Component\Validator\Constraints\GreaterThan;
2928
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
3029
use Symfony\Component\Validator\Constraints\IdenticalTo;
3130
use Symfony\Component\Validator\Constraints\Ip;
31+
use Symfony\Component\Validator\Constraints\IsFalse;
32+
use Symfony\Component\Validator\Constraints\IsTrue;
3233
use Symfony\Component\Validator\Constraints\Length;
3334
use Symfony\Component\Validator\Constraints\LessThan;
3435
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
@@ -37,7 +38,6 @@
3738
use Symfony\Component\Validator\Constraints\NotIdenticalTo;
3839
use Symfony\Component\Validator\Constraints\Range;
3940
use Symfony\Component\Validator\Constraints\Time;
40-
use Symfony\Component\Validator\Constraints\True;
4141
use Symfony\Component\Validator\Constraints\Type;
4242
use Symfony\Component\Validator\Constraints\Url;
4343

@@ -293,10 +293,10 @@ public function transformersAction(Request $request, $isValid, $js)
293293
'checkbox',
294294
array(
295295
'constraints' => array(
296-
new True(array(
296+
new IsTrue(array(
297297
'message' => 'checkbox_false'
298298
)),
299-
new False(array(
299+
new IsFalse(array(
300300
'message' => 'checkbox_true'
301301
))
302302
)
@@ -307,10 +307,10 @@ public function transformersAction(Request $request, $isValid, $js)
307307
'radio',
308308
array(
309309
'constraints' => array(
310-
new True(array(
310+
new IsTrue(array(
311311
'message' => 'radio_false'
312312
)),
313-
new False(array(
313+
new IsFalse(array(
314314
'message' => 'radio_true'
315315
))
316316
)
@@ -469,7 +469,7 @@ public function emptyAction(
469469
'constraints' => array(
470470
new Email(array('message' => 'wrong_email')),
471471
new EqualTo(array('value' => 'asdf', 'message' => 'wrong_equal_to')),
472-
new False(array('message' => 'wrong_false')),
472+
new IsFalse(array('message' => 'wrong_false')),
473473
new GreaterThan(array('value' => 5, 'message' => 'wrong_greater_than')),
474474
new GreaterThanOrEqual(array('value' => 5, 'message' => 'wrong_greater_than_or_equal')),
475475
new IdenticalTo(array('value' => 5, 'message' => 'wrong_identical_to')),
@@ -491,7 +491,7 @@ public function emptyAction(
491491
new Time(array('message' => 'wrong_time')),
492492
new Date(array('message' => 'wrong_date')),
493493
new DateTime(array('message' => 'wrong_date_time')),
494-
new True(array('message' => 'wrong_true')),
494+
new IsTrue(array('message' => 'wrong_true')),
495495
new Type(array('type' => 'integer', 'message' => 'wrong_type')),
496496
new Url(array('message' => 'wrong_url')),
497497
)

Tests/TestBundles/DefaultTestBundle/Entity/BasicConstraintsEntity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public function getDatetime()
352352

353353
/**
354354
* @return bool
355-
* @Assert\True(message="true_value")
355+
* @Assert\IsTrue(message="true_value")
356356
*/
357357
public function isTrue()
358358
{
@@ -361,7 +361,7 @@ public function isTrue()
361361

362362
/**
363363
* @return bool
364-
* @Assert\False(message="false_value")
364+
* @Assert\IsFalse(message="false_value")
365365
*/
366366
public function isFalse()
367367
{
@@ -370,7 +370,7 @@ public function isFalse()
370370

371371
/**
372372
* @return bool
373-
* @Assert\Null(message="null_{{ value }}")
373+
* @Assert\IsNull(message="null_{{ value }}")
374374
*/
375375
public function isNull()
376376
{

Tests/TestBundles/DefaultTestBundle/Entity/CustomizationEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function setEmail($email)
189189
/**
190190
* @return bool
191191
*
192-
* @Assert\True(
192+
* @Assert\IsTrue(
193193
* message="getter_message",
194194
* groups={"groups_callback"}
195195
* )

Tests/TestBundles/DefaultTestBundle/Entity/TestEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public function setEmail($email)
116116

117117
/**
118118
* @return bool
119-
* @Assert\True(
119+
* @Assert\IsTrue(
120120
* message="getter_groups_array_message",
121121
* groups={"groups_array"}
122122
* )
123-
* @Assert\True(
123+
* @Assert\IsTrue(
124124
* message="getter_no_groups_message"
125125
* )
126126
*/

Tests/TestBundles/DefaultTestBundle/Entity/TestSubEntity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ public function getName()
7474

7575
/**
7676
* @return bool
77-
* @Assert\True(
77+
* @Assert\IsTrue(
7878
* message="sub_entity_getter_groups_child_message",
7979
* groups={"groups_child"}
8080
* )
81-
* @Assert\True(
81+
* @Assert\IsTrue(
8282
* message="sub_entity_getter_groups_array_message",
8383
* groups={"groups_array"}
8484
* )
85-
* @Assert\True(
85+
* @Assert\IsTrue(
8686
* message="sub_entity_getter_no_groups_message"
8787
* )
8888
*/

0 commit comments

Comments
 (0)