If no validation is necessary, the returned validator is empty.
public getBaseValidatorConjunction ( string $targetClassName, array $validationGroups = ['Default'] ) : |
||
$targetClassName | string | Fully qualified class name of the target class, ie. the class which should be validated |
$validationGroups | array | The validation groups to build the validator for |
return | The validator conjunction |
/** * Add a domain record * * @param string $siteNodeName The nodeName of the site rootNode, e.g. "neostypo3org" * @param string $hostname The hostname to match on, e.g. "flow.neos.io" * @param string $scheme The scheme for linking (http/https) * @param integer $port The port for linking (0-49151) * @return void */ public function addCommand($siteNodeName, $hostname, $scheme = null, $port = null) { $site = $this->siteRepository->findOneByNodeName($siteNodeName); if (!$site instanceof Site) { $this->outputLine('<error>No site found with nodeName "%s".</error>', [$siteNodeName]); $this->quit(1); } $domains = $this->domainRepository->findByHostname($hostname); if ($domains->count() > 0) { $this->outputLine('<error>The host name "%s" is not unique.</error>', [$hostname]); $this->quit(1); } $domain = new Domain(); if ($scheme !== null) { $domain->setScheme($scheme); } if ($port !== null) { $domain->setPort($port); } $domain->setSite($site); $domain->setHostname($hostname); $domainValidator = $this->validatorResolver->getBaseValidatorConjunction(Domain::class); $result = $domainValidator->validate($domain); if ($result->hasErrors()) { foreach ($result->getFlattenedErrors() as $propertyName => $errors) { $firstError = array_pop($errors); $this->outputLine('<error>Validation failed for "' . $propertyName . '": ' . $firstError . '</error>'); $this->quit(1); } } $this->domainRepository->add($domain); $this->outputLine('Domain entry created.'); }
/** * Checks for a collection and if needed validates the items in the collection. * This is done with the specified element validator or a validator based on * the given element type and validation group. * * Either elementValidator or elementType must be given, otherwise validation * will be skipped. * * @param mixed $value A collection to be validated * @return void */ protected function isValid($value) { foreach ($value as $index => $collectionElement) { if (isset($this->options['elementValidator'])) { $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator'], $this->options['elementValidatorOptions']); } elseif (isset($this->options['elementType'])) { if (isset($this->options['validationGroups'])) { $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']); } else { $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']); } } else { return; } if ($collectionElementValidator instanceof ObjectValidatorInterface) { $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer); } $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement)); } }
/** * @test */ public function buildBaseValidatorCachesTheResultOfTheBuildBaseValidatorConjunctionCalls() { $mockReflectionService = $this->createMock(ReflectionService::class); $mockReflectionService->expects($this->at(0))->method('getAllImplementationClassNamesForInterface')->with(ValidatorInterface::class)->will($this->returnValue([])); $mockReflectionService->expects($this->at(1))->method('getAllImplementationClassNamesForInterface')->with(PolyTypeObjectValidatorInterface::class)->will($this->returnValue([])); $mockObjectManager = $this->createMock(ObjectManagerInterface::class); $mockObjectManager->expects($this->any())->method('get')->will($this->returnValue($mockReflectionService)); $this->validatorResolver->_set('objectManager', $mockObjectManager); $this->validatorResolver->_set('reflectionService', $mockReflectionService); $result1 = $this->validatorResolver->getBaseValidatorConjunction('TYPO3\\Virtual\\Foo'); $this->assertInstanceOf(ConjunctionValidator::class, $result1, '#1'); $result2 = $this->validatorResolver->getBaseValidatorConjunction('TYPO3\\Virtual\\Foo'); $this->assertSame($result1, $result2, '#2'); }
/** * Validates the given object and throws an exception if validation fails. * * @param object $object * @return void * @throws ObjectValidationFailedException * @api */ protected function validateObject($object) { $classSchema = $this->reflectionService->getClassSchema($object); $validator = $this->validatorResolver->getBaseValidatorConjunction($classSchema->getClassName()); if ($validator === null) { return; } $validationResult = $validator->validate($object); if ($validationResult->hasErrors()) { $errorMessages = ''; $allErrors = $validationResult->getFlattenedErrors(); foreach ($allErrors as $path => $errors) { $errorMessages .= $path . ':' . PHP_EOL; foreach ($errors as $error) { $errorMessages .= (string) $error . PHP_EOL; } } throw new ObjectValidationFailedException('An instance of "' . get_class($object) . '" failed to pass validation with ' . count($errors) . ' error(s): ' . PHP_EOL . $errorMessages, 1322585162); } }
/** * Validates the given object and throws an exception if validation fails. * * @param object $object * @param \SplObjectStorage $validatedInstancesContainer * @return void * @throws ObjectValidationFailedException */ protected function validateObject($object, \SplObjectStorage $validatedInstancesContainer) { $className = $this->entityManager->getClassMetadata(get_class($object))->getName(); $validator = $this->validatorResolver->getBaseValidatorConjunction($className, ['Persistence', 'Default']); if ($validator === null) { return; } $validator->setValidatedInstancesContainer($validatedInstancesContainer); $validationResult = $validator->validate($object); if ($validationResult->hasErrors()) { $errorMessages = ''; $errorCount = 0; $allErrors = $validationResult->getFlattenedErrors(); foreach ($allErrors as $path => $errors) { $errorMessages .= $path . ':' . PHP_EOL; foreach ($errors as $error) { $errorCount++; $errorMessages .= (string) $error . PHP_EOL; } } throw new ObjectValidationFailedException('An instance of "' . get_class($object) . '" failed to pass validation with ' . $errorCount . ' error(s): ' . PHP_EOL . $errorMessages, 1322585164); } }