/**
  * @return array|MessageBag
  */
 public function messages() : array
 {
     if ($this->validation === null) {
         return [];
     }
     return $this->validation->messages();
 }
Esempio n. 2
0
 public function getErrorMessages()
 {
     $allMessages = $this->validator->messages()->getMessages();
     if (!$this->reportIlluminateErrors) {
         $availableErrorMessages = $this->form->bootErrorMessages();
         $plainMessages = [];
         foreach ($allMessages as $fieldName => $message) {
             $message = isset($availableErrorMessages[$fieldName]) ? $availableErrorMessages[$fieldName] : "{attribute} is not valid";
             $message = str_replace('{:attribute}', '{' . $fieldName . '}', $message);
             // array of 1 element for compatibility with Laravel's MessageBag
             $plainMessages[$fieldName] = [$message];
         }
         $allMessages = $plainMessages;
     }
     foreach ($allMessages as $fieldName => &$messages) {
         foreach ($messages as &$message) {
             $searchField = str_replace('_', ' ', $fieldName);
             $searchField = '{' . $searchField . '}';
             $element = $this->form->el($fieldName);
             /* @var $element Element */
             $replace = [$searchField => $element->label, '{value}' => htmlspecialchars($element->val(), ENT_NOQUOTES, 'UTF-8')];
             // for choice elements add option to output the text value of an option
             if ($element instanceof Choice) {
                 $replace['{valueText}'] = $element->valueText();
             }
             $message = str_replace(array_keys($replace), array_values($replace), $message);
         }
         $messages = implode(', ', $messages);
     }
     return $allMessages;
 }
Esempio n. 3
0
 public function getMessages()
 {
     if ($this->_validator !== null) {
         return $this->_validator->messages()->getMessages();
     } else {
         return $this->_messages;
     }
 }
Esempio n. 4
0
 /**
  * Return the validation errors with :field-:message format
  *
  * @return mixed
  */
 public function getValidationErrors()
 {
     $errors = [];
     $messages = $this->validation->messages();
     foreach ($messages->getMessages() as $key => $message) {
         $errors[$key] = $message[0];
     }
     return $errors;
 }
Esempio n. 5
0
 public function validate(\Illuminate\Validation\Validator $validator)
 {
     if ($validator->fails()) {
         $this->validation_errors = $validator->messages();
         return false;
     }
     return true;
 }
Esempio n. 6
0
 /**
  * Alias for displaying validation errors.
  * @param Validator $validator
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public static function failedValidation(Validator $validator)
 {
     $response = static::create(null, 406);
     foreach ($validator->messages()->all() as $msg) {
         $response->error($msg, 0);
     }
     return $response;
 }
Esempio n. 7
0
 public function validateOrFail($additional = array())
 {
     $attributes = $this->getAttributes();
     $this->validator = \Validator::make($attributes, $this->prepareRules());
     if ($this->validator->fails()) {
         $messages = $this->validator->messages();
         foreach ($attributes as $key => $value) {
             if ($messages->has($key)) {
                 foreach ($messages->get($key) as $message) {
                     $additional['attribute'] = $key;
                     $this->errors->add(new Error($message, '', $additional));
                 }
             }
         }
         throw new ValidationException($this->errors->errors());
     }
     return true;
 }
 function it_returns_messages_on_failed_validation(Validator $validator, MessageBag $bag)
 {
     $validator->setData($data = [])->shouldBeCalled();
     $validator->passes()->willReturn(false);
     $validator->messages()->willReturn($bag);
     $bag->all()->willReturn($messages = ['foo' => 'failed']);
     $this->validate($data)->shouldReturn(false);
     $this->getMessages()->shouldReturn($messages);
 }
Esempio n. 9
0
 /**
  * Validate data.
  *
  * @author Morten Rugaard <*****@*****.**>
  *
  * @return bool
  * @throws \Nodes\Validation\Exceptions\ValidationException
  */
 public function validate()
 {
     // Generate validator instance
     $this->validator = $this->validatorFactory->make($this->getData(), $this->getRules(), $this->getMessages(), $this->getAttributes());
     // Validation was a success!
     if (!$this->validator->fails()) {
         return true;
     }
     // Collect validation errors
     $this->errors = $this->validator->messages();
     return false;
 }
