How to get different column values for one row - forms

I have a multi-step form in laravel where I pass the session variables from one view to the other and everything works ok.
This is part of my form
<div class="panel-body">
{!! Form::open(array('url' => '/post-quote')) !!}
<div class="form-group">
{!! Form::Label('brand', 'Brand:') !!}
<select class="form-control" name="brand" id="brand">
<option value="{{Input::old('brand')}}" selected="selected">{{Input::old('brand', 'Please select a Brand')}}</option>
#foreach($brands as $brand)
<option value="{{$brand->id}}">{{$brand->brand}}</option>
#endforeach
</select>
</div>
Here is part of my controller
public function quote()
{
$brands = Brand::orderBy('brand')->get();
return View::make('frontend.step1', compact('brands'));
}
The issue I got is that I am passing the id for each option value in the select and after validation, the Input::old populates the id value and it doesn't look right to display a number instead of the brand name.
Question: is there anyway to get input::old with the {{$brand->brand}} instead of the id?
My other option is to pass as a value for each option the brand name, however I dont know how to get the id for my input in the controller.
I hope you get what I am trying to achieve.
Thanks

Did you try passing the brands as an array? Like,
$brands = Brand::orderBy('brand')->lists('brand','id');
return View::make('frontend.step1', compact('brands'));
Then in your view you can use the Form::select instead of creating the select manually. Something like this:
{!! Form::select('brand',$brands, Input::old('brand'),['class' => 'form-control']) !!}
Give it a try and let me know if it works, there are some others things you can try, like using Form::model instead of Form::open (I would go with this approach)

you can pass data to next user request by using flash method like this
$request->flash()
then retrieve it in blade template with global old helper function which gives you old input data,
{{ old('brand') }}

$brands = Brand::orderBy('brand')->lists('brand','id');
return View::make('frontend.step1', compact('brands'));
Then in your view you can use the Form::select instead of creating the select manually. Something like this:
{!! Form::select('brand',$brands, Input::old('brand'),['class' => 'form-control']) !!}

Related

Submit array from Fluid-Template to Controller with f:form viewhelper

I am currently writing an Extbase Extension with a backend module. I have assigned an array to the template of my backend module. Now I am trying to submit that array back to my crontroller action "pageGenerator" with inputs of the form in the template.
Here is the code from my controller and template:
Controller:
public function listAction()
{
$array = [
'1' => '',
'2' => '',
'3' => ''
];
$this->view->assign('array', $array);
$this->view->setTemplatePathAndFilename('EXT:bm_test/Resources/Private/Templates/template.html');
}
public function pageGeneratorAction(array $array=null){}
Fluid-Template:
<div align="center">
<f:form method="post" controller="DomainModel" action="pageGenerator"
name="array" object="{array}" >
<input type="text" name="array[1]">
<input type="text" name="array[2]">
<input type="text" name="array[3]">
<f:form.submit value="Submit" />
</f:form>
</div>
The problem is that the array is null no matter what I type into the textfields.When i remove the "=null" from the pageGenerator action i get the following Error:
Too few arguments to function Bmt\BmTest\Controller\DomainModelController::pageGeneratorAction(), 0 passed and exactly 1 expected
So it seems like that the array isn't submitted.
Does anyone know what am I doing wrong here? Thanks in advance for your help.
have you tried to insert the assign variable after setTemplatePathAndFilename ?
Becouse in typo3 if the template haven't the same name of the Action you need first inizialize them and after return the view with assigned variable
example
$this->view->setTemplatePathAndFilename('EXT:bm_test/Resources/Private/Templates/template.html');
$this->view->assign('array', $array);
return $this->view->render();

Get checkbox checked after laravel validation error

I need to keep the checked checkboxes in the form as checked after I get the validation errors in the Controller.
Actually I find quite horrendous my implementation of the checkboxes in my form.
Could you please provide some advise? Thanks
<div class="form-group">
{!! Form::label('call', 'Llamar:', ['class' => 'control-label']) !!}
{!! Form::checkbox('call', isset($category)?(bool)$category->call:true, ['class' => 'form-control']) !!}
</div>
For the checkbox part, I did not use Laravel Forms. I used normal html "input" tag instead, and used "old" to check for previous value
<input type="checkbox" name="call" value="1" {{(old('call') == "1") ? 'checked': ''}}>Call
Try using old to check the previous value and set the value to checked or not based on that.
You can do it via the form builder much easier like this:
{!! Form::checkbox('call', 1, false, ['class' => 'form-control']) !!}
call = your checkbox name
1 = your checkbox value if it's checked (change to whatever you want)
false = don't load it checked on first page load (setting true will load it always checked)
['class' => 'form-control'] = it will add the class to it
That's all.
You don't need to worry about any kind of values, it will be set automatically.

