iT邦幫忙

0

Laravel-Auth 的用戶註冊 改用phone,錯誤訊息如何設置?

  • 分享至 

  • xImage
  •  

請教大神門:
我客戶要求會員需使用電話號碼註冊,我原本使用Laravel 自帶的

composer require laravel/ui --dev
php artisan ui vue --auth

生成註冊功能:
原本使用 email註冊,若 email有重複時,會提醒,但改為 phone卻直接報錯
https://ithelp.ithome.com.tw/upload/images/20200218/20121754fLW8BGdD76.png

請教我該從哪裡設置錯誤訊息?
我的register.blade.php 'phone', 'E-mail'如下:



                        <div class="form-group row">
                            <label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('phone') }}</label>

                            <div class="col-md-6">
                                <input id="phone" type="phone" class="form-control @error('phone') is-invalid @enderror" name="phone" value="{{ old('phone') }}" required autocomplete="phone">

                                @error('phone')
                                    <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>

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
firecold
iT邦新手 1 級 ‧ 2020-02-18 10:39:12
<input id="phone" type="phone" class="form-control @error('phone') is-invalid @enderror" name="phone" value="{{ old('phone') }}" required autocomplete="phone">

type改成text
看更多先前的回應...收起先前的回應...

已經修改過,但是無效果



                        <div class="form-group row">
                            <label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('phone') }}</label>

                            <div class="col-md-6">
                                <input id="phone" type="text" class="form-control @error('phone') is-invalid @enderror" name="phone" value="{{ old('phone') }}" required autocomplete="phone">

                                @error('phone')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
firecold iT邦新手 1 級 ‧ 2020-02-18 10:56:04 檢舉

你是用laravel Validation?

我只在 RegisterController 和 Register,blade.php -添加 "phone"

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],            
            'phone' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:4', 'confirmed'],
        ]);
    }
        
protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }

其餘的都沒有動過

感謝您提醒。指引我找到答案,謝謝!

1
愛的code
iT邦新手 4 級 ‧ 2020-02-18 16:18:24

找到方法了,原來是那麼簡單:
方法就是只要在 RegisterController 加上 'unique:users'

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'], 
            'phone' => ['required', 'string', 'max:255', 'unique:users'], 
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

已經可以完美呈現了
https://ithelp.ithome.com.tw/upload/images/20200218/20121754A5gNySKwpi.png

firecold iT邦新手 1 級 ‧ 2020-02-18 17:56:22 檢舉

恭喜

我要留言

立即登入留言