It is reasonable to let unchecked box based on user's choice, but the unchecked box always causes form error in the server side,
def config(path: String) = Action { implicit request =>
configForm.bindFromRequest.fold(
formWithErrors => { // if checkedbox is not checked, the action in the server side will match this line
BadRequest(formWithErrors.toString)
},
configs => {
. ....})}
The configForm:
val configForm = Form(
mapping(
"k" -> optional(number),
"gc" -> optional(text),
"doStateCutOff" -> optional(text),
"stateCutoff" -> number(),
"doTimeCutOff" -> optional(text),
"timeCutoff" -> number(),
"doRegex" -> optional(text),
"regex" -> nonEmptyText,
"dochecklist" -> optional(text),
"pl" -> mapping(
"fs" -> checked("File System"),
"fsh" -> nonEmptyText,
"location" -> checked("Location"),
"locationh" -> nonEmptyText,
"pic" -> checked("Picture"),
"pich" -> nonEmptyText,
"deviceid" -> checked("DeviceID"),
"deviceidh" -> nonEmptyText,
"network" -> checked("Network"),
"networkh" -> nonEmptyText
)
{ (_, fs, _, loc, _, pic, _, dev, _, ntw ) => PropertyCheckList(fs, loc, pic, dev, ntw) }
{ pl => Some(true, pl.filesystem, true, pl.location, true, pl.picture,true, pl.deviceid, true, pl.network)}
)
{
(k, gc, doStateCutOff, stateCutoff, doTimeCutoff, timeCutoff,//verbose,
doRegex, regex, dochecklist,
propertyList )
=> {
Configs(k, gc, doStateCutOff, stateCutoff, doTimeCutoff, timeCutoff,
doRegex, regex, dochecklist,
propertyList
) }
}
{
configs => {
Some(configs.k, configs.gc, configs.doStateCutOff, configs.stateCutoff, configs.doTimeCutoff, configs.timeCutoff,
configs.doRegex, configs.regex, configs.dochecklist,
configs.propertyList )
}
}
)
Then I thought up a workaround that just change an attached input text box to checkbox, whenever checkbox is clicked, then I flip the vlaues in the inputtext box, and set checkbox always true so that the server will not complain.
Then the question is that, no matter what I tried to based on the answer Setting "checked" for a checkbox with jQuery? it is just not working!
Does the checkbox's value can only be set by the server side?????
In the form:
@checkbox(
configForm("fs"),
'_id -> "fs",
'_label -> "File System",
'_showConstraints -> false
)
@inputText( configForm("fsh"),
'_id -> "fsh",
'_showConstraints -> false
)
In the script:
I tried to test the set checkedbox value (when init the form in the sever side, I set the checkbox initial value to be false):
<script>
$(document).ready(function (){
var curO = $("#fs").is(":checked");
if(!curO) {
alert(!curO) // true
$("#fs").attr('checked', 'checked'); // prop does not work too..
alert($("#fs").is(":checked")); // still false
}
and in the checkbox event function:
$("#fs").onchange(function(){
var curO = $("#fs").is(":checked");
if(!curO) {
$(this).attr("checked", !curO);
}
var curInput = $("#fsh").val();
if(curInput == "true"){
$("#fsh").val("false") ;
}else {
$("#fsh").val("true") ;
}
Thanks for any hint!!!