目前我正在尝试使用active_url验证数据库设置中的url字段。但是,我想允许用户输入“localhost”。这违反了active_url的规则。如何在通过active_url或匹配“localhost”的文本字段上设置验证规则?
答案 0 :(得分:1)
我想一个简单的解决方案是使用条件逻辑来短路验证器。
示例:
$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。