I am trying to validate the login form and display errors on the page. But i am getting a 422 error when i click on login.
Any help would be appreciated.
This is my handleLogin function that handles the processes when login button is clicked.
public function handleLogin(Request $request){
/* validate user */
$this->validate($request, User::$login_validation_rules);
$data = $request->only('email', 'password');
if(\Auth::attempt($data)){
return redirect()->route('home');
}
return back()->withInput();
}
The following is the routes.php
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController#login']);
Route::post('/handleLogin', ['as' => 'handleLogin', 'uses' => 'LoginController#handleLogin']);
Route::get('/home', ['as' => 'home', 'uses' => 'UsersController#home']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController#logout']);
Route::resource('users', 'UsersController', ['only' => ['create', 'store']]);
And the following is the login form.
#extends('master')
#section('content')
<h2> Login </h2>
#if(count($errors))
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', array('class' => 'form-control')) !!}
</div>
{!! Form::token() !!}
{!! Form::submit('Login', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
#stop
Related
Im trying to use a form to submit reviews for products but I believe the submit button uses the incorrect controller store method. I have a controller for products and one for reviews. The products store works correctly and I can see the database being populated once submitted however when I go to submit a review for a product it will throw the custom error messages from the product store form. If I change the reviews form::open to the products form::open it will throw an error: The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Products form (works properly)
{!! Form::open(['action' => 'App\Http\Controllers\ProductsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
... labels and text ...
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
Reviews form
<div>
<p>Write a review</p>
<!-- submit review form -->
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
<div class="form-group">
{{ Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Write your message']) }}
</div>
<div class="form-group">
{{ Form::label('rating', 'Rating') }}
{{ Form::select('rating', ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'], '1') }}
</div>
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
</div>
ReviewsController store
public function store(Request $request, $id)
{
$this->validate($request, ['description' => 'nullable',
'rating' => 'nullable',
]);
$review = new Review;
$review->rating = $request->input('rating');
$review->reviewerid = auth()->user()->id;
$review->productid = $id;
$review->description = $request->input('description');
$review->save();
return redirect('/products/$id')->with('success', 'Review submitted');
}
Web.php file
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#abouts');
Route::get('/cart', 'App\Http\Controllers\PagesController#cart');
Route::get('/checkout', 'App\Http\Controllers\PagesController#checkout');
Route::get('/dashboard', 'App\Http\Controllers\PagesController#services');
Route::get('/categories/{Category}', 'App\Http\Controllers\PagesController#category');
Route::resource('reviews', 'App\Http\Controllers\ReviewsController');
Route::resource('products', 'App\Http\Controllers\ProductsController');
Auth::routes();
The error is because of this line:
Form::hidden('_method', 'PUT')
You're telling laravel to use put method. Delete it and it will work fine.
For your form action, I think you have type in this line:
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
Change it to :
{!! Form::open(['action' => 'App\Http\Controllers\ReviewsController#store']) !!}
I am using Laravel 5.3, the task that i am currently working on is the Form routing.
This is my routes.php file.
Route::group(['middleware' => 'web'], function() {
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController#login']);
Route::post('/handleLogin', ['as' => 'handleLogin', 'uses' => 'LoginController#handleLogin']);
});
The actual Form code in the view.
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', array('class' => 'form-control')) !!}
</div>
{!! Form::token() !!}
{!! Form::submit('Login', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
The controller that has the handle function.
/* handleLogin function to request the data*/
public function handleLogin(Request $request){
$data = $request-> only('email', 'password');
if(\Auth::attempt($data)){
return 'Is Logged In';
return redirect()-> intended('/home');
}
return back()->withInput();
}
When i click on Login button, a blank page is displayed instead of the page that would display 'Is Logged In'.
Any help would be appreciated.
You should remove web middleware from routes file since you're using Laravel 5.3 in which web middleware is added automatically. Adding it manually will cause problems.
I am trying to set up a contact form on a one page site using laravel, I can't seem to be able to get the form to validate the user input and show any errors that the form might have.
email.blade.php:
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
{!! Form::open(['route' => 'mail', 'method' => 'post', 'role' => 'form', 'id' => 'footer-form']) !!}
<div class="form-group has-feedback">
{!! Form::label('first_name', null, ['class' => 'sr-only']) !!}
{!! Form::text('first_name', null, ['class' => 'form-control', 'placeholder' => 'First Name']) !!}
<i class="fa fa-user form-control-feedback"></i>
#if($errors->has('first_name'))
{{ $errors->first('first_name') }}
#endif
</div>
<div class="form-group has-feedback">
{!! Form::label('last_name', null, ['class' => 'sr-only']) !!}
{!! Form::text('last_name', null, ['class' => 'form-control', 'placeholder' => 'Last Name']) !!}
<i class="fa fa-user form-control-feedback"></i>
#if($errors->has('last_name'))
{{ $errors->first('last_name') }}
#endif
</div>
<div class="form-group has-feedback">
{!! Form::label('email', null, ['class' => 'sr-only']) !!}
{!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Email address']) !!}
<i class="fa fa-envelope form-control-feedback"></i>
#if($errors->has('email'))
{{ $errors->first('email') }}
#endif
</div>
<div class="form-group has-feedback">
{!! Form::label('textarea', null, ['class' => 'sr-only']) !!}
{!! Form::textarea('textarea', null, ['class' => 'form-control', 'rows' => 8, 'placeholder' => 'Message']) !!}
<i class="fa fa-pencil form-control-feedback"></i>
#if($errors->has('textarea'))
{{ $errors->first('textarea') }}
#endif
</div>
{!! Form::submit('Send', ['class' => 'btn btn-default']) !!}
{!! Form::close() !!}
Route: web.php:
Route::get('/ensignhospital', [
'as' => 'home',
'uses' => 'HomeController#home'
]);
Route::group(['before' => 'guest'], function () {
/*
* CSRF Protection
*
* */
Route::group(['before' => 'csrf'], function () {
Route::post('/ensignhospital', [
'as' => 'mail',
'uses' => 'HomeController#postSendMail'
]);
});
});
controller to handle for request:
class HomeController extends Controller {
public function home(){
return View('welcome');
}
public function postSendMail(ContactFormRequest $request){
if($request->fails()){
return Redirect::route('')
->withErrors()
->withInput();
}else{
return View('passed');
}
}
}
The form request validator class:
class ContactFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
'message' => 'required'
];
}
}
Problem the form does not validate when I don't enter a valid input. I need the form to validate user input and remain at the form position on the web page.
please note:
I used a .htaccess file to get the site which is on my computer to display the site root as localhost/mylaravelproject rather than the usual localhost/mylaravelprojec/public that one will normally see on a fresh install of laravel.
From the documentation here: https://laravel.com/docs/5.3/validation#form-request-validation
So, how are the validation rules evaluated? All you need to do is type-hint
the request on your controller method. The incoming form request is validated
before the controller method is called, meaning you do not need to clutter
your controller with any validation logic.
Laravel will never actually remain on the form. It will instead redirect, validate and then redirect back, with the errors. You do not need the check in your code if($request->fails()).
Today I have the following problem with this routes , It has never happened to me before now.
{!! Form::open(array('route' => 'subastas/creado', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Your Name') !!}
{!! Form::text('name', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your name')) !!}
</div>
<div class="form-group">
{!! Form::label('Your E-mail Address') !!}
{!! Form::text('email', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your e-mail address')) !!}
</div>
<div class="form-group">
{!! Form::label('Your Message') !!}
{!! Form::textarea('message', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your message')) !!}
</div>
<div class="form-group">
{!! Form::submit('Contact Us!',
array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
In my route controller
Route::post('subastas/creado', array(
'as' => 'subastas/creado',
'uses' => 'SubastaController#creado'
));
My controller
public function creado()
{
$usuario = new Subasta();
$usuario->name= \Request::input('name');
$usuario->save();
}
When I send the form I recieve this url ? Any idea about this problem ?
http://localhost/laravel30/public/subastas/create?_token=X93VGoFhFL9YaPYZfrTlyvn0ph9KE6Om00KmMaiv&name=asdafs&email=kfh1992%40gmail.com&message=
I assume you have another route of subastas/creado for the GET request to display the form.
In your Form::open() you're using that to generate the URL, laravel is seeing that as a GET route as thats the first one registered in your routes.php and changing the form method to GET rather than the expected POST
The solution is to change the name of the route and use that in your Form::open()
Route::post('subastas/creado', [
'as' => 'subastas/creado/post',
'uses' => 'SubastaController#creado',
]);
Then you can use the following to generate the correct form opening tag.
Form::open(['route' => 'subastas/creado/post'])
I'm not sure why it's not working...
I'm trying to update a user and I keep getting method not allowed error exception.
-- routes
Route::get('superadmin/users', ['as' => 'superadmin.users', 'uses' => 'SuperAdminController#usersIndex']);
Route::post('superadmin/users/{id}', ['as' => 'superadmin.editUser', 'uses' => 'SuperAdminController#editUser']);
-- controller
public function usersIndex()
{
$users = User::all();
return View::make('superadmin.users',compact('users'));
}
public function editUser($id)
{
$user = User::findOrFail($id);
$user->email = Input::get('email');
$user->save();
return Redirect::route('superadmin.users')->with('alertsuccess', 'User has been updated.');
}
-- view
{{ Form::model($user, ['method' => 'PATCH', 'route' => ['superadmin.editUser', $user->id], 'class' => 'form']) }}
<div class="form-group">
{{ Form::label('email', 'Email:', ['class' => 'placeholder-hidden']) }}
{{ Form::text('email', Input::old('email'), ['class' => 'form-control']) }}
</div>
{{ Form::submit('Update User', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}
This is most probably as you need to set up a resource controller in order to use the PATCH method. Try using POST instead.