Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Validator] Added hasser support for entity method validation #9097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion 5 src/Symfony/Component/Validator/Mapping/GetterMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ public function __construct($class, $property)
{
$getMethod = 'get'.ucfirst($property);
$isMethod = 'is'.ucfirst($property);
$hasMethod = 'has'.ucfirst($property);

if (method_exists($class, $getMethod)) {
$method = $getMethod;
} elseif (method_exists($class, $isMethod)) {
$method = $isMethod;
} elseif (method_exists($class, $hasMethod)) {
$method = $hasMethod;
} else {
throw new ValidatorException(sprintf('Neither method %s nor %s exists in class %s', $getMethod, $isMethod, $class));
throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
}

parent::__construct($class, $method, $property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public function loadClassMetadata(ClassMetadata $metadata)

$metadata->addConstraint($constraint);
} elseif ($constraint instanceof Constraint) {
if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
$metadata->addGetterConstraint(lcfirst($matches[2]), $constraint);
} else {
throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get" or "is".', $className, $method->name));
throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
}
}

Expand Down
16 changes: 16 additions & 0 deletions 16 src/Symfony/Component/Validator/Tests/Fixtures/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public function getLastName()
return $this->lastName;
}

/**
* @Assert\True
*/
public function isValid()
{
return 'valid';
}

/**
* @Assert\True
*/
public function hasPermissions()
{
return 'permissions';
}

public function getData()
{
return 'Overridden data';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,20 @@ public function testGetPropertyValueFromOverriddenPublicGetter()

$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
}

public function testGetPropertyValueFromIsser()
{
$entity = new Entity();
$metadata = new GetterMetadata(self::CLASSNAME, 'valid');

$this->assertEquals('valid', $metadata->getPropertyValue($entity));
}

public function testGetPropertyValueFromHasser()
{
$entity = new Entity();
$metadata = new GetterMetadata(self::CLASSNAME, 'permissions');

$this->assertEquals('permissions', $metadata->getPropertyValue($entity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
Expand Down Expand Up @@ -67,6 +68,8 @@ public function testLoadClassMetadata()
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());

// load reflection class so that the comparison passes
$expected->getReflectionClass();
Expand Down Expand Up @@ -134,6 +137,8 @@ public function testLoadClassMetadataAndMerge()
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());

// load reflection class so that the comparison passes
$expected->getReflectionClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
Expand Down Expand Up @@ -69,6 +70,8 @@ public function testLoadClassMetadata()
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());

$this->assertEquals($expected, $metadata);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
Expand Down Expand Up @@ -86,6 +87,8 @@ public function testLoadClassMetadata()
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());

$this->assertEquals($expected, $metadata);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
<getter property="lastName">
<constraint name="NotNull" />
</getter>
<getter property="valid">
<constraint name="True" />
</getter>
<getter property="permissions">
<constraint name="True" />
</getter>
</class>

<class name="Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Symfony\Component\Validator\Tests\Fixtures\Entity:
getters:
lastName:
- NotNull: ~
valid:
- "True": ~
permissions:
- "True": ~

Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity:
group_sequence_provider: true
Morty Proxy This is a proxified and sanitized view of the page, visit original site.