/** * Add an error message to the validator's collection of messages. * * @param string $attribute * @param string $rule * @param array $parameters * @return void */ protected function addError($attribute, $rule, $parameters) { $message = $this->getMessage($attribute, $rule); $message = $this->doReplacements($message, $attribute, $rule, $parameters); $customMessage = new MessageBag(); $customMessage->merge(['code' => strtolower($rule . '_rule_error')]); $customMessage->merge(['message' => $message]); $this->messages->add($attribute, $customMessage); //dd($this->messages); }
public function validate() { $validator = App::make('validator'); $rules = $this->processRules($this->model->validationRules); $this->validator = $validator->make($this->model->toArray(), $rules, $this->model->validationMessages); if ($this->validator->fails()) { $this->messageBag = $this->messageBag ? $this->messageBag->merge($this->validator->messages()) : $this->validator->messages(); return false; } return true; }
/** * 存储错误消息 * @param string|array $key 表单字段名 | 消息列表 * @param string $message 错误消息 * @throws void */ public function error($key, $message = '') { if (!is_array($key)) { $key = array($key => $message); } $messageBag = new MessageBag($key); if (is_null(self::$errors)) { self::$errors = $messageBag; } else { self::$errors->merge($messageBag); } }
/** * Set value * @param string $key * @param array $fields * @param string $label * @return boolean * @throws \LogicException */ public function set($key, &$fields, $label) { $value = $this->getValueByKey($fields, $key); if (isset($this->value)) { $value = $this->value; } elseif (isset($this->function)) { $value = call_user_func_array($this->function, $this->function_params); } else { throw new \LogicException("No value for set for param `{$key}`."); } if (!$this->isEmpty($value)) { foreach ($this->validators as $validator) { $return = $validator->validate($key, $value, $label, $fields); if ($validator->hasErrors()) { if ($this->error === null) { $this->error = new MessageBag(); } $this->error->merge($validator->getErrors()); } if (!$return) { return false; } } } $fields[$key] = $value; return true; }
/** * Puts more errors in its message bag. * @return boolean */ public function putErrors($errors) { if (!is_array($errors) && !$errors instanceof MessageProvider) { $errors = [$errors]; } $this->errors->merge($errors); }
/** * Run the validation checks in the input data * * @param array $data * * @return bool * @throws Exception * @throws ValidateException */ public function runValidationChecks(array $data) { foreach ($this->validators as $validator) { if ($validator instanceof Validator) { if (!$validator->with($data)->validate()) { $this->errors->merge($validator->errors()); } } else { throw new Exception("{$validator} is not an instance of Motty\\Laravel\\Validator\\Contracts\\Validator"); } } if ($this->errors->isEmpty()) { return true; } throw new ValidateException('Validation failed', $this->errors); }
/** * Merge a new array of messages into the warning messages. * * @param MessageProvider|array $messages keyed by control name * @return $this */ public function withWarnings($messages) { if ($messages instanceof ViewErrorBag) { $messages = $messages->getMessageBag(); } $this->warning_messages->merge($messages); return $this; }
/** * Add new messages into the bag. * * @param mixed $messages array, string, \Codenest\Ahem\MessageBag or \Illuminate\Support\MessageBag Instances. * @return \Codenest\Ahem\Notification */ public function messages($messages = array()) { if (is_object($messages)) { $this->messages->merge($messages->getMessages()); } elseif (is_array($messages)) { $this->messages->merge($messages); } else { $this->messages->merge(array($messages)); } return $this; }
/** * Gather and merge import errors from * related model importers. */ public function gatherErrors() { foreach ($this->parents as $parent) { $parent->gatherErrors(); $this->errorMessageBag->merge($parent->errors()); } foreach ($this->children as $child) { $child->gatherErrors(); $this->errorMessageBag->merge($child->errors()); } }
/** * Validate a set of attributes against a certain action. * * @param string $action * @param array $attributes * * @return boolean */ public function valid($action, array $attributes) { if ($this->validator === null) { return true; } $result = $this->validator->valid($action, $attributes); if ($result === false) { $this->errors->merge($this->validator->getErrors()); } return $result; }
/** * Validates nested validators using previously bound input. * * @param mixed $options */ protected function validateNestedValidations($options = array()) { foreach ($this->nestedValidations as $prefix => $validation) { $validation->validate($options); // if valdation fails, add any messages to the message bag if ($validation->driver()->fails()) { $this->hasPassed = false; // Add the validation prefix to each message to ensure there aren't conflicts $messages = $this->applyPrefix($prefix, $validation->driver()->messages()); // Merge the messages into the current message bag $this->messageBag->merge($validation->driver()->messages()); } } }
/** * @return \Illuminate\Http\JsonResponse|null * @throws BadEventResultException */ public function asValidator() { $fails = false; $errors = new MessageBag(); /** @var Validator $validator */ foreach ($this->events() as $validator) { $this->validateValidationEventResponse($validator); if ($validator->fails()) { $fails = true; $errors->merge($validator->getMessageBag()->toArray()); } } if ($fails) { return response()->json($errors, 422); } return null; }
/** * Checks if the input request is doable * * @param array $fields * @param $moduleId * * @return true|MessageBag */ private function validateNewFields(array $fields = array(), $moduleId) { $customBag = new MessageBag(); // Main bag $validatorBags = array(); // Bags of different validators $takenColumnNames = array(); // Column names that already exist // Check if column name already exists $tmpField = new Field(); $rules = array_merge($tmpField->getRules(), array('column_name' => 'unique:fields,column_name,NULL,id,module_id,' . $moduleId)); // Go through all fields and validate them foreach ($fields as $field) { $photonField = new Field($field); $formField = FieldFactory::make($photonField); $formField->setRow($field); $validated = $formField->validate(); if ($validated instanceof MessageBag) { $validatorBags[] = $validated; } /* @var $validator \Illuminate\Validation\Validator */ $validator = \Validator::make($field, $rules); if ($validator->fails()) { $validatorBags[] = $validator->getMessageBag(); } if (in_array($field['column_name'], $takenColumnNames)) { $customBag->add('new_field', 'Column name `' . $field['column_name'] . '` cannot be assigned to multiple fields'); } $takenColumnNames[] = $field['column_name']; } // Merge all bags /* @var $bag MessageBag */ foreach ($validatorBags as $bag) { $customBag->merge($bag->getMessages()); } // Return the bag if there are no errors, otherwise return true return $customBag->isEmpty() ? true : $customBag; }
/** * Validates current attributes against rules. */ public function validate($model = null) { if (isset($this->attributes['created_at']) && isset($this->attributes[$this->primaryKey])) { foreach ($this->rules as $key => $rule) { if (strpos($rule, 'unique:') !== false) { $rulesCol = explode('|', $rule); foreach ($rulesCol as $key2 => $val) { if (starts_with($rulesCol[$key2], 'unique:')) { $rulesCol[$key2] .= ',' . $this->{$this->primaryKey} . ',' . $this->primaryKey; } } $this->rules[$key] = implode('|', $rulesCol); } } } $v = $this->validator->make($this->attributes, $this->rules); $v->setAttributeNames($this->getPrettyFields()); if ($v->passes() && $this->verificarConcurrencia()) { $this->afterValidate(); return true; } $this->errors->merge($v->messages()); return false; }
/** * Merge two message bags together * * @param \Illuminate\Support\MessageBag $bag * @param \Illuminate\Support\MessageBag $errors * @param integer $index * @return \Illuminate\Support\MessageBag */ private function mergeErrors(MessageBag $bag, MessageBag $errors, $index = null) { if (!$errors->count()) { return $bag; } // Add or merge errors into bag if ($index !== null) { $bag->add($index, $errors); } else { $bag->merge($errors); } return $bag; }
/** * Get the messages for the instance. * * @return \Illuminate\Support\MessageBag */ public function getMessageBag() { $messageBag = new MessageBag(static::$messages); return $messageBag->merge(static::$errors); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, Requests\CreateClaimRequest $request) { $request = $request->all(); $claim = Claim::findOrFail($id); $request['update_by'] = Auth::user()->id; $propertyList = array(); $errors = new Support\MessageBag(); if (isset($request["property"])) { $properties = Property::getPropertyByModel($claim); foreach ($properties as $property) { try { //if(isset($request["property"][$property->id])) //{ $attributes = ['value' => $request["property"][$property->id], 'property_id' => $property->id, 'element_id' => $claim->id]; if ($property->type == 'date') { $pr = PropertyTypes\DateProperty::where('property_id', $property->id)->where('element_id', $claim->id)->first(); //$pr->setPropertyTitle($property->title); if (empty($pr)) { $pr = new PropertyTypes\DateProperty($attributes, $property->title); } else { $pr->value = $attributes["value"]; } } elseif ($property->type == 'number') { $pr = PropertyTypes\NumberProperty::where('property_id', $property->id)->where('element_id', $claim->id)->first(); //$pr->setPropertyTitle($property->title); if (empty($pr)) { $pr = new PropertyTypes\NumberProperty($attributes, $property->title); } else { $pr->value = $attributes["value"]; } } else { $pr = PropertyTypes\TextProperty::where('property_id', $property->id)->where('element_id', $claim->id)->first(); //$pr->setPropertyTitle($property->title); if (empty($pr)) { $pr = new PropertyTypes\TextProperty($attributes, $property->title); } else { $pr->value = $attributes["value"]; } } //} $propertyList[] = $pr; } catch (ValidationException $e) { $errors->merge($e->errors()); } } } if ($errors->count() > 0) { return \Redirect::back()->withInput($request)->withErrors($errors); } $claim->update($request); foreach ($propertyList as $pr) { $pr->save(); } return redirect("claim/{$id}"); }
public function unlock() { $rules = ['password' => 'required|min:3']; $messages = ['password.required' => 'Please enter your password.']; $validator = Validator::make(Input::all(), $rules, $messages); // New MessageBag $errorMessages = new Illuminate\Support\MessageBag(); // Check if there is actually any errors if ($validator->fails()) { $errorMessages->merge($validator->errors()->toArray()); return Redirect::to('lock')->withErrors($errorMessages); } else { $login = Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password'))); if ($login) { return Redirect::to('/'); } else { $errorMessages->add('incorrect', 'Incorrect Password'); return Redirect::to('lock')->withErrors($errorMessages); } } }
/** * Validate $this option and assign it to $user. * * @param User * @param string * * @return \Illuminate\Support\MessageBag */ public function assignToUser(User $user, $value) { $errors = new MessageBag(); // If it's non assignable user can't choose value. Use default. if (!$this->assignable) { // Make sure we don't overwrite and already existing non assignable user option if (!$this->assignedToUser($user) and !$this->syncWithUser($user)) { $errors->add($this->name, _('Unable to save option to data base')); } return $errors; } // Validate and save $validator = Validator::make([$this->name => $value], [$this->name => $this->validation_rules]); $validator->setAttributeNames([$this->name => $this->label]); if ($validator->passes() and !$this->syncWithUser($user, $value)) { $errors->add($this->name, _('Unable to save option to data base')); } return $errors->merge($validator->messages()); }