public function validate() { $data = $this->toArray(); $this->validator = Validator::make($data, $this->getSingleValidator()); $result = !$this->validator->fails(); $this->failed = $this->validator->failed(); foreach ($this->getAllValidators() as $key => $validate) { $validator = Validator::make($data[$key], $validate->getSingleValidator()); $result = !$validator->fails() && $result; if ($validator->fails()) { $this->failed = array_merge($this->failed, [$key => $validator->failed()]); } } return $result; }
/** * ValidationException constructor. * * @author Morten Rugaard <*****@*****.**> * @param \Illuminate\Validation\Validator $validator * @param array $errorCodes * @param array $headers * @param bool $report * @param string $severity */ public function __construct(IlluminateValidator $validator, array $errorCodes, array $headers = [], $report = false, $severity = 'error') { // Parse failed rules $failedRules = $this->parseFailedRules($validator->failed()); // Set message of exception $errorMessages = $validator->errors(); if ($errorMessages->count() > 1) { $message = 'Multiple validation rules failed. See "errors" for more details.'; } else { $message = $errorMessages->first(); } // Custom error codes container $customErrorCodes = []; // Custom error codes takes priority, so let's see // if one of our failed rules has one $failedRulesCustomErrorCodes = array_intersect(array_keys($errorCodes), $failedRules); if (!empty($failedRulesCustomErrorCodes)) { foreach ($failedRulesCustomErrorCodes as $failedRule) { $customErrorCodes[$errorCodes[$failedRule]] = $errorCodes[$failedRule]; } } // Determine exception and status code $exceptionCode = $statusCode = !empty($customErrorCodes) ? array_shift($customErrorCodes) : 412; // Construct exception parent::__construct($message, $exceptionCode, $headers, $report, $severity); // Fill exception's error bag with validation errors $this->setErrors($errorMessages); // Set status code $this->setStatusCode($statusCode, $errorMessages->first()); // Do not send report $this->dontReport(); }
/** * {@inheritDoc} */ public function failed() { return $this->validator->failed(); }
/** * @return array */ public function getFailedRules() { return $this->validator->failed(); }
/** * * @param Validator $object */ public function setValidator(Validator $object) { $this->_messages = $object->errors()->getMessages(); $this->_rules = $object->failed(); }