Converting checkbox to select (perl) - 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:)

Related

How do I update which option is selected based on EJS

I have a express server that passes in information from a mongo db into a ejs file. I am trying to update an item in the db. I want to have the current item information entered in as placeholders.
Here is the ejs code I'm not sure how to select one option based on the info from the db.
<select name="category" id="Category" placeholder="<%= product.category %>">
<option value="vegetable">Vegetable</option>
<option value="fruit">Fruit</option>
<option value="dairy">Dairy</option>
</select>
I understand that to have an option as the placeholder you generally have to use <option selected>
Any suggestions are much appreciated.
Use a Ternary Operator
<select name="category" id="Category">
<option value="vegetable" <%=product.category==='vegetable' ? 'selected' : '' %>>Vegetable</option>
<option value="fruit" <%=product.category==='fruit' ? 'selected' : '' %>>Fruit</option>
<option value="dairy" <%=product.category==='dairy' ? 'selected' : '' %>>Dairy</option>
</select>

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>

How use form select in cake php with foreach

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);

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.

jQuery addClass using input select

Im a bit of a jQuery novice.
I have 5 options in a drop down menu.
I want to add a class in a div depending on which option is selected.
This class will then change the layout of the content using CSS.
Here is an example if it helps!
Thanks
<form>
<select>
<option id="1">layout 1</option>
<option id="2">layout 2</option>
<option id="3" selected>layout 3</option>
<option id="4">layout 4</option>
<option id="5">layout 5</option>
</select>
<div class="3">styled content</div>
</form>
You can use the ".attr()" to set the class attribute of the div onchange.
You're best to change the option id to value first. then:
$("select").change(function() {
$("div").attr("class", $(this).val());
});
(EDIT) Change it to:
$("select#np_blog_layout").change(function() {
$("div#changebox").attr("class", $(this).val());
});
What 'rudeovski ze bear' said, but if you still want to set it to the div's class to the selected elements id, here it is.
$('select').change(function() {
$('div').attr('class', $(this).attr('id'));
});
First off, you don't have to use id to your options. Put the values in value attribute. Put ID to your div and select so you can select them using jQuery.
<form>
<select Id="selectElement">
<option value="1">layout 1</option>
<option value="2">layout 2</option>
<option value="3" selected>layout 3</option>
<option value="4">layout 4</option>
<option value="5">layout 5</option>
</select>
<div id="styledContent" class="3">styled content</div>
</form>
On JS
//Attach event handler to select element's onchange event
$('#SelectElement').bind("change",changeStyle);
//The event handler
function changeStyle()
{
//Set class to selected value
$('#styledContent').class($('#SelectElement').val());
}