if($request->isMethod('post')){ //判断请求方式
$this->validate($request, ['custom'=>"string|between:2,8"],['custom.between'=>'自定义关键词要求为词组']);
}
源码中各个验证规则类型
在源码这里获取 行验证的错误信息
vendor/laravel/framework/src/Illuminate/Validation/Validator.php
/**
* Get the inline message for a rule if it exists.
*
* @param string $attribute
* @param string $lowerRule
* @param array $source
* @return string|null
*/
protected function getInlineMessage($attribute, $lowerRule, $source = null)
{
$source = $source ?: $this->customMessages;
$keys = ["{$attribute}.{$lowerRule}", $lowerRule];
// First we will check for a custom message for an attribute specific rule
// message for the fields, then we will check for a general custom line
// that is not attribute specific. If we find either we'll return it.
foreach ($keys as $key) {
foreach (array_keys($source) as $sourceKey) {
if (Str::is($sourceKey, $key)) {
return $source[$sourceKey];
}
}
}
}
从上一个在dug测试Str::is($sourceKey, $key)
是关键判断打印出来发现一个是custom.between
一个是custom
并不相等。由此得知在自定义的验证信息时 需要对验证的信息做出类型上的指明.between
读取源代码是到这里进行 自定义验证消息 key键的对比
/vendor/laravel/framework/src/Illuminate/Support/Str.php
/**
* Determine if a given string matches a given pattern.
*
* @param string $pattern
* @param string $value
* @return bool
*/
public static function is($pattern, $value)
{
if ($pattern == $value) {
return true;
}
$pattern = preg_quote($pattern, '#');
// Asterisks are translated into zero-or-more regular expression wildcards
// to make it convenient to check if the strings starts with the given
// pattern such as "library/*", making any string check convenient.
$pattern = str_replace('\*', '.*', $pattern);
return (bool) preg_match('#^'.$pattern.'\z#', $value);
}
还是要多看看手册啊。 要不就花2小时来读取源码做测试咯