How do I handle a multiple select form field in Perl? - perl

What is the best, in Perl, way to get the selected values of a multiple select form field?
<select name="mult" multiple="multiple">
<option value="1">Opt. 1</option>
<option value="2">Opt. 2</option> <!-- selected -->
<option value="3">Opt. 3</option>
<option value="4">Opt. 4</option> <!-- selected -->
<option value="5">Opt. 5</option>
</select>
I get regular form fields like this: $param1 = param('param1');

If you are using the CGI Module (and I really hope you are) then you can access the multiple values by assigning the param hash to an array and CGI does the rest. So in your example:
my #mult = $q->param('mult');
will store the selected values (2, 4) in the #mult array.

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>

validation on if condition in Laravel

How to give a validation on if condition like in my project case there are two inputs one is deal type and one is event name .so i have to give a condition like if deal type is single then i have to validate that event name otherwise no.
Here is my view :
<select name="deal_type">
<option value="">--Select--</option>
<option value="annual">Annual</option>
<option value="single">Single</option>
</select>
<select name="eventname">
<option value="">--Select--</option>
<option value="event1">Event1</option>
<option value="event2">event2</option>
</select>
my validation request.php :
public function rules()
{
return [
'deal_type'=>'required',
'eventname'=>'required_if:deal_type,single'
];
}
I was trying something like above but it is not working .Any suggestions please.

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

ColdFusion auto select option

What is the best way to automatically set a selected item for a select/option element on post-back? Here's the way we're currently doing it:
<select id="grade" name="grade">
<option value="A"<cfif form.grade = 'A'> selected="selected"</cfif>>A</option>
<option value="B"<cfif form.grade = 'B'> selected="selected"</cfif>>B</option>
<option value="C"<cfif form.grade = 'C'> selected="selected"</cfif>>C</option>
<option value="D"<cfif form.grade = 'D'> selected="selected"</cfif>>D</option>
<option value="F"<cfif form.grade = 'F'> selected="selected"</cfif>>F</option>
</select>
Is there a cleaner or easier way to do this with ColdFusion? Thanks in advance!
In my opinion, one of the best ways to go is to use a CFSelect:
<cfquery name="getGrades" datasource="#application.dsn#">
select gradeLetter from Grades
</cfquery>
<cfselect
name="grade"
query="getGrades"
display="gradeLetter"
value="gradeLetter"
selected="#form.grade#" />
Like this:
<cfsavecontent variable="GradeOptions">
A:A
B:B
C:C
D:D
F:F
</cfsavecontent>
<select id="grade" name="grade">
<cfloop index="CurOpt" list="#trim(GradeOptions)#" delimiters="#Chr(10)#">
<option value="#ListFirst(CurOpt,':')#"<cfif form.grade EQ ListFirst(CurOpt,':')> selected="selected"</cfif>>#ListRest(CurOpt,':')#</option>
</cfloop>
</select>
That assumes you always have separate value:label information - if your value and label are always the same, you can do this:
<cfsavecontent variable="GradeOptions">
A
B
C
D
F
</cfsavecontent>
<select id="grade" name="grade">
<cfloop index="CurOpt" list="#trim(GradeOptions)#" delimiters="#Chr(10)#">
<option<cfif form.grade EQ CurOpt> selected="selected"</cfif>>#CurOpt#</option>
</cfloop>
</select>
You could also do this with an array of structs.
<cfparam name="form.grade" default="C">
<cfset mydata = [{grade="A",value="A"},{grade="B",value="B"},{grade="C",value="C"},{grade="D",value="D"},{grade="F",value="F"}]>
<cfoutput>
<select id="grade" name="grade">
<cfloop array="#mydata#" index="i">
<option value="#i['value']#"<cfif form.grade EQ i['grade']> selected="selected"</cfif>>#i['value']#</option>
</cfloop>
</select>
</cfoutput>
using cfscript with functions
<cfscript>
Function setSelected(val1, val2){
if (val1 EQ val2)
{
Return 'selected="selected"';
}
else
{
Return '';
}
}
</cfscript>
<select id="grade" name="grade">
<option value="A" #setSelected('A', form.grade)#>A</option>
<option value="B" #setSelected('B', form.grade)#>B</option>
<option value="C" #setSelected('C', form.grade)#>C</option>
<option value="D" #setSelected('D', form.grade)#>D</option>
<option value="F" #setSelected('F', form.grade)#>F</option>
</select>
What about this?
<cfparam name="form.grade" default="A">
<cfoutput>
<select id="grade" name="grade">
<cfloop index="code" from="65" to="90">
<option value="#Chr(code)#"<cfif form.grade EQ Chr(code)> selected="selected"</cfif>>#Chr(code)#</option>
</cfloop>
</select>
</cfoutput>
A bit tricky, yeah :)
You know, it may be unpopular to say so, but the solution that you originally outlined in the question is the best.
It's simple, it's easy to see what is being done, and it's not trying to be tricky just to be tricky.
Sometimes you just need to wear gloves
http://thedailywtf.com/Articles/The_Complicator_0x27_s_Gloves.aspx
To expand on my other answer, what you should be doing is properly separating your data from your interface, by storing the grades in your database, and using code similar to:
<cfset GradeOptions = Grades.readAvailable() />
<select id="grade" name="grade">
<cfloop query="GradeOptions">
<option value="#GradeCode#"
<cfif Form.Grade EQ GradeCode>selected="selected</cfif>
>#GradeCode# - #GradeDesc#</option>
</cfloop>
</select>
(Again, if grades are only to be treated as a single letter, the values can be provided in a simple list/array rather than query.)
The key thing is, you can change your grade structure without having to change your interface, and likewise updating the interface doesn't require you to know what the grades are.
To be honest I can't see how any of these are better than a corrected version of your initial pass (below)
<select id="grade" name="grade">
<option value="A"<cfif form.grade EQ "A"> selected </cfif> >A</option>
<option value="B"<cfif form.grade EQ "B"> selected </cfif> >B</option>
<option value="C"<cfif form.grade EQ "C"> selected </cfif> >C</option>
<option value="D"<cfif form.grade EQ "D"> selected </cfif> >D</option>
<option value="F"<cfif form.grade EQ "F"> selected </cfif> >F</option>
</select>
It is simple, clean and understandable.
If you just feel a need to be slicker and are going to be doing a lot of UI manipulation invest some time in jQuery. Study Ray Camden's jQuery and CF Posts and Ben Nadel's Javascript and CF Posts and soon this will be second nature...
<script type="text/javascript">
jQuery(document).ready(function() {
$("#grade option[value='<CFOUTPUT>#FORM.Grade#</CFOUTPUT>']")
.attr('selected', 'selected');
});
</script>
<select id="grade" name="grade">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="F">F</option>
</select>
Sure, it's wackier looking than some of the other options here but amazingly powerful at solving problems that CF just isn't good at once you learn it (trust me, it will quickly make sense and you will wonder how you ever did client UI code without it).
Learn any of the popular JavaScript libraries and your ColdFusion client side code will become dramatically more elegant and powerful.