Im displaying a form as a representation of data in my database. Im using a form because the data is editable by certain users.
The form contains a dropdown list:
<select name="Status" id="Status">
<option id="confirmed" value="Confirmed">Confirmed</option>
<option id="completed" value="Completed">Completed</option>
<option id="released" value="Released">Released</option>
<option id="deleted" value="Deleted">Deleted</option>
</select>
When loading the view I pass in a variable '$status' which is from the database. How do I set the 'selected' attribute for which ever option $status is equal to?
Im using blade tempting engine
if you're using blade template you may want to use the Form helper to build your form fields, pass an array of items to be listed as dropdown options on the 2nd parameter and at the 3rd parameter pass your selected value, see the code below for reference
From controller:
$selectedValue = "confirmed";
$statuses = array(
array("id"=> "confirmed", "value" => "Confirmed"),
array("id"=> "completed", "value" => "Completed"),
array("id"=> "released", "value" => "Released"),
array("id"=> "deleted", "value" => "Deleted"),
);
return view("yourview")->with("statuses", $statuses)->with("selectedValue", $selectedValue);
On your view you could do something like below:
{!! Form::select('status', $statuses , $selectedValue, ['class' => 'form-control', 'id' => 'status']) !!}
Note: the code is untested but should work in your case.
Also please check https://laravelcollective.com/docs/5.2/html
Related
I have a dropdownList which looks like this:
<select id="singleregisterform-titleid" name="SingleRegisterForm[titleId]">
<option value="1">Dr</option>
<option value="2">Miss</option>
<option value="3">Mr</option>
<option value="4">Mrs</option>
<option value="5">Ms</option>
<option value="6">Prof</option>
</select>
When I submit my form I can see this in the app.log file:
'SingleRegisterForm' => [
'titleId' => '1'
'firstName' => 'Kate'
'lastName' => 'Becky'
But when the Model tries to do the save:
$person = new Person();
$person->title_id = $this->titleId;
$person->firstname = $this->firstName;
$person->lastname = $this->lastName;
$person->save();
I get this error:
COLUMN title_id cannot be NULL.
INSERT INTO `person_register` (`title_id`, `firstname`, `lastname`) VALUES (NULL, 'Kate', 'Becky');
Any ideas?
This is very funny. I would have expected Yii2 to not worry about fields which are not part of the validation rules. In this case with my DropDownList, even if the user does not change it, it will still have a value which will be 1, the first one in the list.
Yii2 did not send this back to the Model because I don't have this field as part of the validation rules.
I had to add this to my rules:
['titleId', 'required'],
Now it works perfectly.
Multiselect Drop Down of Color and Size in Dynamic Table with name attribute
In My Form I have Dynamic Table, Having Multi Select Dropdowns with same names color_id[] and size_id[] in each row. I am Not Getting How to save the multi selected values as comma separated values in each row in database. Here I have tried to save in database, but not working.
HTML CODE:
<table><tbody><td><select name="color_id[]" class="select2" id="color_id" style="width:200px; height:100px;" required multiple></select></td><td> <select name="size_id[]" class="select2" id="size_id" style="width:200px; height:100px;" required multiple> </select></td></tbody></table>
Laravel Save Code in Controller:
$class_ids = $request->input('class_ids');
for($x=0; $x<count($class_ids); $x++) {
# code...
$color_ids = implode(',', $request->color_id[$x]);
$size_ids = implode(',', $request->size_id[$x]);
$data3[]=array(
'bom_code'=>$TrNo,
'bom_date'=>$request->bom_date,
'cost_type_id'=>$request->cost_type_id,
'Ac_code'=>$request->Ac_code,
'season_id'=>$request->season_id,
'currency_id'=>$request->currency_id,
'item_code' => $request->item_codes[$x],
'class_id' => $request->class_ids[$x],
'description' => $request->descriptions[$x],
'color_id' => $color_ids,
'size_array' => $size_ids,
'consumption' => $request->consumptions[$x],
'unit_id'=> $request->unit_ids[$x],
'rate_per_unit' => $request->rate_per_units[$x],
'wastage' => $request->wastages[$x],
'bom_qty' => $request->bom_qtys[$x],
'total_amount' => $request->total_amounts[$x],
);
}
BOMSewingTrimsDetailModel::insert($data3);
I have taken two hidden input box having name color_arrays[] and size_arrays[] in every row in the same columns color and size. I wrote below java script function to get comma separated values from multi-select drop-down of color an size. and i save hidden input box values to database.
$(document).on('change', 'select[name^="color_id[]"],select[name^="size_id[]"]', function(){CalculateQtyRowPros2($(this).closest("tr"));});
function CalculateQtyRowPros2(row){
var color_id=row.find('select[name^="color_id[]"]').val().join(",");
var size_id=row.find('select[name^="size_id[]"]').val().join(",");
row.find('input[name^="color_arrays[]"]').val(color_id);
row.find('input[name^="size_arrays[]"]').val(size_id);}
It worked for me.
How I can add an extra attribute like in below screenshot state_id=1 in options list for all.
<?= $form->field($model, 'district_id')->dropDownList(ArrayHelper::map($Districts, 'id', 'name')) ?>
You need to iterate through the $Districts array and associate all the attributes you want to add to the <option> of the dropdown, i assume that your $Districts array has something like below
$Districts=[
1=>"North Andaman",
2=>"South Andaman"
3=>"Nicobar"
];
Now you need to iterate that array and associate the attributes with every option
foreach ($Districts as $id => $name) {
$optionAttributes[$id] = ['my-attr' => 'value'];
}
The above will show you something like
Array
(
[1] => Array
(
[my-attr] => value
)
[2] => Array
(
[my-attr] => value
)
[3] => Array
(
[my-attr] => value
)
)
Now when creating your dropdown you should pass this array to the options option of the dropdownList() see below
echo $form->field($model, 'district_id')->dropDownList(
$Districts,
['options' => $optionAttributes]
);
Now if you see the source of the page it will show you the dropdown like below
<select id="contacts-district_id" name="Contacts[district_id]" class="form-control">
<option value="1" my-attr="value">North Andaman</option>
<option value="2" my-attr="value">South Andaman</option>
<option value="3" my-attr="value">Nicobar</option>
</select>
I am using the following HTML to show a selected drop down with values from the database and rest of the others in the list. It shows the selected name correctly but the selected is also displayed in the list.
How to remove the second time show selected name in the drop down list? Is this a good way to use drop down menu? Here Jobcategory and Jobdetails are associated.
Im using Laravel 4.2 and this is the HTML:
// view drop down form to save data
<div class="large-6 columns">
<label>Job Category
<select name="category[]">
<option value="">Select Category</option>
#foreach(JobCategory::all() as $jcat)
<option value="{{ $jcat->id }}">{{ $jcat->name }}</option>
#endforeach
</select>
</label>
</div>
// Edit drop down form to update the selected value
<div class="large-6 columns">
<label>Job Category
<select name="category[]">
<option value="{{$jobedit->jobcategory->id}}">{{$jobedit->jobcategory->name </option>
#foreach(JobCategory::all() as $jcat)
<option value="{{ $jcat->id }}">{{ $jcat->name }}</option>
#endforeach
</select>
</label>
</div>
// database table for jobcategories
id | name
1 | Accounting/Finance
2 | Advertisement/Event Mgt.
3 | .....
// after save into jobdetails table
id | jobcategory_id | .......
1 | 5 | ...
I can retrieve the value of jobcategory in the edit form but it shows twice one in the selected value and other in the listed value of all jobcategory. This is the problem and i want only show the selected value and then rest of the others from jobcategory table without duplicate value of selected in the drop down. plz help.
// controller to to edit
public function getJobEdit($id)
{
$jobedit = JobDetail::find($id);
return View::make('employers.edit_single_jobs')->with('jobedit', $jobedit);
}
// JobDetail --model
public function jobcategory()
{
return $this->belongsTo('JobCategory');
}
// JobCategory --model
public function jobdetails()
{
return $this->hasMany('JobDetail', 'jobcategories');
}
Have a look at the Forms & HTML helper of laravel.
Generating A Drop-Down List With Selected Default
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');
where the first argument is the name of the select box. The second argument is an array of all entries in the box and the last argument determines which of the array elements is the selected one.
in your case it could look something like this:
{{ Form::select(
'categoryName',
array(
'Accounting/Finance' => 'Accounting/Finance',
'Advertisement/Event Mgt.' => 'Advertisement/Event Mgt.',
// to be continued ...
),
'$cat->category_name'
); }}
//Edit
<div class="large-6 columns">
<label>Job Category
{{ Form::select('category[]', ['' => 'Select a category'] + $all_categories, $jobedit->jobcategory->id) }}
</select>
</label>
</div>
where $all_categories should be an array of all categories as in the first example. You can get this from JobCategory::all().
This is how I set up drop-downs in my projects. I prepare the data in my controller (you'd obviously need a route and controller set up for this, I'm assuming you've done that):
Controller
public function index()
{
$categories = \Category::lists('name', 'id')->orderBy('name'); // assuming you have a Category model
view('categories', compact('categories'));
}
Then you can use the Forms and HTML helper as Peh mentioned, this isn't available as a default in Laravel 5 so you'd need to add it into your project using composer. To do so either run composer install illuminate/html or add "illuminate/html": "~5.0" to your composer.json file and then run composer install. You'd then need to add 'Illuminate\Html\HtmlServiceProvider' to config/app.php in the providers array then add 'Form' => 'Illuminate\Html\FormFacade' and 'HTML' => 'Illuminate\Html\HtmlFacade' to the aliases array in the same file. Once that's sorted you can use the Form and HTML helper in your view, as so:
View
{{ Form::select('categoryName', ['' => 'Select a category'] + $categories) }}
Your view would need to be saved as filename.blade.php if using the handlebars, otherwise echo it within <?php ?> tags.
Hope that helps you on your way.
When i am adding form to view & specifying parameters as
<?= $form->field($model, 'form_name', ['options' => ['id' => 'formName', 'name' => 'formName']])->textInput(); ?>
But, when i run in the browser & check for view page source, there it shows me
<input type="text" id="submitform-form_name" class="form-control" name="SubmitForm[form_name]">
this disturbs my javascript calling for field input. How to stop yii2 from generating its own id???
You are passing options to ActiveField. If you want override id and name attributes, pass them in textInput() options like so:
<?= $form->field($model, 'form_name')->textInput(['id' => 'formName', 'name' => 'formName']) ?>
Generated html output will be:
<input type="text" name="formName" class="form-control" id="formName">
Note that after that client validation for this attribute will stop working and that attribute won't be massively assigned.