Laravel Authintication不验证所有字段

时间:2016-06-21 10:06:31

标签: laravel

我的laravel项目未验证我的注册表格的所有文件

网址:http://themovingpixel.com/myfinancialgoals/register

我的资源/ views / auth / register.blade.php代码在这里

    @extends('layouts.register')
@section('content')
<div class="stage_top_bg">Financial Advisors Register</div>
<div class="container">
  <div class="financial_mid_box">
    <form name="myform" action="{{ url('/register') }}" method="POST">
    {{ csrf_field() }}
    <div class="stage_box">
      <div class="stage_one_box stage_active steps">Stage 1</div>
      <div class="stage_two_box steps">Stage 2</div>
      <div class="stage_three_box steps">Stage 3</div>
      <div class="stage_four_box steps">Stage 4</div>
    </div>

    <div class="financial_form_box formone">
      <div class="col-lg-6 col-md-6">
        <div class="financial_fild_main_box"> <span class="name_text">Name</span>
          <div class="name_fild">
            <input name="name" class="name_main_fild" type="text" placeholder="Name">
          </div>
          @if ($errors->has('name'))
          <span class="help-block"><strong>{{ $errors->first('name') }}</strong></span>
          @endif
        </div>
      </div>
      <div class="col-lg-6 col-md-6">
        <div class="financial_fild_main_box"> <span class="name_text">Practice</span>
          <div class="name_fild">
            <input name="practice" class="name_main_fild" type="text" placeholder="Practice">
          </div>
          @if ($errors->has('practice'))
          <span class="help-block"><strong>{{ $errors->first('practice') }}</strong></span>
          @endif
        </div>
      </div>
      <div class="col-lg-6 col-md-6">
        <div class="financial_fild_main_box"> <span class="name_text">Phone</span>
          <div class="name_fild">
            <input name="phone_number" class="name_main_fild" type="tel" placeholder="Phone">
          </div>
          @if ($errors->has('phone_number'))
          <span class="help-block"><strong>{{ $errors->first('phone_number') }}</strong></span>
          @endif
        </div>
      </div>
      <div class="col-lg-6 col-md-6">
        <div class="financial_fild_main_box"> <span class="name_text">Email</span>
          <div class="name_fild">
            <input name="email" class="name_main_fild" type="text" placeholder="Email">
          </div>
          @if ($errors->has('email'))
          <span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
          @endif
        </div>
      </div>
      <div class="col-lg-6 col-md-6">
        <div class="financial_fild_main_box"> <span class="name_text">Password</span>
          <div class="name_fild">
            <input name="password" class="name_main_fild" type="password" placeholder="Password">
          </div>
        </div>
      </div>
      <div class="clearfix"></div>
      <div class="procced_button">Proceed</div>
    </div>


    <div class="financial_form_box stepsone">
      <div class="col-lg-6 col-md-6">
        <div class="financial_fild_main_box"> <span class="name_text">Package</span>
          <div class="name_fild">
            <select name="packages" class="name_main_fild">
              <option value="0">Select</option>
              <option value="45">$45/month</option>
              <option value="60">$60/month</option>
            </select>
          </div>
        </div>
      </div>
      <div class="clearfix"></div>
      <div class="procced_button btns">Proceed</div>
    </div>





    <div class="financial_form_box steptwo">
      <div class="financial_fild_main_box"> <span class="name_text">Payment</span>
        <div class="payament_box"> <!-- <a href="#">
          <div class="payament_icon"><img src="{{ asset('assets/frontend/images/payaple_icon.jpg')}}" alt="icon"></div>
          </a> <a href="#">
          <div class="payament_icon"><img src="{{ asset('assets/frontend/images/ebay_icon.jpg')}}" alt="icon"></div>
          </a> <a href="#">
          <div class="payament_icon"><img src="{{ asset('assets/frontend/images/pay_icon_three.jpg')}}" alt="icon"></div>
          </a> <a href="#">
          <div class="payament_icon"><img src="{{ asset('assets/frontend/images/pay_icon_four.jpg')}}" alt="icon"></div>
          </a> <a href="#">
          <div class="payament_icon"><img src="{{ asset('assets/frontend/images/pay_icon_six.jpg')}}" alt="icon"></div>
          </a> <a href="#">
          <div class="payament_icon"><img src="{{ asset('assets/frontend/images/pay_icon_seven.jpg')}}" alt="icon"></div>
          </a> <a href="#">
          <div class="payament_icon"><img src="{{ asset('assets/frontend/images/pay_icon_eight.jpg')}}" alt="icon"></div>
          </a> --> 
          <div class="name_fild">
            <select name="payment_type" class="name_main_fild">
              <option value="0">Select</option>
              <option value="paypal">Paypal</option>
              <option value="ebay">Ebay</option>
              <option value="cirrus">Cirrus</option>
              <option value="visa">Visa</option>
              <option value="discover">Discover</option>
              <option value="google">Google</option>
              <option value="eway">Eway</option>
            </select>
          </div>
          </div>
      </div>
      <div class="clearfix"></div>
      <div class="procced_button"><button type="submit" class="process_btn">Proceed</button></div>
    </div>
    </form>
  </div>
</div>
@endsection

我的app / Http / Controllers / Auth代码就在这里

<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Userinfo;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/profile';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'practice' => 'required',
            'password' => 'required|min:6|confirmed',
        ]);
    }
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {

        //Validation
        $this->validate($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'practice' => 'required|max:255',
        'phone_number' => 'required|number',
        'password' => 'required|min:6|confirmed',
        ]);

        $insertedUserInfo =  User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        // echo '<pre>'.print_r($userinfo,1).'</pre>';
        // exit;
        // $userinfo = new Userinfo;
        // $userinfo->user_id = $insertedUserInfo->id;
        // $userinfo->phone_number = $data['phone_number'];
        // $userinfo->address = $data['address'];
        // return $userinfo->save();
        Userinfo::create([
            'user_id' => $insertedUserInfo->id,
            'phone_number' => $data['phone_number'],
            'practice' => $data['practice'],
            'dob' => $data['dob'],
            'postcode' => $data['postcode'],
            'gender' => $data['gender']
        ]);
        return $insertedUserInfo;

    }
}

请帮助我验证从这里的数据注册到3tables用户,userinfo表。

2 个答案:

答案 0 :(得分:0)

首先,对于这种类型的步骤布局,您也应该进行一些客户端验证。 为此,您可以使用jquery validate。

Take a look at this

你的验证器只会验证数组

中的字段
    $this->validate($data, [
    'name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users',
    'practice' => 'required|max:255',
    'phone_number' => 'required|number',
    'password' => 'required|min:6|confirmed',
    ]);

因此,您必须将字段名称传递给验证数组以验证该字段。

答案 1 :(得分:0)

验证方法期望第一个参数作为Request实例,而不是您所做的数组($ data)。 https://laravel.com/api/5.1/Illuminate/Foundation/Validation/ValidatesRequests.html#method_validate