grails Radio Button True or false checked property - grails

I have radio group with two values. On editing a record, the radio should be set to checked or unchecked depending on the value from database.I use
<input type="radio" name="value1" value="${someValue}" ${it.id == true ?'checked="checked"' : ''}>
<input type="radio" name="value1" value="${someValue}" ${it.id == false ? 'checked="checked"' : ''}>
Using such code, gives me syntax error. Please correct my syntax.

Can't you use:
<g:radio name="value1" value="${someValue}" checked="${it.id}" />
(assuming it.id is a boolean as you seem to say)

Looks like you have to go this lengthy way. (assuming it.id yields a boolean)
<g:if test="${it.id}">
<g:radio name="value1" value="${someValue}" checked="${it.id}" />
</g:if>
<g:else>
<g:radio name="value1" value="${someValue}" />
</g:else>
You can use html input instead of g:radio in this case as well.

Related

Is there a way to populate user input fields with default values in thymeleaf spring?

Is there a way to populate user input fields with default value in thymeleaf?
I understand that th:field replaces the value="" tag, but I need to populate the user inputs with default number so, that if the user does not provide input, number 0 will be passed as the input.
I cannot do this in controller as my input type needs to be number, and my model attribute is String[] arraylist.
<input type="number" id = "a2s" name="a2" class="newMatch" value="0" min="0" max="11" th:field="*{player1score}">
<input type="number" id = "b2s" name="b2" class="newMatch" value="0" min="0" max="11" th:field="*{player2score}" >
Try this way:
<input th:value="*{player1score != '' ? player1score : 0}" //...other attr />
th:field will override value, name and id attributes. To populate the field you will have to use the tags separately, like so:
<input type="number" id = "a2s" name="a2" class="newMatch" value="0" min="0" max="11" th:name="*{player1score}" th:id="*${playerscore}">
Solved this by using name and id html fields to replace the need for th:field="*{myVar}" . Like so:
<input type="number" class="newMatch" value="0" min="0" max="25" name="player1score" id="player1score" >
<input type="number" class="newMatch" value="0" min="0" max="25" name="player2score" id="player2score" >
The th:field tag replaces name , id and value fields. So one way to do this is to just use html tags instead.

Saving Set of Domains For a Parent

I have the following Domains:
class Attribute {
static hasMany = [attributeParameters: AttributeParameter]
}
class AttributeParameter {
String value
Integer sequenceNo
static belongsTo = [attribute: Attribute]
}
I have a form where I want to display all the existing AttributeParameters for an Attribute and allow the user to populate their values and click Save. On Save, each AttributeParameter (which already has an ID) needs to be updated.
I'm currently drawing a blank on how I need to create the HTML for this to work. I've tried this:
Code simplified to clarity:
<form>
<input type="hidden" name="attributeParameters[0].id" value="1" />
<input type="text" name="attributeParameters[0].value" value="1234567" />
<input type="hidden" name="attributeParameters[1].id" value="2" />
<input type="text" name="attributeParameters[1].value" value="name" />
</form>
def save() {
def attribute = Attribute.get(params.id)
attribute.properties = params
}
and it populates the collection correctly, but it doesn't work because the AttributeParameter isn't being fetched before the save, so it is failing with an error:
A collection with cascade="all-delete-orphan" was no longer referenced
by the owning entity instance: com.foo.Attribute.attributeParameters
UPDATE:
I modified the HTML to the following:
<form>
<input type="hidden" name="attributeParameters.id" value="1" />
<input type="text" name="attributeParameters.value" value="1234567" />
<input type="hidden" name="attributeParameters.id" value="2" />
<input type="text" name="attributeParameters.value" value="name" />
</form>
And the controller:
params.list('attributeParameters').each {
def ap = AttributeParameter.get(it.id)
ap.value = it.value
ap.save()
}
This works. My only concern is the order in which the parameters come in. If they always come into the params object in the same order they show up on the form, then I should be ok. but if they ever come in differently, I could be modifying the value of the wrong AttributeParameter.
So still looking for a better way or some sort of verification that they params will always be first in-first out.
UPDATE 2:
I ran across this post and it is what I want but I cannot change Attribute.attributeParameters into a List. They need to stay as a Set.
Could you do something like this:
<form>
<input type="text" name="attributeParameter.1" value="1234567" />
<input type="text" name="attributeParameter.2" value="name" />
</form>
Where you create the names and values dynamically from each AttributeParameter:
<g:textField name="attributeParameter.${attributeParameterInstance.id}" value="${attributeParameterInstance.value}" />
And then in your controller
params.attributeParameter.each {id, val->
def ap = AttributeParameter.get(id)
ap.value = val
ap.save()
}
That way you have the actual id of each parameter directly and it wouldn't matter which order they were processed.

