[Validator] The default option name can now be omitted when defining constraints as annotations
This commit is contained in:
parent
599c86509e
commit
5499a29430
@ -87,8 +87,9 @@ abstract class Constraint
|
|||||||
$invalidOptions = array();
|
$invalidOptions = array();
|
||||||
$missingOptions = array_flip((array) $this->getRequiredOptions());
|
$missingOptions = array_flip((array) $this->getRequiredOptions());
|
||||||
|
|
||||||
if (is_array($options) && count($options) == 1 && isset($options['value'])) {
|
if (is_array($options) && count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
|
||||||
$options = $options['value'];
|
$options[$this->getDefaultOption()] = $options['value'];
|
||||||
|
unset($options['value']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($options) && count($options) > 0 && is_string(key($options))) {
|
if (is_array($options) && count($options) > 0 && is_string(key($options))) {
|
||||||
|
@ -15,6 +15,8 @@ use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
|
|||||||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
|
||||||
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValue;
|
||||||
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValueAsDefault;
|
||||||
|
|
||||||
class ConstraintTest extends \PHPUnit_Framework_TestCase
|
class ConstraintTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@ -71,6 +73,30 @@ class ConstraintTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('foo', $constraint->property2);
|
$this->assertEquals('foo', $constraint->property2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetDefaultPropertyDoctrineStylePlusOtherProperty()
|
||||||
|
{
|
||||||
|
$constraint = new ConstraintA(array('value' => 'foo', 'property1' => 'bar'));
|
||||||
|
|
||||||
|
$this->assertEquals('foo', $constraint->property2);
|
||||||
|
$this->assertEquals('bar', $constraint->property1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetDefaultPropertyDoctrineStyleWhenDefaultPropertyIsNamedValue()
|
||||||
|
{
|
||||||
|
$constraint = new ConstraintWithValueAsDefault(array('value' => 'foo'));
|
||||||
|
|
||||||
|
$this->assertEquals('foo', $constraint->value);
|
||||||
|
$this->assertNull($constraint->property);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDontSetDefaultPropertyIfValuePropertyExists()
|
||||||
|
{
|
||||||
|
$constraint = new ConstraintWithValue(array('value' => 'foo'));
|
||||||
|
|
||||||
|
$this->assertEquals('foo', $constraint->value);
|
||||||
|
$this->assertNull($constraint->property);
|
||||||
|
}
|
||||||
|
|
||||||
public function testSetUndefinedDefaultProperty()
|
public function testSetUndefinedDefaultProperty()
|
||||||
{
|
{
|
||||||
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
|
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
<?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\Validator\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/** @Annotation */
|
||||||
|
class ConstraintWithValue extends Constraint
|
||||||
|
{
|
||||||
|
public $property;
|
||||||
|
public $value;
|
||||||
|
|
||||||
|
public function getDefaultOption()
|
||||||
|
{
|
||||||
|
return 'property';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTargets()
|
||||||
|
{
|
||||||
|
return array(self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
<?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\Validator\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/** @Annotation */
|
||||||
|
class ConstraintWithValueAsDefault extends Constraint
|
||||||
|
{
|
||||||
|
public $property;
|
||||||
|
public $value;
|
||||||
|
|
||||||
|
public function getDefaultOption()
|
||||||
|
{
|
||||||
|
return 'value';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTargets()
|
||||||
|
{
|
||||||
|
return array(self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user