I am new to laravel and trying to use Validator class. It fails everytime though form post the data. i have googled and digged a lot to get answer from internet but none of them worked for me. Please stack guys help me out to get rid of this issue.
This is my Conntroller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
class AuthenticationController extends Controller
{
//
public function login()
{
return view('login');
}
public function register()
{
return view('register');
}
public function addUser(Request $request)
{
$Validator=Validator::make($request->all(),array('fname'=>'required'));
if($Validator->fails())
{
return redirect()->back()->withInput()->withErrors($Validator->messages());
}
}
}
This is register blade view file
<!DOCTYPE html>
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<title>Login</title>
</head>
<body>
<div class="wrapper fadeInDown">
<div id="formContent">
<!-- Tabs Titles -->
<!-- Login Form -->
<form method="POST" action="{{url('/register.submit')}}">
<input type="text" id="first-name" class="fadeIn second" name="
fname" placeholder="First Name">
@if($errors->has('fname'))
<div class="alert-danger">
{{$errors->first('fname')}}
</div>
@endif
@csrf
<input type="text" id="last-name" class="fadeIn second" name="
lastname" placeholder="Last Name">
@if($errors->has('last_name'))
<div class="alert-danger">
{{$errors->first('last_name')}}
</div>
@endif
<input type="text" id="email" class="fadeIn second" name="
email" placeholder="Email">
@if($errors->has('email'))
<div class="alert-danger">
{{$errors->first('email')}}
</div>
@endif
<input type="password" id="password" class="fadeIn third" name="login" placeholder="password">
@if($errors->has('password'))
<div class="alert-danger">
{{$errors->first('password')}}
</div>
@endif
<input type="password" id="confirm-password" class="fadeIn third" name="confirmpassword" placeholder="Confirm Password">
@if($errors->has('confirm_password'))
<div class="alert-danger">
{{$errors->first('confirm_password')}}
</div>
@endif
<input type="submit" class="fadeIn fourth" value="Register">
</form>
<!-- Remind Passowrd -->
<div id="formFooter">
<a class="underlineHover" href="{{url('login')}}">Already have account SignIn</a>
<a class="underlineHover" href="{{url('forget_password')}}">Forget Password</a>
</div>
</div>
</div>
</body>
</html>
Make sure your name
attributes are actually in one line and don't spread over two lines. Showing those in code tags here will result in:
name=" fname"
When you display such string in HTML you'll most likely see the whitespace stripped, but PHP/Laravel recognizes it as whitespace and thus your validation fails.