doukuang1897 2014-09-01 20:12
浏览 34
已采纳

Laravel独特的验证问题

I try to make a unique validation the settings of my website but this doesn't work :

In my controller :

$rules = array(
    'username' => 'required|unique:User,username,10',
    'email'    => 'required|email|unique:User,email,10',
    'language' => 'required|in:fr,en',
);

My model:

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;
    protected $primaryKey = 'id_user';
    protected $table = 'user';

}

The problem is:

My Validator Validator::make(Input::all(), $rules, $messages); fails, it says that this username and email already exist.

  • 写回答

2条回答 默认 最新

  • doucong7963 2014-09-01 21:14
    关注

    Disagree with the answer to your own question:

    "Laravel is not done to search in a custom column".

    This is not true.

    To be precise: There is nothing bad in using a Plugin...


    See the important part of a migration file (app/database/migrations):

       // creates a DB-table named 'users'
        Schema::create('users', function (Blueprint $t) {
            $t->increments('id');
            $t->timestamps();
            // ... Here a unique field
            $t->string('user_email_one', 255)->unique();
            // ...
        });
    

    And the relevant validation rules in the UserController:

         $rules = array(
            'user_email_one' => 'required|email|unique:users',
            // ...
         );
    

    And Laravel is doing its job.


    With unique: you have to call the DB-table name, not the model name.

    BTW: the plugin you've chosen does this...

    The Laravel docs about validation:

    unique:table,column,except,idColumn

    The field under validation must be unique on a given database table.

    If the column option is not specified, the field name will be used.


    Just as an interesting info about naming a mySQL table 'User', 'user' or 'users', which could have caused your error. Visit this question:

    Is there a naming convention for MySQL? asked by StackOverflowNewbie, answered by Tom Mac (highest vote & accepted answer)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?