Skip to content

Commit 78a22d7

Browse files
committed
[Form] add support for Length and Range constraint in order to replace MaxLength, MinLength, Max and Min constraints in next release (2.3)
1 parent 3f89d88 commit 78a22d7

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ public function guessTypeForConstraint(Constraint $constraint)
155155

156156
case 'Symfony\Component\Validator\Constraints\MaxLength':
157157
case 'Symfony\Component\Validator\Constraints\MinLength':
158+
case 'Symfony\Component\Validator\Constraints\Length':
158159
case 'Symfony\Component\Validator\Constraints\Regex':
159160
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
160161

161162
case 'Symfony\Component\Validator\Constraints\Min':
162163
case 'Symfony\Component\Validator\Constraints\Max':
164+
case 'Symfony\Component\Validator\Constraints\Range':
163165
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
164166

165167
case 'Symfony\Component\Validator\Constraints\MinCount':
@@ -206,6 +208,12 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
206208
case 'Symfony\Component\Validator\Constraints\MaxLength':
207209
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
208210

211+
case 'Symfony\Component\Validator\Constraints\Length':
212+
if (is_numeric($constraint->max)) {
213+
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
214+
}
215+
break;
216+
209217
case 'Symfony\Component\Validator\Constraints\Type':
210218
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
211219
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
@@ -214,6 +222,12 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
214222

215223
case 'Symfony\Component\Validator\Constraints\Max':
216224
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
225+
226+
case 'Symfony\Component\Validator\Constraints\Range':
227+
if (is_numeric($constraint->max)) {
228+
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
229+
}
230+
break;
217231
}
218232

219233
return null;
@@ -232,6 +246,12 @@ public function guessPatternForConstraint(Constraint $constraint)
232246
case 'Symfony\Component\Validator\Constraints\MinLength':
233247
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);
234248

249+
case 'Symfony\Component\Validator\Constraints\Length':
250+
if (is_numeric($constraint->min)) {
251+
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
252+
}
253+
break;
254+
235255
case 'Symfony\Component\Validator\Constraints\Regex':
236256
$htmlPattern = $constraint->getHtmlPattern();
237257

@@ -243,6 +263,12 @@ public function guessPatternForConstraint(Constraint $constraint)
243263
case 'Symfony\Component\Validator\Constraints\Min':
244264
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
245265

266+
case 'Symfony\Component\Validator\Constraints\Range':
267+
if (is_numeric($constraint->min)) {
268+
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->min)), Guess::LOW_CONFIDENCE);
269+
}
270+
break;
271+
246272
case 'Symfony\Component\Validator\Constraints\Type':
247273
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
248274
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Validator;
13+
14+
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
15+
use Symfony\Component\Form\Guess\Guess;
16+
use Symfony\Component\Validator\Constraints\Length;
17+
use Symfony\Component\Validator\Constraints\Type;
18+
19+
/**
20+
* @author franek <[email protected]>
21+
*/
22+
class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
23+
{
24+
private $typeGuesser;
25+
26+
public function setUp()
27+
{
28+
if (!class_exists('Symfony\Component\Validator\Constraint')) {
29+
$this->markTestSkipped('The "Validator" component is not available');
30+
}
31+
32+
$metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
33+
34+
$this->typeGuesser = new ValidatorTypeGuesser($metadataFactory);
35+
}
36+
37+
public function testGuessMaxLengthForConstraintWithMaxValue()
38+
{
39+
$constraint = new Length(array('max' => '2'));
40+
41+
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
42+
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
43+
$this->assertEquals(2, $result->getValue());
44+
$this->assertEquals(Guess::HIGH_CONFIDENCE, $result->getConfidence());
45+
}
46+
47+
public function testGuessMaxLengthForConstraintWithMinValue()
48+
{
49+
$constraint = new Length(array('min' => '2'));
50+
51+
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
52+
$this->assertNull($result);
53+
}
54+
55+
/**
56+
* @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType
57+
*/
58+
public function testGuessMaxLengthForConstraintWithType($type)
59+
{
60+
$constraint = new Type($type);
61+
62+
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
63+
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
64+
$this->assertEquals(null, $result->getValue());
65+
$this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence());
66+
}
67+
68+
public static function dataProviderTestGuessMaxLengthForConstraintWithType()
69+
{
70+
return array (
71+
array('double'),
72+
array('float'),
73+
array('numeric'),
74+
array('real')
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)