1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel email(unique)【バリデーションエラー】:Validation rule unique requires at least 1 parameters.

Last updated at Posted at 2019-08-07

#やりたい事

  • 自分でuserformを作成して、Laravelに元々入っているusers_tableにデータを入れたい
  • メールアドレスはuniqueで同じものがあれば「既に使用されています」と表示したい

#Error
Validation rule unique requires at least 1 parameters.

#結果
UserFormのbuildFormのemail欄
'rules' => 'required|unique',

↓ *テーブル名を入れた

'rules' => 'required|unique:users',

*参考になったサイト
(https://www.oipapio.com/question-5966264)

#Controller
public function register()
{

    $form = $this->form(UserForm::class);

    if (!$form->isValid()) {
        return redirect()->back()->withErrors($form->getErrors())->withInput();
    }

    $user = $form->save();
    logger($user);

    Auth::guard()->login($user);

    return redirect(route('customer_top'));
}

#View


Register
            <div class="card-body">
                <form method="POST" action="{{ route('register') }}">
                    {{ csrf_field() }}

                    @if ($errors->any())
                    <div class="alert alert-danger">
                        <ul>
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                    @endif

                    {!! form_label($form->name) !!}
                    {!! form_widget($form->name) !!}

                    {!! form_label($form->email) !!}
                    {!! form_widget($form->email) !!}

                    {!! form_label($form->password) !!}
                    {!! form_widget($form->password) !!}

                    {!! form_label($form->password_confirmation) !!}
                    {!! form_widget($form->password_confirmation) !!}

                    <button type="submit" class="btn btn-primary justify-content-center">Register</button>


                </form>

            </div>
        </div>
    </div>

#Form
public function buildForm()
{
$this
->add('name','text',[
'label' => '氏名',
'rules' => 'required'
])
->add('email','email',[
'label' => 'メールアドレス',
'rules' => 'required|unique:users',
])

        ->add('password','password',[
            'rules' => 'required|confirmed',
            'label' => 'パスワード',
        ])

        ->add('password_confirmation','password',[
            'rules' => 'required',
            'label' => 'パスワード確認',

        ]);

}

#validation.php
他にもエラーメッセージを日本語に変える方法はあると思いますが、
一番早そうなのでこの方法でやりましたφ(..)
他のエラーメッセージのやり方がありましたら教えていただけると、初心者プログラマー津田は嬉しいでございます!::bow_tone2:

'unique' => 'このメールアドレスは既に使用されています',
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?