0


I want to make a registration form with laravel form validation.
in that case, i face a great problem.
form validation is working but errors are not displaying properly.

I see these error for email & phone : Validation.unique
for required fields : validation.required

here is my controller's code :

 public function homeregister(Request $request)
    {
      $this->validation($request);
      $request['password'] = bcrypt($request->password);
      $email_verification = time();
      $sms_verify_code = mt_rand(100000, 999999);
      $name = $request->fname.' '.$request->lname;
      $dob = $request->year.'-'.$request->month.'-'.$request->day;
      User::create(['email'=>$request->email,'password'=>$request['password'],'fname'=>$request->fname,'lname'=>$request->lname,'gender'=>$request->gender,'dob'=>$dob,'religion'=>$request->religion,'country'=>$request->country,'email_verify_code'=>$email_verification,'phone'=>$request->phone,'sms_verify_code'=>$sms_verify_code,'name'=>$name,'identifier'=>$request->fname,'profile_created_by'=>$request->profile_for]);
      $value = $request->email;
      $phone = $request->phone;
      Session::put('email', $value);
      Session::put('phone', $phone);
      $myuser = DB::table('users')->select()->where('email',Session::get('email'))->first();
      DB::insert('insert into tbl_profile_photo (userid,photo) values(?,?)',[$myuser->id,null]);
      DB::insert('insert into tbl_cover_photos (userid,photo) values(?,?)',[$myuser->id,null]);
      \Mail::to($value)->send(new wellcome);
      Auth::loginUsingId($myuser->id);
      Session::put('userid',$myuser->id);
      return redirect('/update-profile');
}

Validation function :

public function validation($request)
    {
        $validatedData = $request->validate([
            'fname' => 'required|max:255',
            'lname' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'phone' => 'required|unique:users|max:255',
            'gender' => 'required|max:255',
            'year' => 'required|max:255',
            'month' => 'required|max:255',
            'day' => 'required|max:255',
            'religion' => 'required|max:255',
            'country' => 'required|max:255',
            'password' => 'required|min:6',
    ]);
    }

My views code :

@if (count($errors) > 0)
@foreach ($errors->all() as $error)
<div class="alert alert-danger alert-dismissible mb-2" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
  <span aria-hidden="true">×</span>
</button>
  <strong>Oh snap!</strong> {{$error}}
</div>
@endforeach
@endif

Didn't understand ? whats the wrong ??

4
  • form validation is working but errors are not displaying properly. Please explain? Commented Feb 6, 2019 at 9:11
  • i see errors Validation.unique instead of email must be unique Commented Feb 6, 2019 at 9:15
  • so the issue is with the messages: laravel.com/docs/5.7/validation#customizing-the-error-messages Commented Feb 6, 2019 at 9:16
  • Use a request, don't do the validation in the controller. This is going to give you headaches later with the size of the methods and the controller laravel.com/docs/5.7/validation#form-request-validation Commented Feb 6, 2019 at 10:10

1 Answer 1

0

do you take same name as email in your database table ?if yes then unique validation is working otherwise it will not working.

Sign up to request clarification or add additional context in comments.

2 Comments

i say , my form validation is working but validation errors are not displaying properly
i think you have problem only in email and phone input fields

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.