diff --git a/.gitignore b/.gitignore index 8e9e535..3f423da 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,7 @@ phpunit.xml ### NodeJS ### ############## node_modules/ + +### JQueryValidation ### +######################## +/.tmp/ diff --git a/.travis.yml b/.travis.yml index 52c1162..c9b0db3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,23 +10,42 @@ env: matrix: include: - - php: 5.6 - env: | - - SYMFONY_VERSION=2.7.* + # [PHPUnit] Test symfony versions - php: 5.3.3 env: | COMPOSER_FLAGS='--prefer-stable --prefer-lowest' PHPUNIT_COVERAGE='--coverage-text --coverage-clover=coverage.clover' + - php: 5.6 + env: | + SYMFONY_VERSION=2.7.* + - php: 5.6 + env: | + SYMFONY_VERSION=2.8.* + - php: 5.6 + env: | + SYMFONY_VERSION=3.0.* + # [BEHAT] Test 2.8 without additionals in FF - php: 5.4 env: | + SYMFONY_VERSION=2.8.* TEST_SCOPE='javascript' SYMFONY_ENVIRONMENT='test' SYMFONY__ENABLE_ADDITIONALS='false' addons: firefox: "42.0" + # [BEHAT] Test 2.3 with additionals in FF + - php: 5.4 + env: | + SYMFONY_VERSION=2.3.* + TEST_SCOPE='javascript' + SYMFONY_ENVIRONMENT='test' + SYMFONY__ENABLE_ADDITIONALS='true' + addons: + firefox: "42.0" + # [BEHAT] Test 3.0 with additionals in FF - php: 5.6 env: | - COMPOSER_FLAGS='--prefer-stable --prefer-lowest' + SYMFONY_VERSION=3.0.* TEST_SCOPE='javascript' SYMFONY_ENVIRONMENT='test' SYMFONY__ENABLE_ADDITIONALS='true' diff --git a/behat.yml.dist b/behat.yml.dist index 41d2889..86a8018 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -8,21 +8,21 @@ default: - FeatureContext - Behat\MinkExtension\Context\MinkContext filters: - tags: ~@serverSide&&~@additionals&&~@basic + tags: "~@serverSide&&~@additionals&&~@basic" javascript_basic: paths: [ %paths.base%/tests/Functional/features ] contexts: - FeatureContext - Behat\MinkExtension\Context\MinkContext filters: - tags: @basic + tags: "@basic" javascript_additionals: paths: [ %paths.base%/tests/Functional/features ] contexts: - FeatureContext - Behat\MinkExtension\Context\MinkContext filters: - tags: @additionals + tags: "@additionals" server_side: paths: [ %paths.base%/tests/Functional/features ] contexts: @@ -30,7 +30,7 @@ default: - serverSide: true - Behat\MinkExtension\Context\MinkContext filters: - tags: ~@clientSide + tags: "~@clientSide" extensions: Behat\MinkExtension: diff --git a/composer.json b/composer.json index 7f1e3bf..1f9e2a2 100644 --- a/composer.json +++ b/composer.json @@ -5,20 +5,21 @@ "description": "Jquery form validation bundle for symfony 2", "require": { "php": ">=5.3.3", - "symfony/framework-bundle": "~2.3", - "symfony/dependency-injection": "~2.3", - "symfony/form": "~2.3, >=2.3.4", - "symfony/validator": "~2.3", - "symfony/twig-bundle": "~2.3", - "symfony/twig-bridge": "~2.3", + "symfony/framework-bundle": "~2.3|~3.0", + "symfony/dependency-injection": "~2.3|~3.0", + "symfony/form": "~2.3, >=2.3.5|~3.0", + "symfony/validator": "~2.3|~3.0", + "symfony/twig-bundle": "~2.3|~3.0", + "symfony/twig-bridge": "~2.3|~3.0", "twig/twig": "~1.14, >=1.14.2", "doctrine/collections": "~1.0" }, "require-dev": { - "symfony/browser-kit": "~2.3", - "symfony/dom-crawler": "~2.3", - "symfony/finder": "~2.3", - "symfony/yaml": "~2.3", + "symfony/browser-kit": "~2.3|~3.0", + "symfony/dom-crawler": "~2.3|~3.0", + "symfony/finder": "~2.3|~3.0", + "symfony/yaml": "~2.3|~3.0", + "symfony/css-selector": "~2.3|~3.0", "phpunit/phpunit": "~4.7@stable", "symfony/phpunit-bridge": "~2.7@stable", @@ -46,5 +47,6 @@ "Tests\\Boekkooi\\Bundle\\JqueryValidationBundle\\": "tests/" } }, + "prefer-stable": true, "minimum-stability": "dev" } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6855f4d..e73d781 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,7 +2,6 @@ - diff --git a/src/Form/Extension/ButtonTypeExtension.php b/src/Form/Extension/ButtonTypeExtension.php index 0ae664b..74dbe02 100644 --- a/src/Form/Extension/ButtonTypeExtension.php +++ b/src/Form/Extension/ButtonTypeExtension.php @@ -5,6 +5,7 @@ use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\ClickableInterface; +use Symfony\Component\Form\Extension\Core\Type\ButtonType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; @@ -35,7 +36,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function getExtendedType() { - return 'button'; + return FormHelper::isSymfony3Compatible() ? ButtonType::class : 'button'; } protected function hasRuleBuilderContext(FormView $view) diff --git a/src/Form/Extension/FormTypeExtension.php b/src/Form/Extension/FormTypeExtension.php index 203eb89..e59ff42 100644 --- a/src/Form/Extension/FormTypeExtension.php +++ b/src/Form/Extension/FormTypeExtension.php @@ -10,6 +10,7 @@ use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper; use Boekkooi\Bundle\JqueryValidationBundle\Validator\ConstraintCollection; use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -150,7 +151,7 @@ public function configureOptions(OptionsResolver $resolver) */ public function getExtendedType() { - return 'form'; + return FormHelper::isSymfony3Compatible() ? 'Symfony\Component\Form\Extension\Core\Type\FormType' : 'form'; } /** diff --git a/src/Form/FormDataConstraintFinder.php b/src/Form/FormDataConstraintFinder.php index a510b2a..70b35ed 100644 --- a/src/Form/FormDataConstraintFinder.php +++ b/src/Form/FormDataConstraintFinder.php @@ -9,7 +9,7 @@ use Symfony\Component\Validator\Mapping\CascadingStrategy; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\MemberMetadata; -use Symfony\Component\Validator\MetadataFactoryInterface; +use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; /** * @author Warnar Boekkooi @@ -21,8 +21,19 @@ class FormDataConstraintFinder */ private $metadataFactory; - public function __construct(MetadataFactoryInterface $metadataFactory) + /** + * Constructor. + * @param MetadataFactoryInterface $metadataFactory + */ + public function __construct($metadataFactory) { + if ( + !$metadataFactory instanceof MetadataFactoryInterface && + !$metadataFactory instanceof \Symfony\Component\Validator\MetadataFactoryInterface + ) { + throw new \InvalidArgumentException('metadataFactory must be a instanceof MetadataFactoryInterface'); + } + $this->metadataFactory = $metadataFactory; } @@ -177,7 +188,7 @@ private function resolveDataClass(FormInterface $form) // Now locate the closest data class // TODO what is the length really for? - for ($i = $propertyPath->getLength(); $i != 0; $i--) { + for ($i = $propertyPath->getLength(); $i !== 0; $i--) { $dataForm = $dataForm->getParent(); # When a data class is found then use that form @@ -296,7 +307,7 @@ protected function findPropertyDataTypeInfo(MemberMetadata $propertyMetadata, $d } $type = strtolower($constraint->type); - $type = $type == 'boolean' ? 'bool' : $constraint->type; + $type = $type === 'boolean' ? 'bool' : $constraint->type; $isFunction = 'is_' . $type; $ctypeFunction = 'ctype_' . $type; if (function_exists($isFunction) || function_exists($ctypeFunction)) { diff --git a/src/Form/Rule/Mapping/MaxLengthRule.php b/src/Form/Rule/Mapping/MaxLengthRule.php index ab927fa..36d9d92 100644 --- a/src/Form/Rule/Mapping/MaxLengthRule.php +++ b/src/Form/Rule/Mapping/MaxLengthRule.php @@ -48,7 +48,13 @@ public function supports(Constraint $constraint, FormInterface $form) return false; } - return !($constraintClass === 'Symfony\Component\Validator\Constraints\Length' && $this->isType($form, 'choice')); + return !( + $constraintClass === 'Symfony\Component\Validator\Constraints\Length' && + (FormHelper::isSymfony3Compatible() ? + $this->isType($form, 'Symfony\Component\Form\Extension\Core\Type\ChoiceType') : + $this->isType($form, 'choice') + ) + ); } protected function isType(FormInterface $type, $typeName) diff --git a/src/Form/Rule/Mapping/MinLengthRule.php b/src/Form/Rule/Mapping/MinLengthRule.php index 96ef441..162ab34 100644 --- a/src/Form/Rule/Mapping/MinLengthRule.php +++ b/src/Form/Rule/Mapping/MinLengthRule.php @@ -7,8 +7,11 @@ use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection; use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage; use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints\Choice; +use Symfony\Component\Validator\Constraints\Length; /** * @author Warnar Boekkooi @@ -48,7 +51,13 @@ public function supports(Constraint $constraint, FormInterface $form) return false; } - return !($constraintClass === 'Symfony\Component\Validator\Constraints\Length' && $this->isType($form, 'choice')); + return !( + $constraintClass === 'Symfony\Component\Validator\Constraints\Length' && + (FormHelper::isSymfony3Compatible() ? + $this->isType($form, 'Symfony\Component\Form\Extension\Core\Type\ChoiceType') : + $this->isType($form, 'choice') + ) + ); } protected function isType(FormInterface $type, $typeName) diff --git a/src/Form/Util/FormHelper.php b/src/Form/Util/FormHelper.php index 7763af5..23a5b96 100644 --- a/src/Form/Util/FormHelper.php +++ b/src/Form/Util/FormHelper.php @@ -14,12 +14,33 @@ */ final class FormHelper { + public static function isSymfony3Compatible() + { + return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'); + } + + public static function isSymfony2Compatible() + { + return method_exists('Symfony\Component\Form\ResolvedFormTypeInterface', 'getName'); + } + public static function isType(ResolvedFormTypeInterface $type, $typeName) { do { - if ($type->getName() === $typeName) { + if ( + self::isSymfony3Compatible() && + get_class($type->getInnerType()) === $typeName + ) { return true; } + + if ( + self::isSymfony2Compatible() && + $type->getName() === $typeName + ) { + return true; + } + $type = $type->getParent(); } while ($type !== null); diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 0d8d64a..954c2aa 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -11,17 +11,17 @@ services: boekkooi.jquery_validation.form.form_extension: class: %boekkooi.jquery_validation.form.form_extension.class% arguments: - - @boekkooi.jquery_validation.rule_processor - - @boekkooi.jquery_validation.rule_compiler - - @boekkooi.jquery_validation.constraint_finder + - "@boekkooi.jquery_validation.rule_processor" + - "@boekkooi.jquery_validation.rule_compiler" + - "@boekkooi.jquery_validation.constraint_finder" - %boekkooi.jquery_validation.enabled% tags: - - { name: form.type_extension, alias: form } + - { name: form.type_extension, alias: form, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType } boekkooi.jquery_validation.form.button_extension: class: %boekkooi.jquery_validation.form.button_extension.class% tags: - - { name: form.type_extension, alias: button } + - { name: form.type_extension, alias: button, extended_type: Symfony\Component\Form\Extension\Core\Type\ButtonType } boekkooi.jquery_validation.rule_processor: class: %boekkooi.jquery_validation.rule_processor.class% @@ -36,4 +36,4 @@ services: boekkooi.jquery_validation.constraint_finder: class: %boekkooi.jquery_validation.constraint_finder.class% arguments: - - @validator.mapping.class_metadata_factory + - "@validator.mapping.class_metadata_factory"