Esempio n. 10
0
 /**
  * Validates the supplied data against the options rules
  *
  * @param array		$data
  * @param array		$rules
  * @param array		$messages
  *
  * @param mixed
  */
 public function validateData(array $data, array $rules, array $messages)
 {
     if ($rules) {
         $this->customValidator->setData($data);
         $this->customValidator->setRules($rules);
         $this->customValidator->setCustomMessages($messages);
         //if the validator fails, kick back the errors
         if ($this->customValidator->fails()) {
             return implode('. ', $this->customValidator->messages()->all());
         }
     }
     return true;
 }
Esempio n. 11
0
 /**
  * Returns whether the current state of the validator is valid.
  *
  * @param bool $partial Whether a partial validation is okay.
  * @return bool
  */
 public function valid($partial = false)
 {
     if ($partial) {
         $this->useFields(array_keys($this->values));
     }
     // Generate our implementation from the app.
     $this->validator = self::$factory->make($this->values, $this->rules);
     if ($this->validator->passes()) {
         return true;
     }
     $this->errors = $this->validator->messages();
     return false;
 }
Esempio n. 12
0
 /**
  * Validate the model instance
  *
  * @param array $rules            Validation rules
  * @param array $customMessages   Custom error messages
  * @param array $customAttributes Custom attributes
  * @return bool
  * @throws InvalidModelException
  */
 public function validate(array $rules = array(), array $customMessages = array(), array $customAttributes = array())
 {
     if ($this->fireModelEvent('validating') === false) {
         if ($this->throwOnValidation) {
             throw new InvalidModelException($this);
         } else {
             return false;
         }
     }
     // check for overrides, then remove any empty rules
     $rules = empty($rules) ? static::$rules : $rules;
     foreach ($rules as $field => $rls) {
         if ($rls == '') {
             unset($rules[$field]);
         }
     }
     if (empty($rules)) {
         $success = true;
     } else {
         $customMessages = empty($customMessages) ? static::$customMessages : $customMessages;
         $customAttributes = empty($customAttributes) ? static::$customAttributes : $customAttributes;
         if ($this->forceEntityHydrationFromInput || empty($this->attributes) && $this->autoHydrateEntityFromInput) {
             $this->fill(Input::all());
         }
         $data = $this->getAttributes();
         // the data under validation
         // perform validation
         $this->validator = static::makeValidator($data, $rules, $customMessages, $customAttributes);
         $success = $this->validator->passes();
         if ($success) {
             // if the model is valid, unset old errors
             if ($this->validationErrors === null || $this->validationErrors->count() > 0) {
                 $this->validationErrors = new MessageBag();
             }
         } else {
             // otherwise set the new ones
             $this->validationErrors = $this->validator->messages();
             // stash the input to the current session
             if (!self::$externalValidator && Input::hasSession()) {
                 Input::flash();
             }
         }
     }
     $this->fireModelEvent('validated', false);
     if (!$success && $this->throwOnValidation) {
         throw new InvalidModelException($this);
     }
     return $success;
 }
Esempio n. 13
0
 /**
  * @param $id
  * @param $data
  * @return $this|\Illuminate\Http\RedirectResponse
  */
 public function update($id, $data)
 {
     if (!$this->validate($data)) {
         return back()->withInput()->withErrors($this->validator->messages());
     }
     $this->model = $this->model->with($this->getRelations())->findOrFail($id);
     $this->setFieldOriginalValue();
     $this->prepare($data, $this->saving);
     DB::transaction(function () {
         $updates = $this->prepareUpdate($this->updates);
         foreach ($updates as $column => $value) {
             $this->model->setAttribute($column, $value);
         }
         $this->model->save();
         $this->updateRelation($this->relations);
     });
     return redirect($this->resource());
 }
