目前我正在尝试使用active_url来验证数据库设置中的url字段。但是,我希望允许用户也在"localhost"中输入。这使active_url的规则失效。如何在传递active_url或匹配"localhost"的文本字段上设置验证规则?
我想一个简单的解决方案是使用条件逻辑使验证器短路。
示例:
$host = $_SERVER['HTTP_HOST'];
$rules = array(
'url' => 'active_url' // check DNS if valid (your default option)
);
$validator = Validator::make(Input::all(), $rules);
if ($host !== 'localhost' && $validator->fails()) { // this is where the magic happens
// do stuff on failure
}
如果您在localhost上运行,它只会使条件短路,并返回false。