How use form select in cake php with foreach - cakephp-2.x

I want to fill a form select with informations of a table :"foreach"
i don't find a solution , someone can help me !i use cakephp 2.5.5 .
i want solution like that , but with cake php .
<select id="Select" name="section_id" class="form-control">
#foreach($sections as $section)
<option value="{{$section->id}}">{{$section->section_name}}</option>
#endforeach
</select>

Try this
First fetch data from database like this :
$result = $this->ModelName->findById('id');
$this->set('result',$result);
Then check in view file
<select id="Select" name="section_id" class="form-control">
#foreach($sections as $section)
<option <?php echo ($result['ModelName']['section_id'] == $section->id) ? 'selected="selected"' : '' ?> value="{{$section->id}}">{{$section->section_name}}</option>
#endforeach
</select>
Use Cakephp form helper to create select box so cake automatic display selected data like this
$this->From->select('ModelName.select_box_name',$sections);

Related

Angular2 reactive forms select how to set invalid?

I use reactive forms within my app. In a certain form I want to display a required (Validators.required) select like this:
<select class="form-control"
[id]="dformControl.key"
[formControlName]="dformControl.key"
[multiple]="dformControl.multiple">
<option *ngIf="!dformControl.value"
value="undefined">
Choose ...
</option>
<option *ngFor="let opt of dformControl.options"
[value]="opt.value"
[selected]="dformControl.value == opt.value">
{{opt.label}}
</option>
</select>
The problem is whether I use value="undefined" or value="" the form control still is set to valid because it got a value. Do not present the value attribute results in value="Choose ...".
Am I using select with reactive forms in a false way or how would I be able to make the option "Choose ..." being not valid??
Assigning initial value of select control to null will do the trick. Try below,
model_property = null
....
this.fb.group({
....
'control_key' : [this.model_property, Validators.required]
...
})
Check this Plunker!!, Look into app/reactive/hero-form-reactive.component.ts file.
I updated the Plunker to include below and it seems to be working,
<select id="power" class="form-control"
formControlName="power" required >
// see the value is set to empty,
<option value="">Choose...</option>
<option *ngFor="let p of powers" [value]="p">{{p}}</option>
</select>
Hope this helps!!
What I do is add a blank option and when that is selected since there is no value it is not valid.
<select class="form-control"
[id]="dformControl.key"
[formControlName]="dformControl.key"
[multiple]="dformControl.multiple">
<option></option>
<option *ngFor="let opt of dformControl.options"
[value]="opt.value"
[selected]="dformControl.value == opt.value">
{{opt.label}}
</option>
</select>

Make the tags and select list active without using Form Builder in laravel?

I am unable to make a dropdown select list using the below code to generate a location select dropdown where database table =>location and column=>name
<select name="location">
#foreach ($locations as $loaction)
<option value="{{ $loaction->name }}">{{ $loaction->name }}</option>
#endforeach
</select>

Default value on form select (with foreach loop)

So, I want to achieve a <select> like solution where a default value is equal to the value that is currently in the database, so the 'old value'.
1
I have this, where I loop a foreach to fill <option>.:
<select id="size" name="size" class="form-control">
#foreach ($sizes as $s)
<option value="{{ $s->value }}">{{ $s->name }}</option>
#endforeach
</select>
Now where I want to use this is an edit-form in laravel 5.2 where it fills in this form with the current values from the database for the selected user. My input fields with text fill in correctly but this select doesn't.
2
Working with Laravel forms I know this is possible to set the default value to S. But I don't know how to loop this is foreach...:
{{ Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S') }}
So I need to find a way to set default value with HTML-select tags while in a loop with values from my controller or find a way to loop in Laravel Forms.. Either is good, as long as it works
Don't know if this is what You are asking, but if You know current value, You can check it with if statement:
<select id="size" name="size" class="form-control">
#foreach ($sizes as $s)
#if ($s->value == $currentSize)
<option selected value="{{ $s->value }}">{{ $s->name }}</option>
#else
<option value="{{ $s->value }}">{{ $s->name }}</option>
#endif
#endforeach
</select>

Catalyst $c->req->params empty?

Trying to Catalyse some prototype pages with form fields. My Catalyst controller does not seem to be getting the inputs from the form when it is submitted.
I have tried to reduce the template & controller down as far as I can and I am still getting this problem.
template is simply:
<body>
<form action="/minimal-testing" method="get">
<select id="select02">
<option value="cat1">cat1</option>
<option value="cat2">cat2</option>
</select>
<select id="select06">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit" value="submit" title="submit" />
</form>
<p> Hello, params says: </p>
<p>
[% FOR param IN params ; param.key ; ' = ' ; param.value; END %]
</p>
</body>
Controller is:
sub minimal_testing :Path('minimal-testing') :Args(0) {
use Data::Dumper;
my ( $self, $c ) = #_;
$c->stash(params=>$c->req->params);
$c->stash(template => "dynamic/minimal-testing.tt");
$c->log->debug(Dumper($c->request->params));
}
When I browse to the form, pick from the select options, and submit, my debug log simply says
[debug] $VAR1 = {};
Obviously I am missing something so obvious I am just not seeing it... please enlighten me.
You're dead right. I suspect your Catalyst log should be giving you a clue what's going wrong here. The problem isn't with Catalyst.
Basically, it's malformed HTML - your form inputs each need a name parameter that can be passed back to the server in the POST.
<select id="select02" name="select02">
<option value="cat1">cat1</option>
<option value="cat2">cat2</option>
</select>
id is for CSS, name is for FORM processing.
Try that, and you should get logging like:
[11:31:28.499 16014:debug] Body Parameters are:
.-------------------+----------------------------------------.
| Parameter | Value |
+-------------------+----------------------------------------+
| select02 | cat1 |
| select06 | 2 |
'-------------------+----------------------------------------'
Hope that helps.

Converting checkbox to select (perl)

Before:
<input type='checkbox' name='killerFeature' id='killerFeature'
<%= param('killerFeature') ? ' checked' : ''%>
/>
Now:
<select name="killerFeature" id="killerFeature" class="select">
<option value="1">Enable</option>
<option value="0">Disable</option>
</select>
How do I insert the same checked (should be 'selected' now I guess?) condition in the select now?
This is a perl app, built using Mojolicious web framework.
Many thanks for your help!
Yes, the condition should be selected (selected="selected") but i belive you already figured that out from your other post :)
<select name="killerFeature" id="killerFeature" class="select">
<option value="1" selected="selected">Enable</option>
<option value="0">Disable</option>
</select>
Also from what i saw there inst a way to create the select like you can for the input as in the below example:
<%= input 'test', type => 'text' %>
So it would be something like:
<%== param('killerFeature') eq $my_var ? ' selected="selected"' : ''; %>
Ofc you would need to replace the above to your current variables and field names.
GL:)