Esempio n. 14
0
 /**
  * @param $id
  * @param $data
  * @return $this|\Illuminate\Http\RedirectResponse
  */
 public function update($id, $data)
 {
     if (!$this->validate($data)) {
         return back()->withInput()->withErrors($this->validator->messages());
     }
     $this->model = $this->model->with($this->getRelations())->findOrFail($id);
     $this->setFieldOriginalValue();
     $updates = array_filter($data, 'is_string');
     $relations = array_filter($data, 'is_array');
     DB::transaction(function () use($updates, $relations) {
         $updates = $this->prepareUpdate($updates);
         foreach ($updates as $column => $value) {
             $this->model->setAttribute($column, $value);
         }
         $this->model->save();
         foreach ($relations as $name => $values) {
             if (!method_exists($this->model, $name)) {
                 continue;
             }
             $prepared = $this->prepareUpdate([$name => $values]);
             if (empty($prepared)) {
                 continue;
             }
             $relation = $this->model->{$name}();
             switch (get_class($relation)) {
                 case \Illuminate\Database\Eloquent\Relations\BelongsToMany::class:
                     $relation->sync($prepared[$name]);
                     break;
                 case \Illuminate\Database\Eloquent\Relations\HasOne::class:
                     foreach ($prepared[$name] as $column => $value) {
                         $this->model->{$name}->setAttribute($column, $value);
                     }
                     $this->model->{$name}->save();
                     break;
             }
         }
     });
     return redirect($this->resource());
 }
Esempio n. 15
0
 /**
  * @param Validator $validator
  *
  * @throws CommandValidationException
  */
 protected function throwException(Validator $validator)
 {
     throw new CommandValidationException($validator->messages());
 }
Esempio n. 16
0
 /**
  * respond with all validation errors
  * @param  \Illuminate\Validation\Validator $validator the validator that failed to pass
  * @return \Illuminate\Http\Response the appropriate response containing the error message
  */
 protected function respondWithFailedValidation(\Illuminate\Validation\Validator $validator)
 {
     return $this->respondWithErrors($validator->messages()->all());
 }
Esempio n. 17
0
 /**
  * Get the validation errors.
  *
  * @return mixed
  */
 public function getErrors()
 {
     return $this->validation->messages();
 }
 /**
  * Get the message container for the validator.
  *
  * @return \Illuminate\Support\MessageBag
  */
 public function messages()
 {
     return $this->validator->messages();
 }
 /**
  * Sets error messages from validation object
  *
  * @param \Illuminate\Validation\Validator $validator The validator object to get errors from
  */
 private function setErrors(\Illuminate\Validation\Validator $validator)
 {
     $messages = $validator->messages()->all();
     foreach ($messages as $message) {
         $this->errors[] = $message;
     }
 }
Esempio n. 20
0
 /**
  * Generates a Response with a 400 HTTP header and a given message from validator
  *
  * @param $validator
  * @return \Illuminate\Http\Response
  */
 public function errorWrongArgsValidator(Validator $validator)
 {
     return $this->errorWrongArgs($validator->messages()->toArray());
 }
Esempio n. 21
0
 /**
  * Get the messages from the validator.
  *
  * @return array
  */
 public function getValidatorMessages()
 {
     return $this->validator ? $this->validator->messages() : [];
 }
Esempio n. 22
0
 /**
  * ValidationException constructor.
  *
  * @param string          $message
  * @param int             $code
  * @param \Exception|null $previous
  * @param Validator       $validator
  */
 public function __construct($message = '', $code = 0, \Exception $previous = null, Validator $validator)
 {
     parent::__construct($message, $code, $previous, $validator->messages()->toArray());
 }
Esempio n. 23
0
 public function badRequest(Validator $validator)
 {
     return $this->error(['code' => 400, 'message' => 'Bad Request', 'validations' => $validator->messages()->toArray()]);
 }