Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails checkbox simple question

I have this checkBox in my view inside a form:

<g:checkBox name="myCheckbox" value="${false}" />

In my controller, I how do I know if it is checked or not?

I tried:

if(!params.myCheckbox)
// obviously not,  because it will always be true

if(params.myCheckBox.checked)
// also dont work.
like image 610
Michael Avatar asked Feb 25 '23 02:02

Michael


1 Answers

if (params.myCheckbox) {
  println "checkbox is checked"

} else {
  println "checkbox is not checked or myCheckbox parameter is missing"
}

If you need to separately handle "checkbox is not checked" and "myCheckbox parameter is missing", use:

if (params.myCheckBox == null) {
  println "myCheckbox parameter is missing"

} else if (params.myCheckbox) {
  println "checkbox is checked"

} else {
  println "checkbox is not checked"
}
like image 164
Dónal Avatar answered Mar 08 '23 04:03

Dónal