本文整理汇总了PHP中Illuminate\Validation\Validator::messages方法的典型用法代码示例。如果您正苦于以下问题:PHP Validator::messages方法的具体用法?PHP Validator::messages怎么用?PHP Validator::messages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Validation\Validator
的用法示例。
在下文中一共展示了Validator::messages方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: messages
/**
* @return array|MessageBag
*/
public function messages() : array
{
if ($this->validation === null) {
return [];
}
return $this->validation->messages();
}
示例2: getErrorMessages
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;
}
示例3: getMessages
public function getMessages()
{
if ($this->_validator !== null) {
return $this->_validator->messages()->getMessages();
} else {
return $this->_messages;
}
}
示例4: getValidationErrors
/**
* 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;
}
示例5: validate
public function validate(\Illuminate\Validation\Validator $validator)
{
if ($validator->fails()) {
$this->validation_errors = $validator->messages();
return false;
}
return true;
}
示例6: failedValidation
/**
* 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;
}
示例7: validateOrFail
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;
}
示例8:
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);
}
示例9: validate
/**
* Validate data.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @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;
}
示例10: validateData
/**
* 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;
}
示例11: valid
/**
* 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;
}
示例12: validate
/**
* 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;
}
示例13: update
/**
* @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());
}
示例14: update
/**
* @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());
}
示例15: throwException
/**
* @param Validator $validator
*
* @throws CommandValidationException
*/
protected function throwException(Validator $validator)
{
throw new CommandValidationException($validator->messages());
}