本文整理汇总了PHP中Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor方法的典型用法代码示例。如果您正苦于以下问题:PHP ValidatorInterface::getMetadataFor方法的具体用法?PHP ValidatorInterface::getMetadataFor怎么用?PHP ValidatorInterface::getMetadataFor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Validator\Validator\ValidatorInterface
的用法示例。
在下文中一共展示了ValidatorInterface::getMetadataFor方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (empty($options['data_class'])) {
return;
}
$className = $options['data_class'];
if (!$this->doctrineHelper->isManageableEntity($className)) {
return;
}
if (!$this->entityConfigProvider->hasConfig($className)) {
return;
}
$uniqueKeys = $this->entityConfigProvider->getConfig($className)->get('unique_key');
if (empty($uniqueKeys)) {
return;
}
/* @var \Symfony\Component\Validator\Mapping\ClassMetadata $validatorMetadata */
$validatorMetadata = $this->validator->getMetadataFor($className);
foreach ($uniqueKeys['keys'] as $uniqueKey) {
$fields = $uniqueKey['key'];
$labels = array_map(function ($fieldName) use($className) {
$label = $this->entityConfigProvider->getConfig($className, $fieldName)->get('label');
return $this->translator->trans($label);
}, $fields);
$constraint = new UniqueEntity(['fields' => $fields, 'errorPath' => '', 'message' => $this->translator->transChoice('oro.entity.validation.unique_field', sizeof($fields), ['%field%' => implode(', ', $labels)])]);
$validatorMetadata->addConstraint($constraint);
}
}
示例2: mergeConstraints
/**
* Merges constraints from the form with constraints in the class metaData
*
* @param FormInterface $form
*/
public function mergeConstraints(FormInterface &$form)
{
$metaData = null;
$dataClass = $form->getConfig()->getDataClass();
if ($dataClass != '') {
$metaData = $this->validator->getMetadataFor($dataClass);
}
if ($metaData instanceof ClassMetadata) {
/**
* @var FormInterface $child
*/
foreach ($form->all() as $child) {
$options = $child->getConfig()->getOptions();
$name = $child->getConfig()->getName();
$type = $child->getConfig()->getType()->getName();
if (isset($options['constraints'])) {
$existingConstraints = $options['constraints'];
$extractedConstraints = $this->extractPropertyConstraints($name, $metaData);
// Merge all constraints
$options['constraints'] = array_merge($existingConstraints, $extractedConstraints);
}
$form->add($name, $type, $options);
}
}
}
示例3: getColumnOptions
/**
* @param string $columnName
* @param string $entityName
* @param array $column
*
* @return array
*/
public function getColumnOptions($columnName, $entityName, $column)
{
/** @var ValidatorInterface $validatorMetadata */
$validatorMetadata = $this->validator->getMetadataFor($entityName);
foreach ($this->guessers as $guesser) {
$options = $guesser->guessColumnOptions($columnName, $entityName, $column);
if (!empty($options)) {
if ($validatorMetadata->hasPropertyMetadata($columnName)) {
$options[Configuration::BASE_CONFIG_KEY]['validation_rules'] = $this->getValidationRules($validatorMetadata, $columnName);
}
return $options;
}
}
return [];
}
示例4: isPropertyRequired
/**
* Is required property
*
* @param \ReflectionProperty $property
* @param array $groups
*
* @return bool
*/
private function isPropertyRequired(\ReflectionProperty $property, array $groups)
{
if (!$this->validator) {
// Can not check...
return true;
}
$metadata = $this->validator->getMetadataFor($property->getDeclaringClass()->getName());
if (!$metadata instanceof ClassMetadata) {
return true;
}
$propertyMetadata = $metadata->getPropertyMetadata($property->getName());
if ($propertyMetadata) {
// @todo: merge all metadata?
$propertyMetadata = array_pop($propertyMetadata);
foreach ($groups as $group) {
$constraints = $propertyMetadata->findConstraints($group);
foreach ($constraints as $constraint) {
if ($constraint instanceof NotBlank) {
return true;
}
}
}
}
return false;
}
示例5: getConstraintsForField
/**
* @param string $name
* @throws \NForms\Exceptions\MetadataException
* @return \Symfony\Component\Validator\Constraint[]
*/
public function getConstraintsForField($name)
{
/** @var ClassMetadata $metadata */
$metadata = $this->validator->getMetadataFor($this->class);
// if (!property_exists($this->getClass(), $name)) {
// throw new MetadataException("Field '$name' was not found in the class '{$this->getClass()}'.");
// }
$constraints = array();
$validationGroups = $this->validationGroups ?: array(Constraint::DEFAULT_GROUP);
foreach ($validationGroups as $validationGroup) {
/** @var PropertyMetadata $propertyMetadata */
foreach ($metadata->getPropertyMetadata($name) as $propertyMetadata) {
$constraints += $propertyMetadata->findConstraints($validationGroup);
}
}
$constraints = array_unique($constraints, SORT_REGULAR);
// remove duplicates
return $constraints;
}
示例6: __construct
public function __construct(ValidatorInterface $validator)
{
$metadata = $validator->getMetadataFor('Symfony\\Component\\Form\\Form');
// Register the form constraints in the validator programmatically.
// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml
/* @var $metadata ClassMetadata */
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());
$this->validator = $validator;
}
示例7: getEntityConstraints
/**
* @param FormInterface $form
*
* @return array
*/
private function getEntityConstraints(FormInterface $form)
{
$config = $form->getParent()->getConfig();
if (!$config->hasOption('data_class') || !class_exists($config->getOption('data_class'))) {
return [];
}
$constraints = [];
/** @var ClassMetadata $metadata */
$metadata = $this->validator->getMetadataFor($config->getDataClass());
/** @var PropertyMetadata[] $properties */
$properties = $metadata->getPropertyMetadata($form->getName());
foreach ($properties as $property) {
$constraints = array_merge($constraints, $property->findConstraints($metadata->getDefaultGroup()));
}
return $constraints;
}
示例8: __construct
/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
*
* @throws UnexpectedTypeException If $validator is invalid
*/
public function __construct($validator)
{
// 2.5 API
if ($validator instanceof ValidatorInterface) {
$metadata = $validator->getMetadataFor('Symfony\\Component\\Form\\Form');
// 2.4 API
} elseif ($validator instanceof LegacyValidatorInterface) {
$metadata = $validator->getMetadataFactory()->getMetadataFor('Symfony\\Component\\Form\\Form');
} else {
throw new UnexpectedTypeException($validator, 'Symfony\\Component\\Validator\\Validator\\ValidatorInterface or Symfony\\Component\\Validator\\ValidatorInterface');
}
// Register the form constraints in the validator programmatically.
// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml
/** @var $metadata ClassMetadata */
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());
$this->validator = $validator;
}
示例9: __construct
/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
*
* @throws UnexpectedTypeException If $validator is invalid
*/
public function __construct($validator)
{
// 2.5 API
if ($validator instanceof ValidatorInterface) {
$metadata = $validator->getMetadataFor('Symfony\\Component\\Form\\Form');
// 2.4 API
} elseif ($validator instanceof LegacyValidatorInterface) {
@trigger_error('Passing an instance of Symfony\\Component\\Validator\\ValidatorInterface as argument to the ' . __METHOD__ . ' method is deprecated since version 2.8 and will be removed in 3.0. Use an implementation of Symfony\\Component\\Validator\\Validator\\ValidatorInterface instead', E_USER_DEPRECATED);
$metadata = $validator->getMetadataFactory()->getMetadataFor('Symfony\\Component\\Form\\Form');
} else {
throw new UnexpectedTypeException($validator, 'Symfony\\Component\\Validator\\Validator\\ValidatorInterface or Symfony\\Component\\Validator\\ValidatorInterface');
}
// Register the form constraints in the validator programmatically.
// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml
/* @var $metadata ClassMetadata */
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());
$this->validator = $validator;
}
示例10: attachInlineValidator
/**
* Attach the inline validator to the model metadata, this must be done once per admin.
*/
protected function attachInlineValidator()
{
$admin = $this;
// add the custom inline validation option
// TODO: Remove conditional method when bumping requirements to SF 2.5+
if (method_exists($this->validator, 'getMetadataFor')) {
$metadata = $this->validator->getMetadataFor($this->getClass());
} else {
$metadata = $this->validator->getMetadataFactory()->getMetadataFor($this->getClass());
}
$metadata->addConstraint(new InlineConstraint(array('service' => $this, 'method' => function (ErrorElement $errorElement, $object) use($admin) {
/* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
// This avoid the main validation to be cascaded to children
// The problem occurs when a model Page has a collection of Page as property
if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
return;
}
$admin->validate($errorElement, $object);
foreach ($admin->getExtensions() as $extension) {
$extension->validate($admin, $errorElement, $object);
}
}, 'serializingWarning' => true)));
}
示例11: getMetadataFor
public function getMetadataFor($value)
{
return $this->wrappedValidator->getMetadataFor($value);
}