我有一个始终通过的验证器。如果用户名少于3个字符,我需要它失败。
以下是我的路线:
Route::get('/', function()
{
return View::make('validation.form');
});
Route::post('registration', function()
{
// Fetch all request data.
$data = Input::all();
var_dump($data);
// Build the validation constraint set.
$rules = array(
'username' => 'alpha_num|min:3' //or username =>array('alpha_num','min:3')
);
var_dump($rules);
// Create a new validator instance.
$validator = Validator::make($data, $rules);
if ($validator->passes()) {
// Normally we would do something with the data.
return 'Data was saved.';
}
return 'data not saved';
});
吼叫是'我'的形式
{{ Form::open(array('url' => 'registration')) }}
{{-- Username field. ------------------------}}
{{ Form::label('username', 'Username') }}
{{ Form::text('username') }}
<br/>
{{-- Email address field. -------------------}}
{{ Form::label('email', 'Email address') }}
{{ Form::email('email') }}
<br/>
{{-- Password field. ------------------------}}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
<br/>
{{-- Password confirmation field. -----------}}
{{ Form::label('password_confirmation', 'Password confirmation') }}
{{ Form::password('password_confirmation') }}
<br/>
{{-- Form submit button. --------------------}}
{{ Form::submit('Register') }}
{{ Form::close() }}
当我点击提交按钮时,我总是被路由到/注册保存的消息数据。为什么验证器不工作..?任何想法
答案 0 :(得分:1)
需要规则检查输入是否已填充..如果你给出一个没有require规则的空输入,验证器忽略min:3并且它成功..但如果你给两个字符作为输入它将失败。所以为了避免空数据使用需要规则..
答案 1 :(得分:0)
'username' => = 'required|alpha_num|min:3|max:15';