duankuan5319 2017-02-01 04:01
浏览 34
已采纳

在表单上创建附加验证,注册默认的laravel

This users(mstanggota) table:

 Schema::create('mstanggota', function (Blueprint $table) {
            $table->increments('id');
            $table->string('no_anggota',10);
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

Mstbeasiswas table:

Schema::create('mstbeasiswas', function (Blueprint $table) {
            $table->increments('id');
            $table->string('no_anggota',10)->unique();
            $table->string('nm_anak',25);
            $table->string('kt_lahir',25);
            $table->date('ttl');
            $table->String('nm_skl',50);
            $table->String('st_pend',6);
            $table->String('lbg_pend',6);
            $table->String('prov_skl');
            $table->String('jenkel',9);
            $table->integer('k_umum');
            $table->integer('k_khusus');
            $table->integer('score')->nullable();
            $table->boolean('status')->default(0);
            $table->string('ket',250)->nullable();
            $table->string('img1',50)->nullable();
            $table->string('img2',50)->nullable();
            $table->string('img3',50)->nullable();
            $table->timestamps();
        });

I would like to make an additional validation. If no_anggota does not exist on table mstbeasiswas then the user cannot register, what is there that could help me? :(

register form view-> enter image description here

  • 写回答

1条回答 默认 最新

  • duanhuiuw12555 2017-02-01 04:54
    关注

    Laravel surely works fine with no matter which way you've chose. Both traditional request and AJAX request are accepted.

    You can make your validation in controllers or creating another class. I prefer the second one in this situation to avoid let controller terrible and unclear.

    I assume you've read the chapter of Validation.

    1. making a class by artisan command

    $ php artisan make:request UserRegisterRequest
    

    2. editing validation rules (change with yours)

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|max:10',
            'nickname' => 'required|max:20',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6',
            'permission' => 'required|numeric',
        ];
    }
    

    3. set names for each field (change with your language)

    public function attributes ()
    {
        return [
            'name' => '姓名',
            'nickname' => '綽號',
            'email' => 'E-Mail',
            'password' => '密碼',
            'permission' => '權限',
        ];
    }
    

    4. response if error

    public function response(array $errors) {
        return response()->json([
            'status' => false,
            'errors' => $errors
        ]);
    }
    

    5. validate each request before getting in the controller

    public function account_add(UserRegisterRequest $request) {
    
        // pass the validation
    }
    

    The difference between traditional request and AJAX is that you can return redirection with errors or JSON. In this case, we've chose JSON instead of redirection. So that you can handle your errors at front-end.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本