I have this weird issue that I have never came across before ever since I started learning laravel.
I have a contact form: contact.blade.php
{!! Form::open(['url' => '/contact-form', 'id' => 'formContact']) !!}
<div class="col-md-12 col-sm-12 form-group">
{!! Form::label('full_name', 'Full Name:', ['class' => 'fw_400']) !!}
{!! Form::text('full_name', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="col-md-6 col-sm-6 form-group">
{!! Form::label('email', 'Email:', ['class' => 'fw_400']) !!}
{!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="col-md-6 col-sm-6 form-group">
{!! Form::label('contact', 'Contact Number:', ['class' => 'fw_400']) !!}
{!! Form::text('contact', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="col-md-12 col-sm-12 form-group">
{!! Form::label('contact_message', 'Contact Message:', ['class' => 'fw_400']) !!}
{!! Form::textarea('contact_message', null, ['class' => 'form-control input-sm', 'rows' => 5]) !!}
</div>
<div class="col-md-12 col-sm-12 form-group">
{!! Form::submit('Submit', ['class' => 'btn btn-primary btn-block btnContactOnContactPage']) !!}
</div>
{!! Form::close() !!}
When I click on the submit button, AJAX call is made.. Here's the ajax handler:
(function() {
$('#formContact').submit(function(e) {
$('.btnContactOnContactPage').prop('disabled', true);
var inputData = $('#formContact').serialize();
$.ajax({
url: $('#formContact').attr('action'),
type: 'POST',
data: inputData,
success: function(m) {
$('.btnContactOnContactPage').prop('disabled', false);
if (m.status === 'success') {
toastr.success(m.msg);
setTimeout(function() {
window.location.reload();
}, 3000)
}
},
error: function(e) {
if (e.status === 422) {
$('.btnContactOnContactPage').prop('disabled', false);
var errors = e.responseJSON;
var errorsHtml = '<div class="alert alert-danger"><ul>';
errorsHtml += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>';
$.each(errors, function(key, value) {
errorsHtml += '<li class="text-danger">' + value[0] + '</li>';
});
errorsHtml += '</ul></div>';
$('.errors').html(errorsHtml);
}
}
});
return false;
});
})();
And the controller method:
public function postContact(Request $request)
{
$this->validate($request, [
'full_name' => 'required',
'email' => 'required|email',
'contact' => 'required|regex:/^[0-9+\-]+$/',
'contact_message' => 'required'
]);
if ($request->ajax()) {
$prevContactCode = DB::table('contact_messages')->latest()->limit(1)->pluck('cont_code');
if ($prevContactCode == "" || empty($prevContactCode)) {
$request['cont_code'] = 'CON-000001';
}
else {
$request['cont_code'] = ++$prevContactCode;
}
$contactMessage = ContactMessage::create($request->all());
$dataToMail = [
'cont_code' => $contactMessage->cont_code,
'full_name' => $contactMessage->full_name,
'email' => $contactMessage->email,
'contact' => $contactMessage->contact,
'contact_message' => $contactMessage->contact_message
];
$mail = Mail::send('emails.contact', $dataToMail, function ($message) use($dataToMail)
{
$message->from($dataToMail['email'], $dataToMail['full_name']);
$message->to('contact#example.com', 'Example Contact')
->subject('Contact Form Details from example.com (Form No.:' . $dataToMail['cont_code'] . ')')
->cc(['ex#example.net', 'exam#hotmail.com'])
->bcc(['examp#gmail.com', 'esmap#example.net']);
});
var_dump($mail); // displays **int 5** in chrome console
dd($mail); // displays **5** in chrome console
if ($mail) {
return response(['status' => 'success', 'msg' => 'Contact form details has been successfully submitted.']);
}
}
return response(['status' => 'failed', 'msg' => 'Something went wrong. Please try again later.']);
}
The issue is that, the mail is sent thrice instead of just once. I don't know what is happening here. Earlier [maybe 5 months back], it was working correctly, and now, all of a sudden, it started doing this. I am trying to figure out what is the reason behind this, but to no success. I have also updated the framework using composer update but the issue still exists.
EDIT 1: I was playing trial and error with the Mail facade, and I guess the issue is in bcc method, because I tried no bcc and cc, it worked as expected. Then again I tried with just cc and no bcc, this time also it worked correctly as expected but when added bcc with cc, I received the same mail thrice.
Where I have made the mistake ? Kindly help me out.
Some mail drivers, usually Mandrill, sends CC'ed & BCC'ed emails as separate emails instead of treating them as a single email.
I haven't used mailtrap, but same thing might be happening in your case too.
Common cause of this issue could be that click propogation is not prevented. Which means that even if you interact with other control placed on top could possibly send the request.
Another cause is that click method binding is happening more that one time. It happens when you are binding the click event once server response comes. In such case this is a common issue.
It would be great if you could try add console.log on click events which initializes request. And see how many times it gets logged.
Still if you can't narrow it down would be great if you could extract subset of code and post it on JSfiddle or something so others can take a look.
Hope it will help.
Related
I'm having a problem for the first time when i submit a form.
When i submit the form it doesn't go to post route and i don't know why.
my post route is that:
Route::post('/app/add-new-db', function()
{
$rules = array(
'name' => 'required|min:4',
'file' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('app/add-new-db')
->withErrors($validator);
}
else
{
$name = Input::get('name');
$fname = pathinfo(Input::file('file')->getClientOriginalName(), PATHINFO_FILENAME);
$fext = Input::file('file')->getClientOriginalExtension();
echo $fname.$fext;
//Input::file('file')->move(base_path() . '/public/dbs/', $fname.$fext);
/*
DB::connection('mysql')->insert('insert into databases (name, logotipo) values (?, ?)',
[$name, $fname.$fext]);
*/
//return Redirect::to('app/settings/');
}
});
And my html:
<div class="mdl-cell mdl-cell--12-col content">
{!! Form::open(['url'=>'app/add-new-db', 'files'=>true]) !!}
<div class="mdl-grid no-verticall">
<div class="mdl-cell mdl-cell--12-col">
<h4>Adicionar Base de Dados</h4>
<div class="divider"></div>
</div>
<div class="mdl-cell mdl-cell--6-col">
<div class="form-group">
{!! Form::label('name', 'Nome: ') !!}
{!! Form::text('name', null, ['id'=> 'name', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Logotipo:') !!}
{!! Form::file('image', ['class'=>'form-control']) !!}
#if ($errors->has('image'))
<span class="error">{{ $errors->first('image') }}</span>
#endIf
</div>
<div class="form-group">
{!! Form::submit('Adicionar', ['id'=>'add-new-db', 'class'=>'btn btn-default']) !!}
<p class="text-success"><?php echo Session::get('success'); ?></p>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
I'm loading this from from a jquery get:
function addDB()
{
$( "#settings-new-db" ).click(function(e)
{
$.get('/app/add-new-db', function(response)
{
$('.content-settings').html("");
$('.content-settings').html(response);
componentHandler.upgradeDom();
});
e.preventDefault();
});
}
When i try to submit the form i'm getting 302 found in network console from chrome.
I'm doing forms at this way and it is happens for the first time. Anyone can help me?
Thanks
Fix the name attribute for your image upload field. You refer it as image in the form but you are trying to fetch it as file in your controller.
I'm still learning Laravel and trying to build some sort of an article management system. As part of form for inserting new articles I want to also upload multiple pictures of them. The upload shows success, yet there is no file in uploads folder. Here is my form code:
{!! Form::open(['route' => 'admin_artikli.store', 'class' => 'dropzone', 'id' => 'create_artikal', 'enctype' => 'multipart/form-data' ]) !!}
<div class="form-group">
{!! Form::label('Firma', 'Firma') !!}
{!! Form::text('firma_id', Input::old('firma_id'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('Naziv', 'Naziv') !!}
{!! Form::text('naziv', Input::old('naziv'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('Opis', 'Opis') !!}
{!! Form::text('opis', Input::old('opis'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('Cijena', 'Cijena') !!}
{!! Form::text('cijena', Input::old('cijena'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('kolicina', 'kolicina') !!}
{!! Form::text('kolicina', Input::old('kolicina'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('dostupnost', 'dostupnost') !!}
{!! Form::text('dostupnost', Input::old('dostupnost'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::select('aktivan', array('D' => 'Aktivan', 'N' => 'Neaktivan'), 'D') !!}
</div>
<div class="form-group">
{!! Form::select('aktivan', array('D' => 'Aktivan', 'N' => 'Neaktivan'), 'D') !!}
</div>
{!! Form::submit('Kreiraj artikal', array('class' => 'btn btn-primary', 'id' => 'create_artikal_submit')) !!}
{!! Form::close() !!}
My routes.php(showing only relevant):
Route::post('/upload', 'ArtikalAdminController#upload');
Route::resource('admin_artikli', 'ArtikalAdminController');
My controller methods for store and upload:
public function upload(){
$input = Input::all();
$rules = array(
'file' => 'image|max:3000'
);
echo "eldaaaaaaaaaaaaaaaaaaaaaaaaar";
$validation = Validator::make($input, $rules);
if ($validation->fails())
{
return Response::make($validation->errors->first(), 400);
}
$file = Input::file('file');
$extension = File::extension($file['name']);
$directory = public_path().'uploads/'.sha1(time());
$filename = sha1(time().time()).".{$extension}";
$file->move($directory, $filename);
$upload_success = Input::file('file', $directory, $filename)->move($directory, $filename);;
echo $directory;
if( $upload_success ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
public function store(Request $request)
{
$artikal = new Artikal;
$artikal->firma_id = Input::get('firma_id');
$artikal->naziv = Input::get('naziv');
$artikal->sifra = Input::get('sifra');
$artikal->opis = Input::get('opis');
$artikal->cijena = Input::get('cijena');
$artikal->kolicina = Input::get('kolicina');
$artikal->dostupnost = Input::get('dostupnost');
$artikal->aktivan = Input::get('aktivan');
$artikal->save();
Session::flash('flash_message', 'Uspješno unesen artikal!');
//return redirect()->back();
}
Finally, dropzone options I am currently using:
Dropzone.options.createArtikal = { // The camelized version of the ID of the form element
// The configuration we've talked about above
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 10,
maxFiles: 10,
// The setting up of the dropzone
init: function() {
var myDropzone = this;
// First change the button to actually tell Dropzone to process the queue.
var element = document.getElementById('create_artikal_submit');
var form = document.getElementById('create_artikal');
element.addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
// Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
// of the sending event because uploadMultiple is set to true.
this.on("sendingmultiple", function() {
// Gets triggered when the form is actually being sent.
// Hide the success button or the complete form.
});
this.on("successmultiple", function(files, response) {
// Gets triggered when the files have successfully been sent.
// Redirect user or notify of success.
form.submit();
});
this.on("errormultiple", function(files, response) {
// Gets triggered when there was an error sending the files.
// Maybe show form again, and notify user of error
});
}
}
I know this is lacking many things, but I'm trying to go on step at a time and I'm stuck here. When I check the network tab of dev tools I see XHR requests are 200 OK, but yet I see no file.
You forgot to write 'files' => true in form, update form & try.
{!! Form::open(['route' => 'admin_artikli.store', 'class' => 'dropzone', 'id' => 'create_artikal', 'files'=>true, 'enctype' => 'multipart/form-data' ]) !!}
EDIT
This issue has been solved, but I don't understand why the answer worked.
I would like to know why it didnt work.
Is there some one who could explain me?
Original question
I am stuck on my settings form, my problem is that at the settings form you can enter some email settings but you can also change your password.
The email settings and password reset works and my form fills it self with the data of the current user. But when the form validation fails it redirecteds me back to the form without the form data.
I am not sure if I made myself clear, but this code below will explain it.
ChangeUserSettingRequest.php
class ChangeUserSettingsRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
if (\Auth::check()) {
return true;
}
return false;
}
/**
* Get the validation rules that apply to the request.
*
* WHEN THIS VALIDATION FAILS IT GOES BACK TO SETTINGS.BLADE.PHP
* BUT IT DOES NOT KEEP THE SETTINGS DATA IN THE FORM
*/
public function rules()
{
return [
'current_password' => 'sometimes|required_with:password',
'password' => 'required_with:current_password|min:8|confirmed',
'password_confirmation' => 'required_with:password',
];
}
}
settings.blade.php
{!! Form::model($settings, array('url' => route('store.settings'), 'class' => 'col s12')) !!}
<p>#lang('forms.info.settings')</p>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('info_mail', 0, false, ['id' => 'info_mail']) !!}
{!! Form::label('info_mail', Lang::get('forms.newsletter'), ['for' => 'info_mail']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('message_notification', 0, false, ['id' => 'message_notification']) !!}
{!! Form::label('message_notification', Lang::get('forms.messages'), ['for' => 'message_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('friend_notification', 0, false, ['id' => 'friend_notification']) !!}
{!! Form::label('friend_notification', Lang::get('forms.friendrequest'), ['for' => 'friend_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('item_notification', 0, false, ['id' => 'item_notification']) !!}
{!! Form::label('item_notification', Lang::get('forms.reactiononitem'), ['for' => 'item_notification']) !!}
</div>
#if ($settings and $settings->google_maps !== null)
<div class="settings-explain">
<p class="margin-top-20">#lang('forms.info.companysettings')</p>
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('type', 0, false, ['id' => 'type']) !!}
{!! Form::label('type', Lang::get('forms.companytype'), ['for' => 'type']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('google_maps', 0, false, ['id' => 'google_maps']) !!}
{!! Form::label('google_maps', Lang::get('forms.companymap'), ['for' => 'google_maps']) !!}
</div>
#endif
<div class="settings-explain">
<p class="margin-top-20">#lang('forms.info.changepassword')</p>
</div>
<div class="input-field col s12">
{!! Form::label('current_password', $errors->has('current_password') ? $errors->first('current_password') : Lang::get('forms.currentpassword'), ['for' => 'current_password']) !!}
{!! Form::password('current_password', ['class' => $errors->has('current_password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password', $errors->has('password') ? $errors->first('password') : Lang::get('forms.newpassword'), ['for' => 'password']) !!}
{!! Form::password('password', ['class' => $errors->has('password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password_confirmation', $errors->has('password_confirmation') ? $errors->first('password_confirmation') : Lang::get('forms.repeatpassword'), ['for' => 'password_confirmation']) !!}
{!! Form::password('password_confirmation', ['class' => $errors->has('password_confirmation') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::button(Lang::get('forms.save'), ['class' => 'btn waves-effect waves-light', 'type' => 'submit', 'name' => 'Save']) !!}
</div>
{!! Form::close() !!}
UserInfoController.php
/**
* Function shows the settings form
*/
public function showSettings()
{
$title = Lang::get('titles.settings');
$user_id = Auth::User()->id;
$settings = $this->settings->getUserSettings($user_id);
$companySettings = $this->companySettings->getSettings($user_id);
if ($companySettings) {
$settings->type = $companySettings->type;
$settings->google_maps = $companySettings->google_maps;
}
return view('pages.users.settings', ['title' => $title])->with(compact('settings'));
}
/**
* Function stores the setting changes
*
* ChangeUserSettingsRequest makes sure that the request is valid
*/
public function storeSettings(ChangeUserSettingsRequest $request)
{
$id = Auth::User()->id;
$success = $this->settings->handleStoreSettingsRequest($request);
// Checks if user has company settings
$hasCompanySettings = $this->companySettings->checkForSettings($id);
// If user has company settings
if ($hasCompanySettings === true) {
// Update company settings
$this->companySettings->updateSettings($request);
}
if ($success === true) {
/* Get translated message */
$message = Lang::get('toast.settingsstored');
return Redirect::route('user.profile', array(Auth::User()->permalink))->withMessage($message);
}
$settings = $this->settings->getUserSettings($id);
/* Get translated message */
$message = Lang::get('forms.error.wrongpassword');
/* This works and the form is filled with the correct data after it redirects me back */
return Redirect::back()->withErrors(array('current_password' => $message))->withSettings($settings);
}
Request.php
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
So my problem is that in my UserInfoController I redirect back and it has the good data but when my ChangeUserSettingsRequest redirects me back the form is empty.
Does anyone know why ChangeUserSettingsRequest does not send the data back?
I am not talking about the password data but about the email settings.
I do not want to make a costum validator as I think this should be posible.
Please note: when the validation fails, the function storeSettings will not be executed at all
On failure return them back to the page with their input data (withInput())
return Redirect::back()->withErrors(['current_password' => $message])->withInput($request->except('password'));
For more information see the Laravel docs pertaining to requests... http://laravel.com/docs/5.1/requests
I found it!
The formRequest should have handled it correctly but no idea why it did not.
I found the solution when looking trough the file formRequest.php
https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Http/FormRequest.php
I had to override the response function and it works now!
To only thing that I had to add to my ChangeUserSettingsRequest was this
public function response(array $errors)
{
return $this->redirector->to($this->getRedirectUrl())->withErrors($errors, $this->errorBag);
}
The only difference is that I don't send the input fields back, little strange that this works but I am glad that it is solved now.
Hi, I'm trying to make an image upload via laravel, everything is working but now I want to change the upload to an jquery upload instead but then I get an 500 internal server error
so when I handle things with Jquery it fails. Anyone knows what the problem might be?
html:
{{ Form::open(array('url' => '../public/posts/add', 'class'=>'form-horizontal', 'role' => 'form', 'id' => 'addPin', 'files' => true)) }}
<div id="validation-errors" class="alert alert-danger" hidden>
<p>Some errors occured</p>
<ul></ul>
</div>
<!-- Image Type -->
<span id="type-image" class="type-media">
<div class="form-group">
<label class="col-sm-3 control-label">Title</label>
<div class="col-sm-9">
{{ Form::text('Image-title', null, array('class' => 'form-control', 'placeholder' => '')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Choose file</label>
<div class="col-sm-9">
{{ Form::file('Image-file') }}
<p class="help-block">Only .jpg, .png, .gif, .bmp allowed.</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Description</label>
<div class="col-sm-9">
{{ Form::textarea('Image-description', null, array('class' => 'form-control', 'rows' => '3')) }}
</div>
</div>
</span>
<div class="modal-footer">
{{ Form::submit('Close', array('class' => 'btn btn-default', 'data-dismiss' => 'modal')) }}
{{ Form::submit('Pin it, babe!', array('class' => 'btn btn-info')) }}
</div>
{{ Form::close() }}
Jquery
addPin.on('submit', function() {
event.preventDefault();
var errorForm = addPin.find('div#validation-errors');
$.ajax({
url: '../public/posts/add',
type: 'post',
cache: false,
data: addPin.serialize(),
beforeSend: function() {
errorForm.hide();
errorForm.find("ul").empty();
},
success: function(data) {
if(data.success == false) {
var arr = data.errors;
console.log(arr);
$.each(arr, function(index, value){
if (value.length != 0){
errorForm.find("ul").append('<li>'+ value +'</li>');
}
});
errorForm.show();
} else {
location.reload();
}
},
error: function() {
alert('Something went to wrong.Please Try again later...');
}
});
return false;
} );
PHP
public function postAdd(){
if (Auth::check()){
$rules = array(
'Image-title' => 'Required|Min:3|Max:255|alpha_spaces',
'Image-description' => 'Required|Min:3',
'Image-file' => 'image',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return \Response::json(['success' => false, 'errors' => $validator->getMessageBag()->toArray()]);
} else {
$post = Post::create(array(
'user_id' => Auth::user()->id,
'title' => Input::get('Image-title'),
'description' => Input::get('Image-description'),
'type' => 'Image',
));
$file = Input::file('Image-file');
$destinationPath = 'img/';
$extension = $file->getClientOriginalExtension();
$filename = 'usr_'. Auth::user()->id . '_post'.$post->id .'.'. $extension;
$file->move($destinationPath, $filename);
$post->imgLocation = $filename;
$post->save();
DB::table('board_post')->insert(['board_id' => 2, 'post_id' => $post->id]);
return \Response::json(['success' => true]);//*/
}
}
}
You should have an error in your app/storage/logs/laravel.log file stating the exact error. Setting PHP to show all errors is preferable in a development environment tho, so you might want to consider turning those on, so you'll get a more descriptive message than just "500 Internal Server Error".
Check you upload_max_filesize in php.ini
Echo ini_get ("upload_max_filesize");
See this
The problem was due the fact that Jquery/JS don't really support Image upload
I fixed the problem by when you click on the button, the form get's validated (via ajax) and show the errors when there are, otherwise it does the normal form sending (the one if I wouldn't do the event.prefentdefault())
this works for now, prehaps I'll have a second look at it to make it completly AJax, but probably not :)
I am trying to make a loginbox with jquery using the laravel framework. I have rules in Laravel which i want it to follow. here the code for that:
$rules = array(
'username' => 'required|alphaNum|min:6|max:16',
'password' => 'required|alphaNum|min:6'
);
They work fine, and when you fail with your login the following code is run:
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('index')
->withErrors($validator)
->withInput(Input::except('password'));
The problem is that the log in-box pops up when called by pressing a button, which becomes a problem when I redirected to another page, because the error messages are written inside the log in box. This is good, however i cant get the log in box to pop up automatically once redirected, so you have to click the log in button again to see the error message and try again.
Here is the jQuery i use:
$(function(){
$('#loginform').submit(function(e){
return false;
});
$('#modaltrigger').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
});
And here is the HTML log in form:
<div id="loginmodal" style="display:none;">
<h1>Logga in</h1>
{{ Form::open(array('url' => 'login')) }}
<p>
{{ $errors->first('username') }}
{{ $errors->first('password') }}
</p>
<div class="row">
<div class="col-md-12">
{{ Form::label('username', 'Username', array('class' => 'label')) }}
{{ Form::text('username', Input::old('username'), array('placeholder' => 'BTH Akronym',
'class' => 'textfield',
'id' => 'login-margin',
))
}}
</div>
<div class="col-md-12">
{{ Form::label('password', 'Password', array('class' => 'label')) }}
{{ Form::password('password', array('placeholder' => 'Lösenord',
'class' => 'textfield'
)) }}
</div>
</div>
<div class="center">
{{ Form::submit('Logga in', array('class' => 'login-knapp')) }}
</div>
{{ Form::close() }}
</div>
This is untested, but something like this should work for you...
PHP
if ($validator->fails()) {
if(Request::ajax()) {
return Response::json($validator->getMessages());
} else {
return Redirect::to('index')->withErrors($validator)->withInput(Input::except('password'));
}
}
The idea here is if the request is an ajax request, it will just return some json which we will be able to use on the success function of an ajax request on your template.
jQuery
$("#form").submit(function(e) {
$.ajax({
type: "POST",
url: "{{ route('ajax.form_submit') }}",
data: $("#form").serialize(), // serializes the form's elements.
success: function(data) {
if(data.length > 0) {
console.log(data); // Can set your messages on your modal.
e.preventDefault(); // Stop the form from submitting.
}
}
});
});
If there are any error messages, it will log them to the console and give you a good idea of what else needs to be done. If there are no error messages, it should just continue on with the form submission.
On $validator->fails(), I'd probably put an else on there for a success which would actually log the user in. This way because the function will run twice if it was a successful attempt (once by ajax, once by the form actually submitting), it wouldn't have to run the validator or check the database again (I'm assuming you already have a filter in place on these so it won't run if the user is logged in).