Ale*_*lex 8 html php regex validation laravel
所以,我的模型中有以下规则:
public static $rules = array(
'name' => 'required|alpha_dash|unique:subsidiaries',
'internal_number' => 'required|alpha_dash|unique:subsidiaries',
'expedition_rate' => array('required', 'regex:/^[0-9]{1,3}(\.?)[0-9]{1,2}$/'),
'hundred_kg_rate' => array('regex:/^[0-9]{1,5}(\.?)[0-9]{1,2}$/'),
'handling' => array('regex:/^[0-9]{1,3}(\.?)[0-9]{1,2}$/'),
'insurance' => 'required|numeric',
);
Run Code Online (Sandbox Code Playgroud)
但是,出于某种原因,当正则表达式应用于patternhtml 中的属性标记时...它会中断!
结果:
<input required="true" pattern="^[0-9]{" class="form-control" ....>
_________
\
=> This right here should be
^[0-9]{1,3}(\.?)[0-9]{1,2}$
Run Code Online (Sandbox Code Playgroud)
尝试转义大括号...我敢打赌 PHP 期望解析内部的变量,如下所示:
$var = 'something';
echo 'The value is {$var}.';
// Outputs:
// The value is something.
Run Code Online (Sandbox Code Playgroud)
将此与以下内容进行对比:
$var = 'something';
echo 'The value is \{$var\}.';
// Outputs:
// The value is $var.
Run Code Online (Sandbox Code Playgroud)
但让我感到困惑的是,这些都在单引号内,所以这种情况不应该发生。也许是 Laravel 的事情?