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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测