Ternary Operator in Razor View of MVC3 and Checked Attribute

I use this:
<input type="checkbox" value="#item.Id" checked="#(item.HasAccess ? "checked" : "")"/>
This worked correctly: I mean when HasAccess is true then checked="checked" and when
HasAccess is false then checked="" but always the checkboxs checked, how can I use ternary operator and handle checked attribute correctly?
Unfortunately, in razor V1, you must do it this way:
<input type="checkbox" value="#item.Id" #(item.HasAccess ? "checked=\"checked\"" : "") />
This is because in the HTML world, the mere presence of the attribute at all, regardless of the value, tells the browser to check the box.
In Razor V2, this will be less of a problem. See the conditional attributes section of the article below:
http://vibrantcode.com/blog/2012/4/10/whats-new-in-razor-v2.html/

select checkbox with hidden input type

Folks,
I am using watir-webdriver, I have a piece in my HTML DOM which gets generated on the fly when I enter some credentials, this piece has a bunch of checkboxes, the number of checkboxes vary, I have to select one checkbox, below is an example of this, here I want to select the second checkbox(the one that has value "BS" for the input type hidden but the value for input type checkbox is same for all):
<li class="dir">
<input type="checkbox" value="1" onclick="$(this).next('.should_destroy').value = (this.checked?0:1)" name="should_not_destroy">
<input class="should_destroy" type="hidden" value="1" name="import[dir_attributes][][should_destroy]">
<input type="hidden" value="" name="import[dir_attributes][][id]">
<input type="hidden" value="Automation" name="import[dir_attributes][][path]">
<span class="dir_mode">Include</span>
Automation
</li>
<li class="dir">
<input type="checkbox" value="1" onclick="$(this).next('.should_destroy').value = (this.checked?0:1)" name="should_not_destroy">
<input class="should_destroy" type="hidden" value="1" name="import[dir_attributes][][should_destroy]">
<input type="hidden" value="" name="import[dir_attributes][][id]">
<input type="hidden" value="BS" name="import[dir_attributes][][path]">
<span class="dir_mode">Include</span>
BS
</li>
I may be able to do this with XPATH, but wanted to try a non XPATH solution. The input type hidden has the appropriate value that I need, for example above the second checkbox has value "BS" for input type hidden. I tried to use the hidden method like this:
h = ##browser.hidden(:value, "BS")
h.select
But I dont know what to do after this. I am trying to select the checkbox based on the value of the hidden element. Any feedback is much appreciated.
I would suggest using the visible elements instead. I think it makes it easier to read the test and seems more stable.
Try:
##browser.li(:class => 'dir', :text => /BS/).checkbox.set
Here we go, I think this will do it
Since you have to select the checkbox based on the hidden, you're going to have to go up a level to the containing li, then drill down to the checkbox
#browser.hidden(value: 'BS').parent.checkboxes.first.set

radiobutton with ruby

i m doing a project on "student attendance management system".i using rail of version 1.9.1
and rail of version 2.5. i wanted to use radiobutton in my project to mark present and absent so how can i use?please send me the code and what should i do? if you want to give any suggetion so i will very happy.
Taken from the rails api docs:
radio_button(object_name, method,
tag_value, options = {})
Returns a radio button tag for
accessing a specified attribute
(identified by method) on an object
assigned to the template (identified
by object). If the current value of
method is tag_value the radio button
will be checked.
To force the radio button to be
checked pass :checked => true in the
options hash. You may pass HTML
options there as well. Examples
# Let's say that #student.attendance returns "rails":
radio_button("student", "attendance", "present")
radio_button("student", "attendance", "absent")
# => <input type="radio" id="student_attendance_present" name="student[attendance]" value="present" checked="checked" />
# <input type="radio" id="student_attendance_absent" name="student[attendance]" value="absent" />
radio_button("user", "receive_newsletter", "yes")
radio_button("user", "receive_newsletter", "no")
# => <input type="radio" id="user_receive_newsletter_yes" name="user[receive_newsletter]" value="yes" />
# <input type="radio" id="user_receive_newsletter_no" name="user[receive_newsletter]" value="no" checked="checked" />

Resources