[Flow] Re: Problem with duplicate validation

Sil via silvia.conti at bluewin.ch
Wed Oct 2 17:29:23 CEST 2013


Hello,
please find below the piece of code of my Validator to simplify my question.
Could you tell me if it's correct?

Thank you in advance.
Silvia

class CountryValidator extends \TYPO3\Flow\Validation\Validator\GenericObjectValidator {
    /**
     * @var \SplObjectStorage
     */
    
    /**
     * Allows to set a container to keep track of validated instances.
     *
     * @param \SplObjectStorage $validatedInstancesContainer A container to keep track of validated instances
     * @return void
     */    
    public function setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer) {
        $this->validatedInstancesContainer = $validatedInstancesContainer;
    }
    
    /**
     * Checks if the given value is valid according to the validator, and returns
     * the Error Messages object which occurred.
     *
     * @param mixed $value The value that should be validated
     * @return \TYPO3\Flow\Error\Result
     */
     
    public function validate($value) {
        $this->result = new \TYPO3\Flow\Error\Result();
        if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
            if (!is_object($value)) {
                $this->addError('Object expected, %1$s given.', 1241099149, array(gettype($value)));
            } elseif ($this->isValidatedAlready($value) === FALSE) {
                $this->isValid($value);
            }
        }

        return $this->result;
    }

    /**
     * @param object $object
     * @return boolean
     */
    
    protected function isValidatedAlready($object) {
        if ($this->validatedInstancesContainer === NULL) {
            $this->validatedInstancesContainer = new \SplObjectStorage();
        }
        if ($this->validatedInstancesContainer->contains($object)) {
            return TRUE;
        } else {
            $this->validatedInstancesContainer->attach($object);
            return FALSE;
        }
    }
    
     /**
      * Checks if the given value is a Country.
      *
      * @param mixed $value The value that should be validated
      * @return void
      * @api
      */
    protected function isValid($value) {
        $validator = new \TYPO3\Flow\Validation\Validator\RegularExpressionValidator(array(
                'regularExpression' => "/^[A-Z]/"));
        $result = $validator->validate($value->getName());
        if ($result->hasErrors()) {
            $this->addError('Country name: "%s" must begin with capital letter', 125, array($value->getName()));
        }        
    }
}



Quote: Sil via (sconti) wrote on Fri, 27 September 2013 15:34
----------------------------------------------------
> Hello,
> I implemented a Validator for a model class (Country), I stored it in Validator directory and it is called automatically. The problem is that it is called twice, I followed the instructions contained in Definitive Guide "Avoid Duplicate Validation" and I declared my Validator like below:
> 
> class CountryValidator extends \TYPO3\Flow\Validation\Validator\GenericObjectValidator {
> }
> 
> overriding only the isValid() method.
> 
> But still the validation is called twice. It seems that the InstanceContainier is always NULL and every time re-created.
> 
> Could you help me?
> 
> Thanks
> Silvia
> 
----------------------------------------------------




More information about the Flow mailing list