How to conditionally disable a form input field

*爱你&永不变心* 提交于 2019-12-04 11:46:37

The disabled attribute of <g:select> (and many other <g:...> form field tags) can be a boolean-valued expression:

<g:select name="teacherType" from="${TeacherType?.values()}"
  keys="${TeacherType.values()*.name()}" required=""
  value="${teacherInstance?.teacherType?.name()}"
  disabled="${mode == 'edit'}"/>

This will render as disabled="disabled" if the expression evaluates to true, and as the absence of a disabled attribute (i.e. the field will not be disabled) if the expression is false. You could even use a boolean entry in the model, e.g. render the template with

model="[teacherInstance: teacherInstance, editing:true]"

(or editing:false respectively) and then say disabled="${editing}" on the <g:select>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!