Merge branch '2.2' into 2.3
* 2.2: [Form] add support for Length and Range constraint in order to replace MaxLength, MinLength, Max and Min constraints in next release (2.3) [Form] check the required output timezone against the actual timezone of the input datetime object, rather than the expected timezone supplied
This commit is contained in:
commit
b0687c8d81
@ -58,7 +58,7 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
|
||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
if ($this->outputTimezone !== $this->inputTimezone) {
|
||||
if ($this->outputTimezone !== $dateTime->getTimezone()->getName()) {
|
||||
try {
|
||||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
|
||||
} catch (\Exception $e) {
|
||||
|
@ -147,11 +147,13 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\MaxLength':
|
||||
case 'Symfony\Component\Validator\Constraints\MinLength':
|
||||
case 'Symfony\Component\Validator\Constraints\Length':
|
||||
case 'Symfony\Component\Validator\Constraints\Regex':
|
||||
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Min':
|
||||
case 'Symfony\Component\Validator\Constraints\Max':
|
||||
case 'Symfony\Component\Validator\Constraints\Range':
|
||||
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\MinCount':
|
||||
@ -198,6 +200,12 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
case 'Symfony\Component\Validator\Constraints\MaxLength':
|
||||
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Length':
|
||||
if (is_numeric($constraint->max)) {
|
||||
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Type':
|
||||
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
||||
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
||||
@ -206,6 +214,12 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Max':
|
||||
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Range':
|
||||
if (is_numeric($constraint->max)) {
|
||||
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -224,6 +238,12 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
case 'Symfony\Component\Validator\Constraints\MinLength':
|
||||
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Length':
|
||||
if (is_numeric($constraint->min)) {
|
||||
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Regex':
|
||||
$htmlPattern = $constraint->getHtmlPattern();
|
||||
|
||||
@ -235,6 +255,12 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
case 'Symfony\Component\Validator\Constraints\Min':
|
||||
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Range':
|
||||
if (is_numeric($constraint->min)) {
|
||||
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->min)), Guess::LOW_CONFIDENCE);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Type':
|
||||
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
||||
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
||||
|
@ -65,6 +65,7 @@ class DateTimeToRfc3339TransformerTest extends DateTimeTestCase
|
||||
// format without seconds, as appears in some browsers
|
||||
array('UTC', 'UTC', '2010-02-03 04:05:00 UTC', '2010-02-03T04:05Z'),
|
||||
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:00 America/New_York', '2010-02-03T17:05+08:00'),
|
||||
array('Europe/Amsterdam', 'Europe/Amsterdam', '2013-08-21 10:30:00 Europe/Amsterdam', '2013-08-21T08:30:00Z')
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Tests\Extension\Validator;
|
||||
|
||||
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
|
||||
use Symfony\Component\Form\Guess\Guess;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
use Symfony\Component\Validator\Constraints\Type;
|
||||
|
||||
/**
|
||||
* @author franek <franek@chicour.net>
|
||||
*/
|
||||
class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $typeGuesser;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!class_exists('Symfony\Component\Validator\Constraint')) {
|
||||
$this->markTestSkipped('The "Validator" component is not available');
|
||||
}
|
||||
|
||||
$metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
|
||||
|
||||
$this->typeGuesser = new ValidatorTypeGuesser($metadataFactory);
|
||||
}
|
||||
|
||||
public function testGuessMaxLengthForConstraintWithMaxValue()
|
||||
{
|
||||
$constraint = new Length(array('max' => '2'));
|
||||
|
||||
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
|
||||
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
|
||||
$this->assertEquals(2, $result->getValue());
|
||||
$this->assertEquals(Guess::HIGH_CONFIDENCE, $result->getConfidence());
|
||||
}
|
||||
|
||||
public function testGuessMaxLengthForConstraintWithMinValue()
|
||||
{
|
||||
$constraint = new Length(array('min' => '2'));
|
||||
|
||||
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType
|
||||
*/
|
||||
public function testGuessMaxLengthForConstraintWithType($type)
|
||||
{
|
||||
$constraint = new Type($type);
|
||||
|
||||
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
|
||||
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
|
||||
$this->assertEquals(null, $result->getValue());
|
||||
$this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence());
|
||||
}
|
||||
|
||||
public static function dataProviderTestGuessMaxLengthForConstraintWithType()
|
||||
{
|
||||
return array (
|
||||
array('double'),
|
||||
array('float'),
|
||||
array('numeric'),
|
||||
array('real')
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user