Binding.scala: how to define checked in a Checkbox - scala

I want to define a checkbox in Binding.scala, like:
<input type="checkbox"
checked={elem.checked}
/>
elem.checked can have any String value (even an empty):the checkbox is always checked.
How can I get
<input type="checkbox"
checked/>
if checked, resp.
<input type="checkbox"/>
if not.

According to https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes , in XHTML 5, any text values means true.
Fortunately, Binding.scala XML literals allows attributes of Boolean type. If elem.checked is false (not "false"), the attribute will be removed.

Related

MechanicalSoup - Set hidden fields?

Using MechanicalSoup, does one have to set values for hidden fields in a form as well? Or can we just ignore them?
Here is their code in the HTML form:
<input type="hidden" name="details[sid]" />
<input type="hidden" name="details[page_num]" value="1" />
<input type="hidden" name="details[page_count]" value="1" />
<input type="hidden" name="details[finished]" value="0" />
<input type="hidden" name="form_build_id" value="form-OoBDi0_aQvgHZN-Iyc" />
<input type="hidden" name="form_id" value="webform_client_form_337" />
<input type="hidden" name="honeypot_time" value="1519679330|-x8kCHBe6qh7E" />
Hidden fields are typically used internally by the website you're browsing: they are generated with a value, and this value is sent together with non-hidden fields when you submit the form.
MechanicalSoup does what you expect here: it sends the values of hidden fields when you submit the form, so essentially you don't have to care about them: their value will be set by the website you're visiting, and then taken into account by MechanicalSoup just like a normal browser.
You shouldn't need to modify their value, because when using the website with a real browser, the user has no simple way to set a value (that's the point of hiding the fields ...).
You can add your own hidden input name:value pairs using the new_control() method. I've had to do this when the form used javascript to set a hidden element before form submission.
browser.new_control('hidden','name','value')
https://mechanicalsoup.readthedocs.io/en/stable/mechanicalsoup.html#mechanicalsoup.Form.new_control

Angular2 ng-dirty class is not set when ngModel value changes

I have the following form with html
<h1>ng-dirty Test</h1>
<form [formGroup]="myForm">
<input required [(ngModel)]='myVar' formControlName="myVar" />
</form>
myForm.controls.myVar.dirty: {{myForm.controls.myVar.dirty}}
<br/>
my value 1
my value 2
my value 3
When I enter a value manually into the input field, I see ng-dirty is set as a class.
However, when I click the links to set the value, I do not see ng-dirty is changing.
https://plnkr.co/edit/C3Y4pE?p=preview
Does anyone know what the reason is?

PHP GET Form Multiple Checkbox Array 'Checked' Status from URI Parameters

I want to use a form to do parametric filtering by passing values to a URI. Specifically, part of this form would entail multiple checkboxes for one parameter set, I'll call this 'my-checkbox-parameter'
What I Have
A basic form with checkbox fields that post the values via the GET method
This does correctly post the values as I'd expect
My code:
<form id="results-filters" class="form-inline" action="form.php" method="GET">
<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" id="val3" value="val1" name="my-checkbox-parameter[]" class="form-control">
Value 1
</label>
<label class="checkbox-inline">
<input type="checkbox" id="val2" value="val2" name="my-checkbox-parameter[]" class="form-control">
Value 2
</label>
<label class="checkbox-inline">
<input type="checkbox" id="val3" value="val3" name="my-checkbox-parameter[]" class="form-control">
Value 3
</label>
</div>
<button type="submit" class="btn btn-default">
Filter Results
</button>
</form>
What I'm Stuck on
Making the checkbox fields reflect 'checked' status based on what parameters are in the URL. For instance, if my URI is: /form.php?my-checkbox-parameter[]=val1&my-checkbox-parameter[]=val2 , how can I make sure that the field values for 'Value 1' and 'Value 2' are checked? Is JavaScript/AJAX the only way to do this?
Bonus points, not a super huge priority, but rather a 'nice to have'... Is there a better way to handle checkbox value arrays in URIs? For instance, if I have 10 checkbox fields, the concatenated URI with these parameters might be quite long...
Thanks in advance!
First:
$checked= array();
$checked=$_GET['my-checkbox-parameter'];
And then:
<input type="checkbox" id="val1" value="val1" name="my-checkbox-parameter[]" class="form-control" <?php if(in_array("val1", $checked))echo "checked"; ?> >Value 1
So if there is val1 in your get parameters, the check box will be checked.
Do it for all check boxes and it should be ok.
AND for the second question: actually you are doing well with my-checkbox-parameter[ ], it's the usual way to do it in PHP. But you can check this question for more ways.

How to use struts tag iterator and OGNL to realize multiple rows selection

I would like to do multiple rows selection. Rows are display through strut2 tag s:iterator, how can I get the selection information, which should contains a list of selected "id"
<s:form action='Selection'>
<s:iterator value="transInfos">
<input type='hidden' name=id value='<s:property value="id" />' />
<s:checkbox name="selected"/>
<s:property value="name" />
</s:iterator>
<s:submit value="Selection" />
</s:form>
One option which seems to me is to create a hidden field in your form like
<s:form action="selection">
<input type='hidden' name="selectedId" value=""/>
</s:form>
you can add a on-click event to your check-box and if it get checked you can add the value t a variable and set in the hidden field,each new addition should be added as new values in comma separated way like in end hidden field should be like
<input type='hidden' name="selectedId" value="1,2,3,4"/>
moment you submit the form you can parse the form value and can split it based on the separator ","
other option is to name the check-box with same name so moment it will get submitted the values of the checked one will be submitted as a collection, choice is all yours and you need to decide which way to go
I'm glad I can answer this question myself.
The answer is quite simple.
<s:form action="..." >
<s:iterator value="transInfos">
<input type="checkbox" name="transIds" value='<s:property value="transID" />'/>
</s:iterator>
<s:submit value="Select"/>
</s:form>
the value of checkbox is what you want to pass to the action, all the selected checkboxes will pass their values as a list to the action.

Do I really have to remove a second, hidden form field from within a label for my HTML to validate?

We have the following code in which we are getting errors in the w3c validator for "Any input descendant of a label element with a for attribute must have an ID value that matches that for attribute." and "The label element may contain at most one input, button, select, textarea, or keygen descendant." Is this something that should just be ignored by the validator (as it is seeminlgly correct) or should it be changed to appease the w3c? Note this is html5 doctype.
<fieldset>
<label for="user_is_subscribed">
<input type="hidden" value="0" name="user[is_subscribed]">
<input type="checkbox" value="1" name="user[is_subscribed]" id="user_is_subscribed">
Newsletter Signup
</label>
<span class="checkLabel">We will never spam or give away your information</span>
</fieldset>
Thank in advance!
Labels should contain at most one input element. Move the hidden input out of the label. Also, when an input is a descendant of a label, the for attribute is superfluous.
<fieldset>
<input type="hidden" value="0" name="user[is_subscribed]">
<label>
<input type="checkbox" value="1" name="user[is_subscribed]" id="user_is_subscribed">
Newsletter Signup
</label>
<span class="checkLabel">We will never spam or give away your information</span>
</fieldset>
Is this something that should just be ignored by the validator
No
(as it is seeminlgly correct)
It isn't
or should it be changed
Yes
to appease the w3c?
No. It should be changed because it is wrong, and browsers have to error correct to figure out which element the label is associated with.
The label isn't labeling the hidden input, move it elsewhere.