6

我在我的 Laravel 应用程序中创建了一组自定义验证规则。我首先在目录中创建了一个validators.php文件App\Http

/**
 * Require a certain number of parameters to be present.
 *
 * @param  int     $count
 * @param  array   $parameters
 * @param  string  $rule
 * @return void
 * @throws \InvalidArgumentException
 */

    function requireParameterCount($count, $parameters, $rule) {

        if (count($parameters) < $count):
            throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters.");
        endif;

    }


/**
 * Validate the width of an image is less than the maximum value.
 *
 * @param  string  $attribute
 * @param  mixed   $value
 * @param  array   $parameters
 * @return bool
 */

    $validator->extend('image_width_max', function ($attribute, $value, $parameters) {

        requireParameterCount(1, $parameters, 'image_width_max');

        list($width, $height) = getimagesize($value);

        if ($width >= $parameters[0]):
            return false;
        endif;

        return true;

    });

然后我在我的AppServiceProvider.php文件中添加这个(同时也在use Illuminate\Validation\Factory;这个文件的顶部添加):

public function boot(Factory $validator) {

    require_once app_path('Http/validators.php');

}

然后在我的表单请求文件中,我可以调用自定义验证规则,如下所示:

$rules = [
    'image' => 'required|image|image_width:50,800',
];

validation.php然后在位于目录中的 Laravel文件中resources/lang/en,我将另一个键/值添加到数组中,以在验证返回 false 并且失败时显示错误消息,如下所示:

'image_width' => 'The :attribute width must be between :min and :max pixels.',

一切正常,它会正确检查图像,如果失败则显示错误消息,但我不确定如何替换:min:max使用表单请求文件中声明的值(50,800),以相同的方式:attribute替换为表单字段姓名。所以目前它显示:

The image width must be between :min and :max pixels.

而我希望它像这样显示

The image width must be between 50 and 800 pixels.

replace*在主Validator.php文件中看到了一些函数(vendor/laravel/framework/src/Illumiate/Validation/),但我似乎不太明白如何让它与我自己的自定义验证规则一起工作。

4

3 回答 3

11

我没有以这种方式使用它,但您可能可以使用:

$validator->replacer('image_width_max',
    function ($message, $attribute, $rule, $parameters) {
        return str_replace([':min', ':max'], [$parameters[0], $parameters[1]], $message);
    });
于 2015-03-29T07:50:04.007 回答
1

这是我使用的解决方案:

在 composer.json 中:

"autoload": {
    "classmap": [
        "app/Validators"
    ],

在 App/Providers/AppServiceProvider.php 中:

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

在 App/Validators/CustomValidator.php

namespace App\Validators;

use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Validator as Validator;

class CustomValidator extends Validator
{
    // This is my custom validator to check unique with
    public function validateUniqueWith($attribute, $value, $parameters)
    {
        $this->requireParameterCount(4, $parameters, 'unique_with');
        $parameters    = array_map('trim', $parameters);
        $parameters[1] = strtolower($parameters[1] == '' ? $attribute : $parameters[1]);
        list($table, $column, $withColumn, $withValue) = $parameters;

        return DB::table($table)->where($column, '=', $value)->where($withColumn, '=', $withValue)->count() == 0;
    }

    // All you have to do is create this function changing
    // 'validate' to 'replace' in the function name
    protected function replaceUniqueWith($message, $attribute, $rule, $parameters)
    {
        return str_replace([':name'], $parameters[4], $message);
    }
}

:name 在此 replaceUniqueWith 函数中被 $parameters[4] 替换

在 App/resources/lang/en/validation.php

<?php
return [
    'unique_with' => 'The :attribute has already been taken in the :name.',
];

在我的控制器中,我这样称呼这个验证器:

$organizationId = session('organization')['id'];    
$this->validate($request, [
    'product_short_title' => "uniqueWith:products,short_title,
                              organization_id,$organizationId,
                              Organization",
]);

这就是我的表单中的样子:)

在此处输入图像描述

于 2015-08-09T12:11:53.313 回答
0

我在 Laravel 5.4 中使用这样的东西:

AppServiceProvider.php

public function boot()
{
    \Validator::extend('contains_field', 'App\Validators\ContainsFieldValidator@validate');
    \Validator::replacer('contains_field', 'App\Validators\ContainsFieldValidator@replace');
}

App\Validators\ContainsFieldValidator.php

class ContainsFieldValidator
{
    public function validate($attribute, $value, $parameters, Validator $validator)
    {
        $required = $parameters[0];
        $requiredDefault = isset($parameters[1]) ?: null;

        if (!$required && !$requiredDefault) {
            return false;
        }

        $requiredValue = isset($validator->attributes()[$required]) ? $validator->attributes()[$required] : $requiredDefault;

        return !(strpos($value, $requiredValue) === false);
    }

    public function replace($message, $attribute, $rule, $parameters)
    {
        return str_replace([':required'], str_replace('_', ' ', $parameters[0]), $message);
    }
}
于 2017-05-05T08:56:17.357 回答