Neos\Flow\Validation\Validator\GenericObjectValidator::addPropertyValidator PHP Method

addPropertyValidator() public method

Adds the given validator for validation of the specified property.
public addPropertyValidator ( string $propertyName, Neos\Flow\Validation\Validator\ValidatorInterface $validator ) : void
$propertyName string Name of the property to validate
$validator Neos\Flow\Validation\Validator\ValidatorInterface The property validator
return void
    public function addPropertyValidator($propertyName, ValidatorInterface $validator)
    {
        if (!isset($this->propertyValidators[$propertyName])) {
            $this->propertyValidators[$propertyName] = new \SplObjectStorage();
        }
        $this->propertyValidators[$propertyName]->attach($validator);
    }

Usage Example

 /**
  * @test
  */
 public function collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping()
 {
     $classNameA = 'A' . md5(uniqid(mt_rand(), true));
     eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
     $classNameB = 'B' . md5(uniqid(mt_rand(), true));
     eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
     $A = new $classNameA();
     $B = new $classNameB();
     $A->b = [$B];
     $B->a = $A;
     $B->c = [$A];
     $this->mockValidatorResolver->expects($this->any())->method('createValidator')->with('Integer')->will($this->returnValue(new IntegerValidator()));
     $this->mockValidatorResolver->expects($this->any())->method('buildBaseValidatorConjunction')->will($this->returnValue(new GenericObjectValidator()));
     // Create validators
     $aValidator = new GenericObjectValidator([]);
     $this->validator->_set('options', ['elementValidator' => 'Integer', 'elementValidatorOptions' => []]);
     $integerValidator = new IntegerValidator([]);
     // Add validators to properties
     $aValidator->addPropertyValidator('b', $this->validator);
     $aValidator->addPropertyValidator('integer', $integerValidator);
     $result = $aValidator->validate($A)->getFlattenedErrors();
     $this->assertEquals('A valid integer number is expected.', $result['b.0'][0]->getMessage());
 }
All Usage Examples Of Neos\Flow\Validation\Validator\GenericObjectValidator::addPropertyValidator