laravel Sending values to blade - php

I'm having a register form and login form on my home page
the login form is activated when you press the login link (via bootstrap)
and the register form is visible all the time
now when one off those fails, I wanna send the error's to the page
but I can only access the var's of these:
return Redirect::to('/')->withErrors($validator)
->withInput(Input::except('password'));
but when I add ->with('method', 'login') I can't check in my home page the var $method
it's empty and never get's any values
can someone tell me what I might be doing wrong?
and also how I can activate the login form with variables that I send with the ->with()
the login form is made up like this:
<div class="modal fade bs-example-modal-sm pvvoModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Login to your account</h4>
</div>
{{ Form::open(array('url' => 'admin/login', 'class'=>'form', 'role' => 'form')) }}
<div class="modal-body">
#if ( $errors->first('username') != null || $errors->first('password') != null)
<div class="alert alert-danger">
<p>Some errors occured</p>
{{ $errors->first('username') }}
{{ $errors->first('password') }}
</div>
#endif
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-user"></span></span>
{{ Form::text('username', $value = null, array('placeholder' => 'Username', 'class'=> 'form-control', 'required' => 'required', 'autofocus' => 'autofocus' )) }}
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-asterisk"></span></span>
{{ Form::password('password', array('placeholder' => 'Password', 'class' => 'form-control', 'id'=>'password', 'required' => 'required')) }}
</div>
</div>
<div class="text-center">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{{ Form::submit('Login', array('class' => ' btn btn-primary')) }}
</div>
</div>
{{ Form::close() }}
</div><!-- /.modal-content -->
</div>
</div>
</div>

The with() method flashes data to the session, you have to retrieve your data using Session::get()method.
Source : Laravel doc

Fixed it by doing it via AJAX :)
was easier for keeping the login form open

Related

Submitting modal form while other form is open in Laravel 5.6

