Webiny\Component\Entity\Attribute\Validation\ValidationException::addError PHP Method

addError() public method

This is useful when you are validating an array attribute which can have validators on every nested key. When validating a simple attribute with no nested values, use this method to set error message for the attribute itself.
public addError ( string $key, string | integer $message, null | array $params = null )
$key string Attribute name or nested attribute key
$message string | integer
$params null | array
    public function addError($key, $message, $params = null)
    {
        if ($params !== null && is_int($message)) {
            $message = vsprintf(static::$messages[$message], $params);
        }
        $this->errors[$key] = $message;
        return $this;
    }

Usage Example

Example #1
0
 protected function validate(&$value)
 {
     if ($this->isNull($value)) {
         return $this;
     }
     if (!$this->isArray($value) && !$this->isArrayObject($value)) {
         $this->expected('array or ArrayObject', gettype($value));
     }
     $value = StdObjectWrapper::toArray($value);
     if (!array_key_exists('lat', $value) || !array_key_exists('lng', $value)) {
         $ex = new ValidationException(ValidationException::VALIDATION_FAILED);
         $ex->addError($this->attribute, 'GeoPointAttribute value must contain `lat` and `lng`');
         throw $ex;
     }
 }
All Usage Examples Of Webiny\Component\Entity\Attribute\Validation\ValidationException::addError