Laravel 5.2具有自定义验证功能的自定义验证消息 [英] Laravel 5.2 Custom validation message with custom validation function

查看:154
本文介绍了Laravel 5.2具有自定义验证功能的自定义验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自定义验证错误消息创建自定义验证规则.为此,我创建了一条规则:

I want to create custom validation rule with custom validation error message. For this I created a rule:

$rule => [
    'app_id' => 'isValidTag'
]

对于自定义消息:

$message => [
   app_id.isValidTag   => 'Not a Valid id'
];

此后,我创建了服务提供商:

After that I created Service Provider:

class CustomValidationServiceProvider extends ServiceProvider
{
    public function boot() {

    //parent::boot();

    $this->app->validator->resolver(function($transator,$data,$rules,$messages){
            return new CustomValidator($transator,$data,$rules,$messages);
        });
    }
}

我的自定义验证类是:

class CustomValidator extends Validator {
    if(empty($parameters)) {
        return true;
    }

    $conext = $parameters[0];
    $tag = Tag::where('id', $value)->where('context', $conext)->get();

    $flag = false;
    if($tag->count() > 0) {
        $flag = true;
    }       

    return $flag;
}

一切正常,但问题是我对app_id.isValidTag的自定义消息无法正常工作,即使所有其他消息都工作正常.

All is working fine but the issue is my custom message for app_id.isValidTag is not working even all other message are working fine.

请向我建议我在这里或Laravel 5.2中缺少的内容,以显示消息.任何想法将不胜感激.

Please suggest me what I missing here or in Laravel 5.2 there is some change to display message. Any idea will be appreciated.

推荐答案

以下是一个很棒的教程:

Here is a great tutorial for this: http://itsolutionstuff.com/post/laravel-5-create-custom-validation-rule-exampleexample.html

我认为您是通过Laravel 4. *做到的.这是在 Laravel 5.2 中完成的操作 在我制作注册授权表单的示例中,是预先制作了诸如AuthController.php之类的文件:

I think you did it Laravel 4.* way. This is how it is done in Laravel 5.2 in my example where i was making registration authorisation form so files like AuthController.php was premade:

  1. AuthController.php

  1. AuthController.php

Validator::make($data, [
    ...
    // add your field for validation
    'name_of_the_field' => 'validation_tag', // validation tag from validation.php
    ...

  • CustomAuthProvider.php //如果您没有使自定义提供程序使用Providers/AppServiceProvider.php

  • CustomAuthProvider.php // if you didn't make a custom provider use Providers/AppServiceProvider.php

    public function boot() {
        ...
        Validator::extend('validation_tag', function($attribute, $value, $parameters, $validator) {
                // handle here your validation
                if (  your_query ) {
                    return true;
                }
                return false;
        });
    

  • validation.php

  • validation.php

    ...
    // add your validation tag and message to be displayed
    'validation_tag'           => 'The field :attribute isn't good',
    ...
    

  • file.blade.php //在页面末尾添加所有错误,添加

  • file.blade.php // to add at the end of the page all your errors add

    @if (count($errors) > 0)
          <div class="alert alert-danger">
                 <ul>
                       @foreach ($errors->all() as $error)
                             <li>{{ $error }}</li>
                       @endforeach
                 </ul>
           </div>
    @endif
    

  • 这篇关于Laravel 5.2具有自定义验证功能的自定义验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