Php 在第一次失败时停止valitron对链式规则的验证
我在valitron中建立了一个验证规则链,最后一个是自定义规则Php 在第一次失败时停止valitron对链式规则的验证,php,validation,Php,Validation,我在valitron中建立了一个验证规则链,最后一个是自定义规则 $this->validator->rule('required', ['id'])->rule('integer', ['id'])->rule(function($field, $value, $params, $fields) use ($input) { // My custom code here }, ['id'])->message("Not a val
$this->validator->rule('required', ['id'])->rule('integer', ['id'])->rule(function($field, $value, $params, $fields) use ($input) {
// My custom code here
}, ['id'])->message("Not a valid Launcher ID");
问题是:如果id为空或不是数字,我宁愿不执行自定义验证。换句话说:我想在链的第一次失败时停止评估。
我该怎么做
/**
* Should the validation stop a rule is failed
* @param bool $stop
*/
public function stopOnFirstFail($stop = true)
{
$this->stop_on_first_fail = (bool)$stop;
}
在验证之前,只需调用
$this->validator->stopOnFirstFail()
。谢谢!然而,这并不是我想要的。对不起,我不清楚。我想在特定链上第一次失败后停止。使用您的方法,它会在任何其他失败规则上停止,即使是在其他参数上。@Lorenzo查看源代码,Valitron没有任何内置的规则链选项。您的示例实际上并没有将规则作为一个链添加,它只是逐个添加规则。为了得到你想要的行为,你必须自己写。可能通过在“父”验证器的规则中嵌套第二个验证器。