0

I am trying to figure out if an array I have, has more than one string associated with it or if it is just a character array (string). Right now if I enter a singular ID of 1234 I will get an array of 1,2,3,4 but if I say there are two ID's 12 and 34 the array will return 12, 34. How would I check for a string when it should always be an array of strings?

<div class="area">
<h2>Select all people who will be Traveling</h2>
<div>
<g:if test="${disabled=='false'}">
 <g:select name="selector" class="claim" value="None" from="${fullList}" optionKey="studentID" optionValue="${{it.firstName + ' ' + it.lastName}}" noSelection="${['null':' ']}" disabled="${disabled}"/>
   <g:if test="${tripInstance?.student!= null }">
   <g:each var="i" in="${(0..<tripInstance?.student?.length) }">
    <div>
     <input class='field' name='Name' readonly type='text' value='${fullList.firstName[(tripInstance?.student[i]).toInteger()]} ${fullList.lastName[(tripInstance?.student[i]).toInteger()]}'/>
     <input class='field' name='student' readonly type='hidden' value='${tripInstance?.student[i]}'/>
       <label class='removeEdit fakeLink'> Remove</label>
    </div>
   </g:each>
   </g:if>
</g:if>

<g:if test="${disabled=='true'}">
<g:if test="${tripInstance?.student!= null }">
 <g:each var="i" in="${(0..<tripInstance?.student?.size()) }">
  <div>
   <input class='field' name='student' readonly disabled="${disabled}" type='text' value='${tripInstance?.student[i]}'/>
  </div>
 </g:each>
 </g:if>
</g:if>
</div>
</div>

I have attempted to check based on class. I can't check based on size as a string will have a size and so will an array of strings. It is a string array instead of Int array due to other parts of the code wanting it in this format. Hopefully I'm not overlooking something easy.

4
  • Could it not be 1, 234 or 123, 4 or 1,2,34 or 12,3,4? Basically, how do you determine the valid states? Commented Dec 21, 2015 at 20:15
  • It could be a student of ID of 1 and 234 but the array would register that those are two separate elements, however if only one student ID of 91 or 123 or even 4 (any number of digits but only a single entry) the array will become a character array and split that one entry into multiple elements Commented Dec 21, 2015 at 20:21
  • Yes, but how do you disambiguate? You say "the array"... what array? Commented Dec 21, 2015 at 20:24
  • tripInstance?.student is a string[] of ID's that were displayed from the list fullList and selected by the user. but if only one ID is selected by the user tripInstance?.student becomes a character array where any double digit or higher number becomes multiple elements instead of a singular element. Beyond that I am not sure how else to describe the issue sorry. Commented Dec 21, 2015 at 20:34

1 Answer 1

2

Grails provides a convenience method on the params object to always return a List, rather than possibly iterating over a single String. It is conveniently called list():

def ids = params.list('ids')

It can be found in the documentation under Simple Type Converters.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.