Laravel 4 自定义验证规则 - 在哪里扩展验证器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20517898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:45:04  来源:igfitidea点击:

Laravel 4 custom validation rule - where to extend the validator?

laravellaravel-4

提问by user1686123

I want to make a custom validation rule. My model looks like this at the moment:

我想制定一个自定义验证规则。我的模型现在看起来像这样:

protected $rules = array(
    'first_name'  => 'required',
    'last_name'   => 'required',
    'ssn'         => 'required|integer|min:4|max:4',
    'email'       => 'required|email',
    'dob'         => 'required|checkAge',
    'phone'       => 'required',
    'street'      => 'required',
    'postal_code' => 'required|integer|min:4',
    'city'        => 'required'
);

But where I have to put the custom validation rule? I have read that I need to extend it Validator. For this I've created a simple function

但是我必须把自定义验证规则放在哪里?我已经读过我需要扩展它的验证器。为此,我创建了一个简单的函数

Validator::extend('foo', function($attribute, $value, $parameters)
{
    return $value == 'foo';
});

And I don't know where I have to check it?

而且我不知道我必须在哪里检查它?

Maybe someone can help me.

也许有人可以帮助我。

Thanks

谢谢

回答by Rooneyl

I do it by creating a validation folder in /app with any custom validation files in there.
I autoload this by editing app/start/global.php.

我通过在 /app 中创建一个带有任何自定义验证文件的验证文件夹来做到这一点。
我通过编辑app/start/global.php自动加载它。

ClassLoader::addDirectories(array(
    app_path() . '/commands',
    app_path() . '/controllers',
    app_path() . '/models',
    app_path() . '/presenters',
    app_path() . '/validation',
    app_path() . '/database/seeds',
));

I also register the resolver in this file;

我也在这个文件中注册了解析器;

Validator::resolver(function($translator, $data, $rules, $messages) {
        return new CoreValidator($translator, $data, $rules, $messages);
    });

A sample custom validator class (in the validation folder);

示例自定义验证器类(在验证文件夹中);

<?php

class CoreValidator extends Illuminate\Validation\Validator
{

    protected $implicitRules = array('Required', 'RequiredWith', 'RequiredWithout', 'RequiredIf', 'Accepted', 'RequiredWithoutField');

    public function __construct(\Symfony\Component\Translation\TranslatorInterface $translator, $data, $rules, $messages = array())
    {
        parent::__construct($translator, $data, $rules, $messages);
        $this->isImplicit('fail');
    }

    public function validatePostcode($attribute, $value, $parameters = null)
    {
        $regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
        if (preg_match($regex, $value)) {
            return true;
        }
        return false;
    }
}

And add the custom error message to the array in in app/lang/en/validation.php

并将自定义错误消息添加到app/lang/en/validation.php 中的数组中

return array(
...
"postcode" => "Invalid :attribute entered.",
...
)

回答by Antonio Carlos Ribeiro

Extensions can be added as soon as the application boots. What I do is to create a validations.phpfile in the same level of routes.phpand filters.phpand then add this to my app/start/global.php:

一旦应用程序启动,就可以添加扩展。我要做的就是创建一个validations.php在同级别的文件routes.phpfilters.php然后加入到我的app/start/global.php

require app_path().'/filters.php'; /// this one is already there...
require app_path().'/validations.php';