Hey guys i am new to laravel and i am trying to build a college project. In the signup part i am not able to validate the users password and email. If the credentials are correct it redirects me to previous page just as i want. But if the credentials are wrong then it is not redirecting me to home page but the page just reloads.
Validation function:
public function user_register(Request $request){
$res = false;
$request->validate([
"name"=>"required",
"email"=>"required|email|unique:custom__auths",
"password"=>"required|min:5|max:12"
]);
$user = new Custom_Auth();
$user->name=$request->name;
$user->email=$request->email;
$user->pasword=$request->password;
$res = $user->save();
echo $res;
if($res==1){
return back()->with('success','you have registered successfully');
}
else{
return redirect('/');
}
}
table in database:
public function up()
{
Schema::create('custom__auths', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->string("email")->unique();
$table->string("password");
$table->timestamps();
});
}
Signup view:
@extends('Layouts.Master')
@section('content')
<link rel="stylesheet" href="{{asset('/Assets/CSS/Auth.css')}}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div >
<input type="checkbox" id="flip">
<div >
<div >
<img src="https://cdn.pixabay.com/photo/2018/01/31/07/36/sunset-3120484_960_720.jpg" alt="">
<div >
<span >Helping you grow<br> stronger each day</span>
<span >Let's code together</span>
</div>
</div>
<div >
<img src="https://cdn.pixabay.com/photo/2018/01/31/07/36/sunset-3120484_960_720.jpg" alt="">
<div >
<span >Complete miles of journey <br> with one step</span>
<span >Let's get started</span>
</div>
</div>
</div>
<div >
<div >
<div >
<div >Signup</div>
<form action="{{route('UserAuth')}}" method="post">
@csrf
<div >
<div >
<i ></i>
<input name="name" type="text" placeholder="Enter your name" required>
</div>
<div >
<i ></i>
<input name="email" type="email" placeholder="Enter your email" required>
</div>
<div >
<i ></i>
<input name="password" type="password" placeholder="Enter your password" required>
</div>
<div >
<input type="submit" value="Sumbit">
</div>
<div >Already have an account?<a href="/login" style="color: blue"> Login now</a></div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
I tried to use dd to understand the root cause of problem but unfortunately i am not able to get to the root cause of the problem. Any help will be greatly appreciated !!
CodePudding user response:
I think that "page reloads" means validation failed but you didn't show the error messages.
From Laravel docs, what happens if $request->validate()
fails:
"If validation fails during a traditional HTTP request, a redirect response to the previous URL will be generated."
So it automatically redirects back, exactly what you're saying - "page reloads".
I assume you just don't show the validation errors.
Again, from the same docs, example how to show them:
@if ($errors->any())
<div >
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
The $errors
variable will automatically be filled from the session.