What I have:
I've got a web application where I want to be able to add qualifications to a user.
When I open the Edit Employee form there is an Add button and a Delete button next to a select box displaying all qualifications associated with that user. When clicking on the Add button, a Bootstrap modal opens, with another form in it, where you can choose the Qualification as well as a Date Of Issue.
What I want:
For now I want the qualification to be added to the user and then let the controller redirect to the edit view, with a success message. Later I would like the qualification to be added via Ajax, close the window and display a success message as well. Maybe only refresh the Qualifications form element in the main form as well, if possible.
Problem:
When I submit the form in the modal, the main form get's submitted, not the one from the modal, even though I call different controller methods on submit.
My Forms:
Modal:
<link href="{{ asset('css/modals.css') }}" media="all" rel="stylesheet" type="text/css" />
<div class="modal fade" id="qualificationModal" tabindex="-1" role="dialog" aria-labelledby="qualificationModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<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" id="qualificationModalLabel" style='font-weight: bold;'>Add Qualification</h4>
</div>
<div class="modal-body">
{!! Form::open(['route' => ['qualification.add', $employee->id], 'method' => 'PUT']) !!}
<div class="form-group">
{{ Form::label('qualification', 'Qualification') }}<br>
{{ Form::select('qualification', $qualification_names, null, array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('issue_date', 'Date Of Issue') }}<br>
{{ Form::date('issue_date', \Carbon\Carbon::now(), array('class' => 'form-control', 'required' => 'required')) }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
{!! Form::submit('Add', array('class' => 'btn btn-primary')) !!}
</div>
</div>
</div>
</div>
Main:
#section('content')
<div class='col-lg-4 col-lg-offset-4'>
<h1><i class='fa fa-user-plus'></i> Edit {{$employee->name}}</h1>
<hr>
{{ Form::model($employee, ['route' => ['employees.update', $employee->id], 'method' => 'PUT']) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', null, array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email', null, array('class' => 'form-control', 'required' => 'required')) }}
</div>
<h5><b>Give Role</b></h5>
<div class='form-group'>
#foreach ($roles as $role)
{{ Form::checkbox('roles[]', $role->id, $employee->roles ) }}
{{ Form::label($role->name, ucfirst($role->name)) }}<br>
#endforeach
</div>
<div class="form-group">
{{ Form::label('password', 'Password') }}<br>
{{ Form::password('password', array('class' => 'form-control', 'placeholder' => ' • • • • • • • • • •', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Confirm Password') }}<br>
{{ Form::password('password_confirmation', array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('qualifications', 'Qualifications') }}<br>
{{ Form::select('qualifications', $employee->qualifications, null, ['size' => 5, 'class' => 'form-control']) }}
<button
type="button"
class="btn btn-default pull-right"
data-toggle="modal"
data-target="#qualificationModal"
data-qualifications="{{ $qualifications }}"
data-qualification_names="{{ $qualification_names }}">
Add
</button>
<button type="button" class="btn btn-danger pull-right">Remove</button>
#include('dispo.employees.add_qualification')
<br>
<br>
</div>
{{ Form::submit('Save', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
#stop
Routes:
Route::put('', [
'as' => 'qualification.add',
'uses' => 'DispoEmployeeController#addQualification'
]);
Route::resource('employees', 'Dispo\DispoEmployeeController');
Controller
public function update(Request $request, $id){
$employee = User::findOrFail($id);
//Validate name, email and password fields
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id,
'password'=>'required|min:6|confirmed',
]);
$input = $request->only(['name', 'email', 'password']);
$roles = $request->get('roles'); //Retreive all roles
$employee->fill($input)->save();
if (isset($roles)) {
$employee->syncRoles($roles);
}
else {
$employee->roles()->detach();
}
return redirect()->route('employees.index')->with('success','User successfully edited.');
}
public function addQualification(Request $request, $id){
$employee = User::findOrFail($id);
$this->validate($request, [
]);
$qualification = $request->input('qualification');
$employee->qualifications()->attach($qualification);
return redirect()->route('employees.edit')->with('success','Qualification successfully edited.');
}
I've made an assumption in this answer, so if this is wrong let me know and I can adjust.
Assumption: your modal is being added to the page via the blade partial #include('dispo.employees.add_qualification').
If this is how the modal is being included, then you have your modal form nested inside your main form (which would explain why the main form is being submitted).
Solution: move your modal outside the main form, by shifting #include('dispo.employees.add_qualification') to be below {{ Form::close() }} in the main blade file.

Bootstrap Modal Not Showing - Laravel - AdminLTE

i'm using an AdminLTE package with Laravel to give me the AdminLTE theme for my app.
I cannot get the modal to launch at all. If I copy the contents of my form.blade.php into my index.blade.php it works. Although the way i have coded my controller & views i need them to be independent files.
Index.blade.php <- view serving the datatable
form.blade.php <-- view serving the modal from click edit on index.blade.php
CostCenterController.php <-- Controller responsible for both views.
CostCenterController.php
So in my controller I have a function that returns the results with server side processing into my datatable, this is just the part showing how each item in the table is generated, specifically with the btn-group for the edit function.:
$data = array();
if (!empty($costcenters)){
foreach ($costcenters as $costcenter){
$editURL = "delete/".$costcenter->id;
$temp['name'] = $costcenter->name;
$temp['code'] = $costcenter->code;
$action = '';
$action .= '
<div class="btn-group">
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</div>
';
$temp['action'] = $action;
array_push($data, $temp);
}//end for each
}// end if empty
$recordsTotal = count($recordsTotal);
echo json_encode(array('recordsTotal' => $recordsTotal, 'data' => $data, 'recordsFiltered' => $recordsTotal));
In my index.blade.php i have nothing of importance or relevance really.
form.blade.php
I created a form.blade.php so when I hit edit, a route calls this view with the data needed from the edit function in the associated controller. This view works as plain html if opened in a new tab.
<div class="modal fade" id="modal" role="dialog">
<div class="modal-dialog" >
#if ($mode == 'edit')
{!! Form::model($costcenter,array('route'=>array('costcenter.update',$costcenter->id),'id'=>'costcenter-form')) !!}
#else
{!! Form::open(array('url'=>'costcenter/store','method'=>'post','data-toggle'=>'validator','id'=>'costcenter-form')) !!}
#endif
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">
#if ($mode == 'edit')
Edit {{$costcenter->name}}'s Cost Center
#else
Create Cost Center
#endif
</h5>
<button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true"> ×</span></button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
{!! Form::label('name','Cost Center Name:',['class'=>'control-label']) !!}
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Enter Cost Center Name','data-error'=>"Please enter the name of the cost center",'required'=>true]) !!}
</div>
<div class="form-group">
{!! Form::label('code','CostCenter Code:',['class'=>'control-label']) !!}
{!! Form::text('code',null,['class'=>'form-control','placeholder'=>'Cost Center code as it appears in SYSPRO.','data-error'=>"Invalid Cost Center Code.",'required'=>true]) !!}
</div>
</form>
</div>
<div class="modal-footer">
{!! Form::submit('Save',['class'=>'btn btn-primary'])!!}
</div>
</div>
</div>
</div>
Any help would be appreciated.

Laravel 5 Update existing in database

I'm trying to update an existing row in my table using a form in a bootstrap modal. But nothing changes when I attempt it. I have an html table with a list of cars and in each row a button "Change Status" that opens the bootstrap modal with the id="myModal_{{ $car->id }}" and with the form inside it to edit the status of a car in repair.
Here's my modal:
<!-- Modal -->
<div class="modal fade" id="myModal_{{ $car->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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" id="myModalLabel">Breyta stöðu bíls</h4>
</div>
<div class="modal-body">
<div class="form-group">
<h3>Breyta stöðu fyrir {{ $car->LicencePlate }}</h3>
</div>
{!! Form::open(['url' => 'create']) !!}
<div class="form-group">
{!! Form::label('Status', 'Status: ') !!}
{!! Form::select('Status', [
null => ' -- Veldu Stöðu bíls -- ',
'Í Biðröð' => 'Í Biðröð',
'Í Viðgerð' => 'Í Viðgerð',
'Tilbúinn' => 'Tilbúinn'
], null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{!! Form::submit('Skrá stöðu', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
Update method in CarController:
public function CarEdit($id, Requests\CreateCarsRequest $request)
{
$edit = Car::update($request->all());
return redirect::back();
}
My Route to that method:
Route::post('/dashboard', 'CarController#CarEdit');
You should do this:
['action' => 'CarController#CarEdit']
Also, you're using mass assignment, so you must fill $fillable array in your Car model to make it work:
https://laravel.com/docs/5.1/eloquent#mass-assignment
I was able to fix this with the help of my colleagues. What I did was I created a new requet called UpdateCarRequest where the LicencePlate field was not unique. And edited my method like so:
public function CarEdit(Requests\UpdateCarRequest $request)
{
$edit = Car::where('LicencePlate', '=', $request->LicencePlate)->first();
$edit->update($request->all());
return redirect('/dashboard');
}
I also added a hidden field in the form with the value $car->LicencePlate.

Laravel, show row value on <p> tag

I know this should be something easy but I don't figured out how to do it:
I have multiple "delet" buttons implemented on a datatable, and want to show a modal with the dialog: Do you reall want to delete: , but I don't know how to put the sentence.
This is my View:
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<div class="modal-body">
<div id="message">
<h3> BORRAR TIPO </h3>
{{ Form::model($catalog["nambe"], array('method' => 'DELETE', 'id' => 'type', 'route' => array($catalog["name"].'.destroy', $catalog["id"]))) }}
<br>
<p>Do you reall want to delete: {{ //I whant to display the exactly name of that button }} </p>
<br>
{{ Form::submit('Save', array('class' => 'btn btn-danger')) }}
<button type="button" class="btn btn-info" data-dismiss="modal">Cancel</button>
{{ Form::close() }}
</div>
//mode code below
In my DB I have:
name of table |type|
|id |
|name|
And I Want to show the name of the exact type than I press, but don't know how to do it, can someone help me please? Thanks!
This is actually a javascript issue. I've fixed this many times with this:
<div class="modal" id="your-modal">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<div class="modal-body">
<div id="message">
<h3> BORRAR TIPO </h3>
{{ Form::model($catalog["nambe"], array('method' => 'DELETE', 'id' => 'type', 'route' => array($catalog["name"].'.destroy', $catalog["id"]))) }}
<br>
<p>Do you reall want to delete: <span id="deleteSentence"></span></p>
<br>
{{ Form::submit('Save', array('class' => 'btn btn-danger')) }}
<button type="button" class="btn btn-info" data-dismiss="modal">Cancel</button>
{{ Form::close() }}
</div>
And with this script you can change the value of the span:
<script>
$('#your-modal').on('show.bs.modal', function (e) {
$('#deleteSentence').html($(e.relatedTarget).data('sentence'));
});
</script>
And then add the invoker button:
<a class="btn" data-toggle="modal" data-sentence="{{$name}}" data-target="#your-modal" href="http://some-url" >Launch Modal</a>
If you wan't to send the name with you're form then put a hidden input field in the modal.

Prevent other forms from submitting

I have a form which has another form inside it.
Everytime I submit the inner form, the outer form also submits. I would just like to submit my inner form via ajax.
Here's my twig template:
<div id="askDiv">
{{ form_start(form, { 'attr' : { 'novalidate' : 'novalidate', 'class' : 'col-md-offset-3 form-control-static col-md-7' } }) }}
<div class="col-lg-12" style="margin-bottom: 30px;">
<span id="titleLabel" data-toggle="popover" data-container="body" data-placement="left" data-trigger="manual" data-html="true" data-content='{{form_errors(form.title)}}' class="col-lg-1 text-left askLabels">{{ form_label(form.title) }}</span>
{{form_widget(form.title, { 'attr' : { 'class' : 'right form-control col-lg-8' } })}}
</div>
{{ form_widget(form.content, { 'attr' : { 'data-toggle' : 'popover', 'data-container' : 'body', 'data-placement' : 'left', 'data-trigger' : 'manual', 'data-html' : 'true', 'data-content' : form_errors(form.content), 'class' : 'col-lg-12' } }) }}
<div class="col-lg-12" style="margin-top: 20px;">
<input id="fieldTags" type="hidden" value="{{ tags|join(',') }}">
<label id="tagLabel" data-toggle="popover" data-container="body" data-placement="left" data-trigger="manual" data-html="true" data-content='{{form_errors(form.tags)}}' class="col-lg-1 text-left askLabels" for="tagField">Tags</label>
<div class="col-lg-8">
{{ form_widget(form.tags) }}
</div>
{% if app.user.reputati on >= 100 %}
<a id="addTag" title="Add New Tag" data-toggle="tooltip modal" data-placement="left" class="col-lg-3" href="#"><i class="fa fa-plus-circle"></i></a>
{% endif %}
</div>
<div style="margin-top: 20px; ">
{{ form_widget(form.submit, { 'attr' : { 'class' : 'col-md-offset-5 col-md-3 btn btn-primary' } }) }}
</div>
<div hidden id="errors">
{{ form_errors(form.title) }}
{{ form_errors(form.content) }}
{{ form_errors(form.tags) }}
{{ form_errors(form) }}
{{form_rest(form)}}
</div>
<div id="mymodal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Add New Tag</h4>
</div>
<form class="tagForm" id="tag-form" action="{{ path('tag_create') }}" method="post">
<div class="modal-body">
<label for="tagName">Tag Name: </label>
<input id="tagName" class="form-control" type="text"/>
</div>
<div id="responseDiv" ></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input id="tag-form-submit" type="submit" class="btn btn-primary" value="Add Tag">
</div>
</form>
</div>
</div>
My Script:
$(document).ready(function() {
$('#tag-form').submit(function(e) {
e.preventDefault();
var options = {
url: $('#tag-form').attr('action'),
type: 'post'
};
$('#tag-form').ajaxSubmit(options);
$('form[name="verysoft_askmebundle_question"]').submit(function(ev) {
ev.preventDefault();
});
});
});
Seems like my forms aren't nested after all. Sorry about that. My current problem is that everytime I hit the submit button of one of the forms, the other one submits also.
Nested forms is invalid HTML markup since it's not supported and not part of the w3c standard according to HTML5 draft:
Content model: Flow content, but with no form element descendants.
So you'll need separate your forms and apply the same logic but different jQuery code.

Categories