Search code examples
grailsinputplaceholdergsp

grails input tag: placeholder not shown if value is a whitespace


I have an input tag field in my gsp (Grails project) and I want to use a placeholder to show some text when field is empty:

<input type="text" name="textField" id="textField" value="${receiptInstance?.patient?.surname} ${receiptInstance?.patient?.name}" placeholder=<g:message code="patient.choose" default="Insert patient..." />/>

When I create a new object, value is not empty, but it has got a whitespace, so placeholder is not shown. How can I change this kind of behaviour to use placeholder? Is there a way to eliminate the whitespace in value?


Solution

  • A workaround can be:

    <g:set var="myVal" value="${receiptInstance?.patient?.surname?:''} ${receiptInstance?.patient?.name?:''}"/>
    <input type="text" name="textField" id="textField" value="${myVal?.trim()}" placeholder='<g:message code="patient.choose" default="Insert patient..." />'/>
    

    Note:- Add ' in your placeholder, as I have added. Or you can use following code as well

    <input type="text" name="textField" id="textField" value="${myVal?.trim()}" placeholder="${g.message(code: 'patient.choose', default: 'Insert Patient...')}"/>