我尝试在项目中验证2密码字段时出错了
这是我的控制器
public function showReg()
{
return View::make('reg');
}
public function addUser()
{
$uname = Input::get('uname');
$f_name = Input::get('name');
$pass = Input::get('pass');
$pass2 = Input::get('pass2');
$hashed = Hash::make($pass);
$hashed2 = Hash::make($pass2);
$val_uname = Validator::make(
array('User Name' => $uname),
array('User Name' => 'required|max:20|min:4')
);
$val_fname = Validator::make(
array('First Name' => $f_name),
array('First Name' =>'required|max:20|min:4')
);
$val_pass = Validator::make(
array('Password' => $pass),
array('Password' => 'required|min:6|max:8')
);
$val_pass2 = Validator::make(
array('Password confimation' => $pass2),
array('Password confimation' => 'required|min:6|max:8|same:pass')
);
if ($val_uname->fails())
{
return Redirect::to('reg')->withErrors($val_uname);
}
if ($val_fname->fails())
{
return Redirect::to('reg')->withErrors($val_fname);
}
if ($val_pass->fails())
{
return Redirect::to('reg')->withErrors($val_pass);
}
if ($val_pass2->fails())
{
return Redirect::to('reg')->withErrors($val_pass2);
}
$user_data = new User;
$user_data->uname = $uname;
$user_data->name = $f_name;
$user_data->pass = $hashed;
$user_data->save();
return Redirect::to('sucess');
}
}
这是我的观看代码
@extends('master')
@section('main')
{{-- the Forms to post the information to database to create a user --}}
{{ Form::open(array('url' => 'reg')) }}
{{ Form::label('uname', 'User Name : ') }}
{{ Form::text('uname') }}
<br>
{{ Form::label('name', 'First Name : ') }}
{{ Form::text('name') }}
<br>
{{ Form::label('pass', 'Password : ') }}
{{ Form::Password('pass') }}
<br>
{{ Form::label('pass2', 'Password confirmation : ') }}
{{ Form::Password('pass2') }}
<br>
{{ Form::submit('Submit') }}
{{ Form::close()}}
{{ $errors->first() }}
@endsection
和我的错误
密码配置和密码必须匹配。
我不知道我的代码中的错误部分在哪里
感谢@Sam,感谢他的帮助和支持
答案 0 :(得分:2)
您可以尝试以下方式:
...
$uname = Input::get('uname');
$f_name = Input::get('name');
$pass = Input::get('pass');
$pass2 = Input::get('pass_confirmation');
$hashed = Hash::make($pass);
$hashed2 = Hash::make($pass2);
...
$rules = array(
'uname' => 'required|max:20|min:4',
'name' => 'required|max:20|min:4',
'pass' => 'required|min:6|max:8|confirmed',
'pass_confirmation' => 'required|min:6|max:8|same:pass'
);
$v = Validator::make($input, $rules);
if( $v->fails() ) {
$messages = $v->getMessages()->all();
}
else {
//validation passed
}
Confirmed rule确保对于给定属性,存在匹配的attribute_confirmation属性。并将确认密码的html更改为:
{{ Form::label('pass2', 'Password confirmation : ') }}
{{ Form::Password('pass_confirmation') }}
答案 1 :(得分:0)
您不需要使用不同的验证器来验证不同的字段。您可以使用一个验证器来完成。您已使用same:pass
验证密码确认,但验证工具中没有字段pass
。
代码的一个不太复杂的版本:
function addUser()
{
$uname = Input::get('uname');
$f_name = Input::get('name');
$pass = Input::get('pass');
$pass2 = Input::get('pass2');
$hashed = Hash::make($pass);
$hashed2 = Hash::make($pass2);
$input = array(
'uname' => $uname,
'name' => $f_name,
'pass' => $pass,
'pass2' => $pass2
);
$rules = array(
'uname' => 'required|max:20|min:4',
'name' => 'required|max:20|min:4',
'pass' => 'required|min:6|max:8',
'pass2' => 'required|min:6|max:8|same:pass'
);
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
return Redirect::to('reg')->withErrors($validator);
}
$user_data = new User;
$user_data->uname = $uname;
$user_data->name = $f_name;
$user_data->pass = $hashed;
$user_data->save();
return Redirect::to('sucess');
}