【发布时间】:2014-12-31 15:10:53
【问题描述】:
我正在尝试验证一些数据。我在 scotch.io 找到了这个tutorial。我在我的 UsersController 中使用以下内容来尝试验证一些数据:
public function store(){
$validator = Validator::make(Input::all(), User::$rules);
return Redirect::action("UserController@index");
}
但是,我不断收到错误消息“访问未声明的静态属性:User::$rules”。难道我做错了什么?我尝试使用'php artisan dump-autoload'
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
protected $fillable = array(
'username',
'forename',
'surname',
'email',
'telephone',
'password',
'admin',
'customer',
'verification_link'
);
public static $rules = array(
'name' => 'required', // just a normal required validation
'email' => 'required|email|unique:ducks', // required and must be unique in the ducks table
'password' => 'required',
'password_confirm' => 'required|same:password' // required and has to match the password field
);
}
【问题讨论】:
-
老实说,一切看起来都不错。再次尝试运行
php artisan dump-autoload。也许您不小心在另一个文件夹中运行了它(之前发生在我身上..)。或者尝试使变量成为对象,而不是静态的。 -
试过你的代码,没有出现错误。也许您可以将规则移到控制器中,因为它是负责验证的控制器,而不是模型
标签: php laravel laravel-4 laravel-validation