Laravel 5.2 Bootstrap Modal Form Hi,
I have created a modal with form included in it.
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" style="text-align: center;"></h4>
</div>
<div class="modal-body">
{{ Form::open(array('action' => 'Auth\AuthController@login ', 'method' => 'post')) }}
<div class="form-group">
<label for="login-email">Email address</label>
{{ Form::email('login-email', '', array('id' => 'login-email', 'class' => 'form-control input-sm', 'placeholder' => 'Email Address')) }}
</div>
<div class="form-group">
<label for="login-password">Password</label>
{{ Form::password('login-password', array('id' => 'login-password', 'class' => 'form-control input-sm', 'placeholder' => 'Password')) }}
</div>
{{ Form::submit('Sign in', array('class' => 'btn btn-info btn-block')) }}
<hr>
{{ Form::close() }}
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
I open the modal but I don't know exactly how should I continue. I am confused if I should use a route or not. Can someone suggest me a solution so that can I create a login functionality.
Should I create route for this and what type(post, get etc) of route ?
You need a POST route but what about validation ? With a modal form you should use Ajax to manage validation.
Based on what you have written I have created a POST route like this....
Route::group(['middleware' => ['web']], function () {
Route::post('/', 'Auth\AuthController@login ');
});
Ican see my modal but when I fill it with values and click submit nothing happend(no save).
Need to check Ajax functionality on Laravel
Set the good route for Auth\AuthController@login action, I dont think it's "/", but something like "auth/login", or just "login". Check your routes.
@papa if you are using the latest laravel release, web middleware is used by default. So you can just do something like
Route::post('/', 'Auth\AuthController@login ');
if you are not using the latest release then keep web routes inside web middleware
Route::group(['middleware' => ['web']], function () {
Route::post('/', 'Auth\AuthController@login ');
});
but this looks very wrong. you should not post login requests to / url, most applications often sent to /login
Route::post('/login', 'Auth\AuthController@login ');
You can also use the laravel Route::auth() in your routes file. Take a look at https://laravel.com/docs/5.2/authentication#authentication-quickstart
Actually I can not unserstand the difference between the routes
Route::post('/', 'Auth\AuthController@login ');
Route::post('/login', 'Auth\AuthController@login ');
because for login I have a modal that is accessible for any page by a header Link (LOGIN)
Route::post('/', 'Auth\AuthController@login '); = www.example.com/
Route::post('/login', 'Auth\AuthController@login '); = www.example.com/login
P.s
now to render a page with a login url, the route for this page would be a get like this
Route::get('/login', function(){
return view('someView');
});
edited: the get login route example
@mehany yeah but if I leave it as
Route::post('/', 'Auth\AuthController@login ');
makes the same work
There are maybe safety reasons should I make it
Route::post('/login', 'Auth\AuthController@login ');
if you are using the default auth provider, which I assume you are, that is the way it is defined. Go to your projects root and run php artisan route:list
ok @mehany I have to check now the AJAX call, because for my understanding when we work with modals would be better to POST the data by AJAX and validate to.
For Registration
I can successfully save the data when I pass the rules from my Form Request file I created in request folder.
When I don't pass the rules I get in my console.log
jquery-1.12.2.min.js:4 POST http://localhost/myApp/public/register 422 (Unprocessable Entity)
I am not sure but I don't use the
<meta name="csrf-token" content="{{ csrf_token() }}" />
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
and my data are saved.
In my Modal Form I have this which I am not sure if it is correct.
<input name="_token" type="hidden">
Trying using
<input name="_token" type="hidden" value="{{ csrf_token() }}">
has no result
I have found that my Routes should be included in
Route::group(['middleware' => ['web']], function () {
route so that I can have the CSRF token
To test if CSRF is the problem you can temporarily disable in the VierifyCsrfToken middleware .
protected $except = [
'/*'
];
I was stuck on this for 2 days before I figured out what was going on. There are no errors of any kind when CSRF verification fails.
Also, a lot of the information on how to add CSRF token doesn't seem to work anymore for some reason. It seems that the way Laravel does it keeps changing depending on what version you are running.
For 5.5 using ajax datatables I did the following.
ajax: {
"headers": {'X-CSRF-Token': '{{ csrf_token() }}'},
...
...
}
Please sign in or create an account to participate in this conversation.