preg_match(): 未找到结束分隔符“/”Laravel
preg_match(): No ending delimiter '/' found Laravel
我正在通过请求的正则表达式进行验证,此验证是为了检查发送的参数是否为 IP。
我的规则如下:
<?php
namespace App\Http\Requests\usuarios;
use Illuminate\Foundation\Http\FormRequest;
class storeVPN extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'segmentwan' => 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',
'segmentlan' => 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',
];
}
调试器告诉我错误在这一行
调试器告诉我错误在这一行
return preg_match($parameters[0], $value) > 0;
你的错误完成了 es el siguiente
* @return bool
*/
public function validateRegex($attribute, $value, $parameters)
{
if (! is_string($value) && ! is_numeric($value)) {
return false;
}
$this->requireParameterCount(1, $parameters, 'regex');
return preg_match($parameters[0], $value) > 0;
}
/**
* Validate that a required attribute exists.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateRequired($attribute, $value)
{
if (is_null($value)) {
return false;
} elseif (is_string($value) && trim($value) === '') {
return false;
} elseif ((is_array($value) || $value instanceof Countable) && count($value) < 1) {
return false;
} elseif ($value instanceof File) {
return (string) $value->getPath() !== '';
}
更改您的验证规则。当您使用正则表达式时,您需要使用 Array 而不是管道定界符。
return [
'segmentwan' => [ 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/'],
'segmentlan' => [ 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/'],
];
Note: When using the regex pattern, it may be necessary to specify
rules in an array instead of using pipe delimiters, especially if the
regular expression contains a pipe character.
更新
如果您要验证 IP 地址,laravel 会提供三个验证选项。
ip:
The field under validation must be an IP address.
ipv4:
The field under validation must be an IPv4 address.
ipv6:
The field under validation must be an IPv6 address.
您不应该使用正则表达式来验证 IP;您可以使用 filter_var()
内置函数来代替
它有很多标志,包括 FILTER_FLAG_IPV4
用于 ipv4 和 FILTER_FLAG_IPV6
用于 ipv6 ...当然你可以混合使用标志,这样你就可以检查两者
在 Laravel 中,您有 ip 验证规则,因此您可以使用:
return [
'segmentwan' => 'ip',
'segmentlan' => 'ip',
];
老实说,当我查看您的代码时,我没有看到错误。您确定这 2 个正则表达式有误吗?
我正在通过请求的正则表达式进行验证,此验证是为了检查发送的参数是否为 IP。
我的规则如下:
<?php
namespace App\Http\Requests\usuarios;
use Illuminate\Foundation\Http\FormRequest;
class storeVPN extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'segmentwan' => 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',
'segmentlan' => 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',
];
}
调试器告诉我错误在这一行
调试器告诉我错误在这一行
return preg_match($parameters[0], $value) > 0;
你的错误完成了 es el siguiente
* @return bool
*/
public function validateRegex($attribute, $value, $parameters)
{
if (! is_string($value) && ! is_numeric($value)) {
return false;
}
$this->requireParameterCount(1, $parameters, 'regex');
return preg_match($parameters[0], $value) > 0;
}
/**
* Validate that a required attribute exists.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateRequired($attribute, $value)
{
if (is_null($value)) {
return false;
} elseif (is_string($value) && trim($value) === '') {
return false;
} elseif ((is_array($value) || $value instanceof Countable) && count($value) < 1) {
return false;
} elseif ($value instanceof File) {
return (string) $value->getPath() !== '';
}
更改您的验证规则。当您使用正则表达式时,您需要使用 Array 而不是管道定界符。
return [
'segmentwan' => [ 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/'],
'segmentlan' => [ 'regex:/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/'],
];
Note: When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
更新
如果您要验证 IP 地址,laravel 会提供三个验证选项。
ip: The field under validation must be an IP address.
ipv4: The field under validation must be an IPv4 address.
ipv6: The field under validation must be an IPv6 address.
您不应该使用正则表达式来验证 IP;您可以使用 filter_var()
内置函数来代替
它有很多标志,包括 FILTER_FLAG_IPV4
用于 ipv4 和 FILTER_FLAG_IPV6
用于 ipv6 ...当然你可以混合使用标志,这样你就可以检查两者
在 Laravel 中,您有 ip 验证规则,因此您可以使用:
return [
'segmentwan' => 'ip',
'segmentlan' => 'ip',
];
老实说,当我查看您的代码时,我没有看到错误。您确定这 2 个正则表达式有误吗?