php 自定义占位符,关于php:Laravel 5中用于自定义验证规则的自定义占位符

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

/**

* 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',

];

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

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

一切正常,检查图像正确,如果失败,则显示错误消息,但是我不确定如何用表单请求文件中声明的值(50,800)替换:min和:max :attribute替换为表单字段名称。因此,当前显示为:

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

我想让它像这样显示

The image width must be between 50 and 800 pixels.

我已经在主Validator.php文件(vendor/laravel/framework/src/Illumiate/Validation/)中看到了一些replace*函数,但是我似乎还不太清楚如何使它与我自己的自定义验证规则一起使用。

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

$validator->replacer('image_width_max',

function ($message, $attribute, $rule, $parameters) {

return str_replace([':min', ':max'], [$parameters[0], $parameters[1]], $message);

});

不久后我会给这个测试做个麻烦,谢谢您,让我知道我过得如何!

工作请客,我很感激。

这是我使用的解决方案:

在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 is replace by $parameters[4] in this replaceUniqueWith function

在App / resources / lang / en / validation.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",

]);

这就是我的形式:)

07f897bcb65b6978998c35bfa41b2b12.png

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

AppServiceProvider.php

public function boot()

{

\Validator::extend('contains_field', 'App\Validators\ContainsFieldValidator@validate');

\Validator::replacer('contains_field', 'App\Validators\ContainsFieldValidator@replace');

}

应用程序验证器 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值