我正在使用Laravel4.2.8并试图验证下一个表单:

需要第一个select字段。接下来的三个字段只需要一个。带有格式的电话是最后一个。另外两个是数字(一些ID)。我在控制器中验证,下面是代码:
public function getApplication()
{
$input = Input::except('_token');
Debugbar::info($input);
$input['phone'] = preg_replace('/[^0-9]/', '', $input['phone']); // remove format from phone
$input = array_map('intval', $input); // convert all numeric data to int
Debugbar::info($input);
$rules = [ // Validation rules
['operation-location' => 'required|numeric'],
['app-id' => 'numeric|min:1|required_without_all:card-id,phone'],
['card-id' => 'numeric|digits:16|required_without_all:app-id,phone'],
['phone' => 'numeric|digits:12|required_without_all:app-id,card-id']
];
$validator = Validator::make($input, $rules);
if ($validator->passes()) {
Debugbar::info('Validation OK');
return Redirect::route('appl.journal', ['by' => 'application']);
}
else { // Validation FAIL
Debugbar::info('Validation error');
// Redirect to form with error
return Redirect::route('appl.journal', ['by' => 'application'])
->withErrors($validator)
->withInput();
}
}如您所见,我自己将数字ID转换为整数,只为电话号码留号。问题是当我提交表单时,它是通过验证的,尽管需要一个字段,并且初学者电话格式太短。我尝试在所有字段(!)上将required_without_all更改为required (!),但在提交空白空表单时,它仍然可以通过。我希望至少有一个领域会得到适当的填补。
调试我的输入。初步:
array(4) [
'operation-location' => string (1) "0"
'app-id' => string (0) ""
'card-id' => string (0) ""
'phone' => string (6) "+3 8(0"
]在转换为int之后:
array(4) [
'operation-location' => integer 0
'app-id' => integer 0
'card-id' => integer 0
'phone' => integer 380
]在Laravel问题上发布了类似的小问题。
发布于 2014-08-16 05:50:50
我知道这听起来很奇怪,但我认为这只是你的规则数组的一个问题。
当前的规则数组是数组的数组。Validator查找具有键和值的数组。我相信你目前的规则被解析为钥匙,但没有任何价值。然后,Validator基本上没有看到任何规则,并且自动通过。尝尝这个。
$rules = [
'operation-location' => 'required|numeric',
'app-id' => 'numeric|min:1|required_without_all:card-id,phone',
'card-id' => 'numeric|digits:16|required_without_all:app-id,phone',
'phone' => 'numeric|digits:12|required_without_all:app-id,card-id'
];https://stackoverflow.com/questions/25323629
复制相似问题