验证器不使用指定的规则,在失败时传递,使用方式/数据库

时间:2015-01-29 12:03:14

标签: php laravel laravel-4 laravel-validation

我正在使用way/database包进行Laravel 4.2验证,并设置了一个简单的用户注册方法。

我正在尝试使用已存在的电子邮件地址创建新用户来测试此问题。验证器返回true,然后继续给出错误:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'test2@test.com' for key 'users_email_unique' (SQL: insert into `users` (`email`, `password`, `updated_at`, `created_at`) values (test2@test.com, 123, 2015-01-29 11:50:37, 2015-01-29 11:50:37))

这样我的模特出了什么问题?

控制器:

public function store()
{
    $user = User::create(Input::only('email', 'password'));

    if ($user->hasErrors()){
        return Response::json(array(
            'error' => $user->getErrors()
        ));
    }

    Auth::login($user);

    return Response::json(array('success' => 'true'));
}

User.php型号:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Model implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $fillable = array(
        'email', 'password'
    );

    protected static $rules = [
        'email' => 'required:unique'
    ];

    //Use this for custom messages
    protected static $messages = [
        'email.required' => 'An email address is required'
    ];

    /**
     * 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');

}

以下是way/database的验证模型:

class Model extends Eloquent {

    /**
     * Error message bag
     * 
     * @var Illuminate\Support\MessageBag
     */
    protected $errors;

    /**
     * Validation rules
     * 
     * @var Array
     */
    protected static $rules = array();

    /**
     * Custom messages
     * 
     * @var Array
     */
    protected static $messages = array();

    /**
     * Validator instance
     * 
     * @var Illuminate\Validation\Validators
     */
    protected $validator;

    public function __construct(array $attributes = array(), Validator $validator = null)
    {
        parent::__construct($attributes);

        $this->validator = $validator ?: \App::make('validator');
    }

    /**
     * Listen for save event
     */
    protected static function boot()
    {
        parent::boot();

        static::saving(function($model)
        {
            return $model->validate();
        });
    }

    /**
     * Validates current attributes against rules
     */
    public function validate()
    {
        $v = $this->validator->make($this->attributes, static::$rules, static::$messages);

        if ($v->passes())
        {
            return true;
        }

        $this->setErrors($v->messages());

        return false;
    }

    /**
     * Set error message bag
     * 
     * @var Illuminate\Support\MessageBag
     */
    protected function setErrors($errors)
    {
        $this->errors = $errors;
    }

    /**
     * Retrieve error message bag
     */
    public function getErrors()
    {
        return $this->errors;
    }

    /**
     * Inverse of wasSaved
     */
    public function hasErrors()
    {
        return ! empty($this->errors);
    }

}

有人能指出我做错了吗?

1 个答案:

答案 0 :(得分:1)

尝试更改

protected static $rules = [
    'email' => 'required:unique'
];

protected static $rules = [
    'email' => 'required|unique'
];

Laravel文档会有所帮助 http://laravel.com/docs/4.2/validation