laravel框架中注册信息验证
1.路由配置
<?php
Route::get('/',['as'=>'blog.index','uses'=>'BlogController@index']);
Route::get('/create',['as'=>'blog.create','uses'=>'BlogController@create']);
Route::post('blog/store',['as'=>'blog.store','uses'=>'BlogController@store']);
2. 控制器分配页面及验证表单提交内容
<?php
class BlogController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('blog.index');
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
return View::make('blog.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$Input = Input::all();
$rules = array(
'username' => 'required',
'password' => 'required',
'rpassword' => 'required|same:password',
'email' => 'required|email',
'sex' => 'required|boolean'
);
$validator = Validator::make($Input, $rules);
if ($validator->fails())
{
//dd('123');
return Redirect::to('/create')->withErrors($validator, 'login');
}
// dd('456');
return Redirect::to('/');
}
3.form 表单验证
{{ Form::open(array('url' => 'blog/store', 'method' => 'post')) }}
<div class="form-group">
<label >用户名:</label>
{{ Form::text('username','',['class'=>'form-control','placeholder' => 'username']) }}
</div>
<div class="form-group">
<label >密 码:</label>
{{ Form::password('password',['class'=>'form-control','placeholder' => 'Password']) }}
</div>
<div class="form-group">
<label >重复密码:</label>
{{ Form::password('rpassword',['class'=>'form-control','placeholder' => 'RePassword']) }}
</div>
<div class="form-group">
<label >邮箱:</label>
{{ Form::text('email','',['class'=>'form-control','placeholder' => 'email']) }}
</div>
<div class="form-group">
<label for="exampleInputFile">上传照片:</label>
{{ Form::file('file', ['class' => 'file']) }}
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="form-group">
<label for="exampleInputFile">性别:</label>
{{ Form::radio('sex','1') }}男 {{ Form::radio('sex','0') }}女
</div>
<div class="form-group">
{{ Form::submit('提交', array('class' => 'btn btn-primary')) }}
{{ Form::button('取消', array('class' => 'btn btn-default')) }}
</div>
<div class="alert alert-danger" role="alert">
{{ $errors->login->first() }}
</div>
{{ Form::close() }}
</div>
</div>
<script type="text/javascript">
$('.alert').delay(800).slideUp();
</script>
4.引入css文件和js文件
{{ HTML::style('blog/css/bootstrap.min.css') }}
{{ HTML::script('blog/js/jquery-1.8.3.min.js') }}
{{ HTML::script('blog/js/bootstrap.min.js') }}
<?php
Route::get('/',['as'=>'blog.index','uses'=>'BlogController@index']);
Route::get('/create',['as'=>'blog.create','uses'=>'BlogController@create']);
Route::post('blog/store',['as'=>'blog.store','uses'=>'BlogController@store']);
2. 控制器分配页面及验证表单提交内容
<?php
class BlogController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('blog.index');
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
return View::make('blog.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$Input = Input::all();
$rules = array(
'username' => 'required',
'password' => 'required',
'rpassword' => 'required|same:password',
'email' => 'required|email',
'sex' => 'required|boolean'
);
$validator = Validator::make($Input, $rules);
if ($validator->fails())
{
//dd('123');
return Redirect::to('/create')->withErrors($validator, 'login');
}
// dd('456');
return Redirect::to('/');
}
3.form 表单验证
{{ Form::open(array('url' => 'blog/store', 'method' => 'post')) }}
<div class="form-group">
<label >用户名:</label>
{{ Form::text('username','',['class'=>'form-control','placeholder' => 'username']) }}
</div>
<div class="form-group">
<label >密 码:</label>
{{ Form::password('password',['class'=>'form-control','placeholder' => 'Password']) }}
</div>
<div class="form-group">
<label >重复密码:</label>
{{ Form::password('rpassword',['class'=>'form-control','placeholder' => 'RePassword']) }}
</div>
<div class="form-group">
<label >邮箱:</label>
{{ Form::text('email','',['class'=>'form-control','placeholder' => 'email']) }}
</div>
<div class="form-group">
<label for="exampleInputFile">上传照片:</label>
{{ Form::file('file', ['class' => 'file']) }}
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="form-group">
<label for="exampleInputFile">性别:</label>
{{ Form::radio('sex','1') }}男 {{ Form::radio('sex','0') }}女
</div>
<div class="form-group">
{{ Form::submit('提交', array('class' => 'btn btn-primary')) }}
{{ Form::button('取消', array('class' => 'btn btn-default')) }}
</div>
<div class="alert alert-danger" role="alert">
{{ $errors->login->first() }}
</div>
{{ Form::close() }}
</div>
</div>
<script type="text/javascript">
$('.alert').delay(800).slideUp();
</script>
4.引入css文件和js文件
{{ HTML::style('blog/css/bootstrap.min.css') }}
{{ HTML::script('blog/js/jquery-1.8.3.min.js') }}
{{ HTML::script('blog/js/bootstrap.min.js') }}