Laravel back()->withInput(); not working - laravel

I am currently following a video tutorial on a login form. Here's the code used in case Auth::attempt() fails:
return back()->withInput();
This should return the user to the form and fill out the inputs again (email, password). However the fields stay empty, while login is working correctly.
How can I fix this?
This is my form:
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group has-feedback">
{!! Form::text('email', null, array('class' => 'form-control', 'placeholder' => 'EMail')) !!}
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
{!! Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) !!}
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<label>
<input type="checkbox"> Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
{!! Form::token() !!}
{!! Form::submit(null, array('class' => 'btn btn-primary btn-block btn-flat')) !!}
</div>
<!-- /.col -->
</div>
{!! Form::close() !!}
Edit: Found out that the following code I have works for login but the else statement is not executed when the login credentials are wrong. why?
public function handleLogin(Request $request)
{
$this->validate($request, User::$login_validation_rules);
$data = $request->only('email', 'password');
if(\Auth::attempt($data)){
return redirect()->intended('home');
}else {
return back()->withInput();
}
}

So, as we discussed, the problem is the web middleware. In previous revisions of the Laravel 5.2 framework, if you wanted to enable sessions and error variables, you had to wrap the routes in the web middleware.
As of version 5.2.27, this is not the case anymore, since that middleware group is already applied to all routes, by default.
Knowingly, having version 5.2.27 or later, and using the web middleware group, causes issues with those same variables, common issues are session variables not being passed around, and the $errors variable provided by the class \Illuminate\View\Middleware\ShareErrorsFromSession, that is returned by the Laravel validation API is set, but empty**.
Summary
If you have Laravel 5.2.27 and later, you don't need to wrap the routes in the web middleware group.
If the version is lower than that, then you are required to do so, in order to use session variables and get validation errors.
Sources
https://github.com/laravel/framework/issues/13313
https://laravel.com/docs/5.2/middleware#middleware-groups

Out of the box, Laravel comes with web and api middleware groups that contains common middleware you may want to apply to web UI and your API routes:
App/Http/Kernal.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
'auth:api',
],
];
Take a look at web which contains StartSession and ShareErrorsFromSession which are responsible for session handling so if you want to show validation errors or persist old data then you have to include them in your middleware group. in this case its web.

You Have To make the condiotion if The form has been back with errors, it should been fill out.
The {{old('nom')}} function look for The Previous Filled Value for a given field and echo it.
you can use it as below:
<div class="col-sm-8">
<input type="text"
class="form-control form-control-sm #error('nom') is-invalid #enderror"
id="nom"
name="nom"
placeholder="Ecriver le nom votre ecole "
value="{{old('nom')}}"
>
</div>

