Skip to content

Commit

Permalink
Fixed a bug where inherit_data was ignored and added check for cameli…
Browse files Browse the repository at this point in the history
…zed properties
  • Loading branch information
boekkooi committed Apr 28, 2015
1 parent b6fabb5 commit 5d7b509
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/Form/FormDataConstraintFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,22 @@ private function findPropertyConstraints(ClassMetadata $metadata, PropertyPathIn
$property = $propertyPath->getElement(0);
$constraintCollection = new ConstraintCollection();

// Ensure that the property has metadata
/*
* The problem with ensuring that we get the constraints of a property is:
* - If the property is private, some method is used to set it but we can really know what property is set by a method
* *also see PropertyAccessor::readProperty*
*
* To fix this (for now) we will check for the property using it's name and it's lower camel case name.
*/

// Find the 'probable' property with the constraint metadata
if (!$metadata->hasPropertyMetadata($property)) {
return $constraintCollection;
// If we can't find the property we will also check it's camelized version
$property = $this->camelize($property);

if (!$metadata->hasPropertyMetadata($property)) {
return $constraintCollection;
}
}

foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) {
Expand Down Expand Up @@ -121,10 +134,16 @@ private function getDataClass(FormInterface $form)
return $this->getDataClass($form->getParent());
}

// Now locate the root of the data
for ($i = $propertyPath->getLength(); $i != 0; $i--) {
$dataForm = $dataForm->getParent();
}

// If the root inherits data, then grab the parent
if ($dataForm->getConfig()->getInheritData()) {
$dataForm = $dataForm->getParent();
}

return $dataForm->getConfig()->getDataClass();
}

Expand All @@ -140,4 +159,15 @@ private function addCascadingValidConstraint(MemberMetadata $propertyMetadata, C
}
}
}

/**
* Returns the lowerCamelCase form of a string.
*
* @param string $string The string to camelize.
* @return string The camelized version of the string
*/
private function camelize($string)
{
return lcfirst(strtr(ucwords(strtr($string, array('_' => ' '))), array(' ' => '')));
}
}

0 comments on commit 5d7b509

Please sign in to comment.