Grails GSP null security check problem

I am trying to populate a textbox in my GSP as such:

<label>Phone(aaa-bbb-cccc):</label>&nbsp<g:textField name="phone" style ="border-radius: 5px" 
                    value="${recordToEdit.telephones = [] ? null : recordToEdit.telephones.first()}"></g:textField><br> 

      

but it still tells me that I cannot access first () on an empty list. phones is a list of strings, each of which is a phone number.

+3


source to share


3 answers


as @ gross-jonas pointed out is recordToEdit.telephones = [] ? .. : ..

already terribly wrong unless it's a typo

the check you are trying to do should look like this:

value="${recordToEdit.telephones ? recordToEdit.telephones.first() : ''}"

      



or

value="${recordToEdit.telephones?.getAt( 0 ) ?: ''}"

      

+5


source


You can just use Null Safe Operator (?) As

${recordToEdit.telephones?.first()}

      

for null checks, which is not enough.

UPDATE



to check for empty lists and null checks,

${ recordToEdit.telephones ? recordToEdit.telephones[0] : '' }

      

will be good.

+4


source


Dude, didn't you mean ==

instead =

?

It looks like you are overwriting yours telephones

which is issued successfully rather than comparing it.

+1


source







All Articles