try changing
return back()->withInput();
with
return redirect()->back()->withInput(\Input::all()

Related

Email works with route using get request but email will not send with post request and there are no errors just a page refresh

I have verified emails are sending via mailgun with a public function mail using a get request with no data on the page. Trying to send an email from a contact page with a POST request simply refreshes the page with no errors but email does not send.
I have set the .env and config.mail / config.services and successfully send mail using a get request in web.php.
First I used Terminal php artisan make:mail. Then I created two routes for a contact page (GET / POST), created a contact view (works), send the data to PagesController.php, run a validation of the three inputs, pass the data to Mail::send. On click of submit button the page refreshes with no errors (validation not working) and email doesn't send.
PagesController.php:
Namespace(
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Mail\Welcome;
use Mail;
class PagesController extends Controller
{
public function getContact(){
return view('pages/contact');
}
public function postContact(Request $request){
$this->validate($request,[
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min: 3'
]);
$data = array(
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
\Mail::send('email.Test', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('visionquest.jesse#gmail.com');
$message->subject($data['subject']);
});
}
Web.php:
Route::get('contact', 'PagesController#getContact');
Route::post('contact', 'PagesController#postContact');
Test.blade.php:
<html>
<h1> Email from: {{ $email }}</h1>
<p> {{ $subject }} </p>
<br><br>
<hr>
<p>{{ $bodyMessage}}</p>
contact.blade.php:
#extends('main')
#section('title', '| Contact')
#section('content')
<div class="row">
<div class="col-md-12">
<h1>Contact Me</h1>
<hr>
<form acion="{{ url('contact') }}">
{{ csrf_field() }}
<div class="form-group">
<label name="email">Email:</label>
<input id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label name="subject">Subject:</label>
<input id="subject" name="subject" class="form-control">
</div>
<div class="form-group">
<label name="message">Message:</label>
<textarea id="message" name="message" class="form- control">Type your message here...</textarea>
</div>
<input type="submit" value="Send message" class="btn btn-success">
</form>
</div>
</div>
#endsection
All I want to do is pull the information from the form and send it via email to a set address. I have been working on this for over week and now that I am not getting errors I have no idea what to do.
You can add this above the form in your blade file. You would be able to see the validation errors if any :
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
Also change :
<form acion="{{ url('contact') }}">
To
<form action="{{ url('contact') }}" method="POST">
put some method on your form : method="POST" because once you will submit it, it will route to the GET route not the POST route since you did not declare a method on your form

Request validation with laravel 5

I have problem when trying validate an input in the form, catch an error of "unique". Although I am modifying and not inserting a new record...
This eror is The element is already in use codigotiporiesgo
Look at the code: FORM:
<div class="form-group" id='test'>
{!!Form::label('codigoTipoRiesgo', 'Código', array('class' => 'col-sm-2 control-label')) !!}
<div class="col-sm-10">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-barcode"></i>
</span>
<input type="hidden" id="token" value="{{csrf_token()}}"/>
{!!Form::text('codigoTipoRiesgo',null,['class'=>'form-control','placeholder'=>'Ingresa el código del tipo de riesgo'])!!}
{!!Form::hidden('idTipoRiesgo', null, array('id' => 'idTipoRiesgo')) !!}
</div>
</div>
</div>
and REQUEST:
"codigoTipoRiesgo" => "required|string|max:20|unique:tiporiesgo,codigoTipoRiesgo,".$this->get('idTipoRiesgo') .",idTipoRiesgo",
"nombreTipoRiesgo" => "required|string|max:80",
"origenTipoRiesgo" => "required|string",
"ClasificacionRiesgo_idClasificacionRiesgo" => "required|numeric"
I believe your issue may be with $this->get('idTipoRiesgo') and it is returning null. Try dd($this->get('idTipoRiesgo')) and see what you get.
Try querying for the model before you rules.
$model = Model::find($this->idTipoRiesgo);
then your rule would be pretty much what you posted, but now you should have the $model->id to work with.
'field_name' => 'unique:table_name,column_name,' . $model->id . ',primary_key_column'

Adding Route and Perameter in Form action in html in laravel 5

I am unable to get this, My Route is as under;
Route::patch('/info/{iplive}', ['as' => 'info', 'uses' => 'InfoController#index']);
and Here is My Form,
{!! Form::open(['url'=>'info/', 'id'=> 'lookup', 'class' => 'user_form','method'=>'PATCH']) !!}
<div class="row center-block">
<div class="col-lg-6 center-block">
<div class="input-group">
<input type="text" id="lookup" name="lookup" value="" class="form-control" placeholder="Search for Address">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Lookup</button>
</span>
</div>
</div>
</div>
<!--</form> -->
{!! Form::close() !!}
I am in efforts to use a url like, link but unable to get it done :(
your route expects a paramater to be filled in,
try something like this
{!! Form::open(['method' => 'PATCH', , 'id'=> 'lookup', 'class' => 'user_form', 'route' => [ 'info' , $your_iplive_variable]]) !!}
where $your_iplive_variable should be the paramater you wish to fill in.

laravel 5 not deleting user record

All i want to do is simply delete a user form the database.
My Route is a resource as seen below:
Route::resource('users', 'UserController');
So this should mean that the destroy action in my UserController should be the place for my code.
So my controller action is below:
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return Redirect::back();
}
Now when i click the delete button, which links to /users/destroy/4
it should find the user with id 4 and then delete it.
Instead i get the error
NotFoundHttpException in RouteCollection.php line 145:
EDIT:
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->username }}</td>
<td>{{$user->HWID}}</td>
<td>{{$user->name}}</td>
<td class="tools">
<i class="fa fa-pencil-square-o fa-lg"></i>
<i class="fa fa-trash fa-lg"></i>
</td>
</tr>
#endforeach
I dont know if it's possible to directly delete a user from your database via a link as you specified in your table.
My work around for this is to first point the user to the show function in your controller. And giving the user an overview of the information of the user itself.
This page contains a form with the DELETE method. Below the information of the user I put a delete button which will submit the form with the DELETE method to the URL: /users/4
Cause the link: /users/destroy/4 is not a valid resource link.
See this link for extra information about the resource controller links: Resource Controller
Example delete/show page of my own application:
{!! Form::model($ManagementUser, array('method' => 'DELETE', 'url' => 'admin/management/' . $ManagementUser->id, 'role' => 'form')) !!}
<div class="box-body">
<div class="form-group">
<label>Name</label>
{!! Form::text('name', Input::old('name'), array('class' => 'form-control', 'placeholder' => 'Name', 'name' => 'name', 'disabled')) !!}
</div>
<div class="form-group">
<label>E-mailaddress</label>
{!! Form::text('email', Input::old('email'), array('class' => 'form-control', 'placeholder' => 'E-Mail', 'name' => 'email', 'disabled')) !!}
</div>
{!! Form::submit('Delete', array('class' => 'btn btn-block btn-default')) !!}
</div>
{!! Form::close() !!}
In Resource Controller, destroy action is handled by DELETE method. Not GET method. Currently you are accessing a route with GET method that is not registered. The following command will help you to understand Resource Routes that you registered.
php artisan route:list
GET
<i class="fa fa-trash fa-lg"></i>
DELETE (You can delete the record by using form and DELETE method as follows)
<form action="{{ route('users.destroy', $user->id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE" />
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>" />
<button><i class="fa fa-trash fa-lg"></i></button>
</form>
Reference
Resource Controller
Method Spoofing

Laravel 5, Forms, TokenMismatchException in VerifyCsrfToken.php line 46

I keep getting the "TokenMismatchException in VerifyCsrfToken.php line 46" error when submitting forms.
This is one of the forms:
{!! Form::model($product, array('url' => 'product/'.$product->id, 'class' => 'form', 'method' => 'PATCH')) !!}
<div class="form-group">
{!! Form::textarea('note', $product->note,
array('class'=>'form-control', 'id'=>'product-note', 'placeholder'=>Lang::get('customtranslation.form_placeholder_note'), 'rows'=>3)) !!}
<br />
<span class="btn btn-link" id="remove-note" role="button"><i class="fa fa-times"></i> {{ Lang::get('customtranslation.button_txt_reset_note') }}</span>
</div>
<div class="form-group">
{!! Form::submit(Lang::get('customtranslation.button_txt_finish_edit_product'), array('class'=>'btn btn-success')) !!}
</div>
<div class="form-group">
<!-- Custom tags -->
{!! Form::label('additional-tags', Lang::get('customtranslation.form_edit_label_additional_tags')) !!}
{!! Form::text('additional-tags','', array('id'=>'additional-tags', 'data-role'=>'tagsinput')) !!}
</div>
{!! Form::close() !!}
The input element with name "_token" gets generated and set as expected.
The strange thing is that this occurs only in Internet Explorer (IE11). Chrome and FF make a submit without any problems.
Does anyone else have this problem and a possible solution?
Internet explorer rejects sessions from domains with an underscore it. This is a known issue.
Please see here: Issue with Session and Cookie in Internet Explorer for websites containing underscore
And also:
http://blogs.msdn.com/b/ieinternals/archive/2009/08/20/wininet-ie-cookie-internals-faq.aspx
Probably you have not set the _token parameter in your request to the server or you have put it in somewhere incorrect.

Resources