我正在尝试使用form
进行简单的用户注册laravel4
。当我提交form
时,我目前收到此错误:
Symfony\Component\Debug\Exception\FatalErrorException thrown with message "Undefined class constant 'rules'"
Stacktrace:
#1 Symfony\Component\Debug\Exception\FatalErrorException in C:\wamp\www\growinlove.tld\app\controllers\UsersController.php:12
#0 Illuminate\Exception\Handler:handleShutdown in <#unknown>:0
这是我的Controller
:
class UsersController extends BaseController {
//Run when user visits site...Load a view
public function index(){
return View::make('home.index');
}
//Handles processing of registration form data
public function postCreate(){
$validator = Validator::make(Input::all(), User::rules);
if($validator->passes()){
//Save in DB - Success
$user = new User;
$user->fname = Input::get('fname'); //Get the details of form
$user->lname = Input::get('lname');
$user->email = Input::get('email');
$password->password = Hash::make(Input::get('password'));//Encrypt the password
$user->save();
return Redirect::to('/books')->with('Thank you for Registering!');
}else{
//Display error - Failed
return Redirect::to('/')->with('message', 'The Following Errors occurred')->withErrors($validator)->withInput();
}
}
}
这是我的View
:
<!-- Register Form -->
<form action="{{ action('UsersController@postCreate') }}" method="post" role="form">
<h2 class="form-signup-heading">Register</h2>
<!-- Display Errors -->
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
<!-- First Name -->
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="fname" />
</div>
<!-- Last Name -->
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lname" />
</div>
<!-- Email -->
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" />
</div>
<!-- Password-->
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" />
</div>
<!-- Confirm Password -->
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" name="password_confirmation" />
</div>
<input type="submit" value="Register" class="btn btn-primary"/>
</form>
这是我的Model
:
public static $rules = array(
'firstname'=>'required|alpha|min:2',
'lastname'=>'required|alpha|min:2',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:6,12|confirmed',
'password_confirmation'=>'required|alpha_num|between:6,12'
);
use UserTrait, RemindableTrait;
答案 0 :(得分:4)
问题出在UserController
的第12行。
相反,User::rules
使用User::$rules
。
答案 1 :(得分:2)
替换你的控制器
$validator = Validator::make(Input::all(), User::rules);
与
$validator = Validator::make(Input::all(), User::$rules);