Laravel dynamic select box return index instead of value from collection

I am trying to make a select box that return value from $collection, but it gives me index of the selection instead of value.
screenshot:output of my code
screenshot:browser source code view
<div class="form-group">
{!! Form::label('Supervisor') !!}<br />
{!! Form::select('supervisor_id', $lecturers, null,
array('placeholder' => '----------------','class' => 'form-control')) !!}
</div>
and when it's show in browser source code view:
<option value="">----------------</option>
<option value="0">1111111111</option>
<option value="1">1212121212</option>
Anyone who know the syntax of laravel form to make this dynamic form gives value of $lecturers please help.
Ok here we go.
First in your controller, when you are getting the lecturers list, make sure you pluck just the name and the id...in this order:
$lecturers = Lecturer::pluck('name', 'id');
Then in your view:
{{Form::select('lecturer_id', $lecturers, null, ['class' => 'form-control'])}}
Thats all.
The array of lecturers, that you plucked will be divided into "id" and "values", hence the order of plucking is important.
Now, in your form source, you will see that the option value will be lecturers ids like "1, 2, 3" etc while the "selectable" options shown to users will be the names of the lecturers.
Like this:
<select class="form-control" name="lecturers_id"><option value="1">John Doe</option><option value="2">Jane Doe</option></select>
Try these and lets know your result.

laravel collective form - how to send id (foreign key) with form

As in title how to do that? I have table comments, the structure of this table is:
-id
-title
-contents
-user_id
Here is code of my form:
{!! Form::open(['action' => ['LibraryController#newcomment', $sth->myuserid]]) !!}
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null) !!}
{!! Form::label('contents', 'Contents:') !!}
{!! Form::textarea('contents', null) !!}
{!! Form::submit('Send') !!}
{!! Form::close() !!}
So, comment has the user_id and I have to send it too with form, maybee by this:$sth->myuserid but how can I use it in LibraryController?
Additionally, I found this: http://laravel-recipes.com/recipes/157/creating-a-hidden-input-field is it a good option?
You could use a hidden field to store the user id and then retrieve it in your controller:
{{ Form::hidden('user_id', $user_id) }}
But if you are using Auth component you can get the authenticated user by using the Auth facade:
use Illuminate\Support\Facades\Auth;
...
// Get the currently authenticated user's ID...
$id = Auth::id();
If it's for admin panel or something, you can use hidden input:
{!! Form::hidden('user_id', $user->id) !!}
But if you want to pass an ID of a current user, do not pass it through a form. Use auth()->id() in a controller to get ID.
Okay, but what if it isn't auth but something else and I want use it:['LibraryController#sth', $sth->sth_id], how to use it in LibraryController ?

Laravel 5 - Handling empty select input on edit page

in my create function, I pass my view a list of users which I then display. However, I have changed it to the following
<select class="internalWork" name="internalWork">
<option value=""></option>
#foreach($users as $user)
<option value="{{ $user->userName }}">{{ $user->userName }}</option>
#endforeach
</select>
So I have added an empty option before the list is displayed. This is fine because this tables column allows null input.
In my edit function, I get the list
$users = User::lists('userName', 'userName');
I then pass this to my edit view. In my edit view, I have the following
<div class="form-group">
{!! Form::label('internalWork', 'Internal work for:', array('class' => 'col-sm-5 control-label red')) !!}
<div class="col-sm-7">
{!! Form::select('internalWork', $users, null, ['class' => 'internalWork']) !!}
</div>
</div>
If I selected a user in the create, then when I go to the edit page, that user is selected by default. However, if I do not select a user in the create, on the edit page, the first user in the list is displayed by default.
How can I get it to display an empty input if the database value is null?
Thanks
I think the issue is that although you are providing an empty option in the create view, you are not doing so in the edit view.
If, in your edit function, you populate your array like this:
$users = ['' => ''] + User::lists('userName', 'userName');
then I think the empty option will be selected if userName is empty. This is essentially concatenating an empty pair to the beginning of the $users array that you're passing to the view.
You might also consider constructing the create view in the same way - using the above code to generate your $users array in your create method and then using the Form facade in the view.
I've found this quite useful for working with Form::select(): Creating a Select Box Field
#if (!empty($users)
// the Form
#else
// No result
#endif
OR
array_unshift($users, "There are no users") // Prepend null value to $users
...
// Form::select(..etc