在Laravel中编辑了现成的身份验证表单,但现在不会提交给数据库

时间:2019-06-12 20:31:44

标签: laravel authentication laravel-5 laravel-blade

我在laravel中建立了一个新应用,并使用php artisan make:auth创建了开箱即用的身份验证。我需要在表单中添加一些其他字段,然后按照一个教程(我什至找不到)再重新运行迁移。新字段进入数据库,并且看起来都很不错。但是,当我尝试注册时,该表单不会提交到数据库,也不会重定向,并且许多字段都被清除了。

我发现在堆栈上有一篇文章提到我的表单视图可能与注册按钮弄混了,因此我一直在确保所有div均已关闭,但没有解决问题。我还认为问题可能与我的单选按钮和复选框有关,因此我删除了这些按钮和复选框以进行测试,但仍然存在相同的问题。

我的注册控制器:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * 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', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'phone' => ['required', 'string', 'max:15'],
            'program' => ['required'],
            'disability'=>['string', 'max:255'],
            'terms' => ['required'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'phone' => $data['phone'],
            'program' =>$data['program'],
            'disability'=>$data['disability'],
            'terms' => $data['terms'],
            'password'=> Hash::make($data['password']),
        ]);

    }
}

下面是我的表格(很抱歉分享整个内容)

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Register') }}</div>
                <div class="card-body">
                    <form method="POST" action="{{ route('register') }}">
                        @csrf
                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror"
                                    name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

                                @error('name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="email"
                                class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror"
                                    name="email" value="{{ old('email') }}" required autocomplete="email">

                                @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>
                        <div class="form-group row">
                                <label for="phone number"
                                    class="col-md-4 col-form-label text-md-right">{{ __('Phone Number') }}</label>
                            <div class="col-md-6">
                                    <input id="phone number" type="text"
                                        class="form-control @error('phone number') is-invalid @enderror"
                                        name="phone number" value="{{ old('phone') }}" required autocomplete="phone"
                                        autofocus>

                                    @error('phone')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                            </div>
                        </div>
                        <div class="form-group row">
                                <label for="program"
                                    class="col-md-4 col-form-label text-md-right">{{ __('Program') }}</label>
                                <div class="col-md-6 ">
                                    <div class="form-inline"><input id="veterans" type="radio" class="form-inline"
                                            name="program" value="Veteran Program ($35.00/month)"
                                            {{ (old('program')== 'veteran') ? 'checked':''}}>
                                        Veteran Program ($35.00/month
                                    </div>
                                    <div class="form-inline"><input id="masters" type="radio" class="form-inline"
                                            name="program" value="Masters Program ($50.00/month)"
                                            {{ (old('program')== 'masters') ? 'checked':''}}>
                                        Masters Program ($50.00/month)
                                    </div>
                                    <div class="form-inline"><input id="adaptive" type="radio" class="form-inline"
                                            name="program" value="Non-Veteran Adaptive Program ($50.00/month)"
                                            {{ (old('program')== 'adaptive') ? 'checked':''}}> Masters Adaptive Program
                                        ($50.00/month)
                                    </div>

                                    @error('program')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                        </div>
                        <div class="form-group row">
                                <label for="type"
                                    class="col-md-4 col-form-label text-md-right">{{ __('Disability Type (If applicable)') }}</label>
                                <div class="col-md-6">
                                    <input id="disability" type="text" class="form-control" name="disability"
                                        value="{{ old('disability') }}" autofocus>
                                </div>
                        </div>
                        <div class="form-group row">
                                <label for="waiver"
                                    class="col-md-4 col-form-label text-md-right">{{ __('Terms and Conditions') }}</label>
                                <div class="col-md-6 ">
                                    <div class="form-inline"><input id="waiver" type="checkbox" class="form-inline"
                                            name="waiver" value="waiver" {{ (old('waiver')== '1') ? 'checked':''}}> By
                                        checking this box, I agree to the ORCA terms & conditions. </div>
                                    @error('waiver')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>
                        <div class="form-group row">
                                <label for="password"
                                    class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                                <div class="col-md-6">
                                    <input id="password" type="password"
                                        class="form-control @error('password') is-invalid @enderror" name="password"
                                        required autocomplete="new-password">

                                    @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                        </div>
                        <div class="form-group row">
                                <label for="password-confirm"
                                    class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
                                <div class="col-md-6">
                                    <input id="password-confirm" type="password" class="form-control"
                                        name="password_confirmation" required autocomplete="new-password">
                                </div>
                        </div>
                        <div class="form-group row">
                                <div class="col-md-6 offset-md-4">
                                    <button type="submit" class="btn btn-primary">
                                        {{ __('Register') }}
                                    </button>
                                </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

我希望它可以创建帐户,登录和重定向。不幸的是,它没有抛出任何错误消息,这使得排除故障变得更加困难。

2 个答案:

答案 0 :(得分:0)

我试图发表评论,但我还无法发表评论,首先请检查电话的名称和术语输入,原因是您将电话输入中的“电话号码”放在名称属性中,对于术语输入,您将“豁免”,

请问您以后可以共享整个控制器吗?

答案 1 :(得分:0)

残疾字段需要更新以使其为空白。在寄存器控制器上的验证器函数中添加了“可空”,它可以正常工作。还需要在迁移中更新该列,以便数据库不会出错。