Announcement Announcement Module
Collapse
No announcement yet.
Grails elvis operator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Grails elvis operator

    Hello

    I am new to Grails and Groovy. I am currently walking through one of the tutorials which introduces you to grails plugins. Searchable plugin is the one used as an example but on installing the plugin sucessfully I noticed that when using the search tool to search my classes and leaving the input field blank it gives me a <EOF> found error.

    Not a problem as I then proceeded to adapt the search method within my class controller from:

    def search = {
    flash.message = "Search results for: ${params.q}"
    def resultsMap = Race.search(params.q, params)
    render(view:'list', model:[
    raceInstanceList:resultsMap.results,
    raceInstanceTotal:Race.countHits(params.q)
    ])
    }

    to:

    def search = {
    def qStr = params.q != "" ? params.q : "*"
    flash.message = "Search results for: $qStr"
    def resultsMap = Race.search(qStr, params)
    render(view:'list', model:[
    raceInstanceList:resultsMap.results,
    raceInstanceTotal:Race.countHits(qStr)
    ])
    }

    This works fine but I was hoping to use a more elegant solution. Can anyone help with the following points:

    1. Can I improve the elvis operator? I tried the below instead of the above but it did not work. I thought that it would implicitly apply the params.q to my qStr variable if the condition was true?

    def qStr = params.q != "" ?: "*"

    2. How can I perform a regExpression and check for all non-expected entries. I.e. If I enter a space into the field this also causes an error.

    Look forward to some Guru to give me advise. Please be gentle as I'm a beginner. No wacky solutions please :-)

    Kind regards

    Andyrh3

  • #2
    Originally posted by andyrh3 View Post
    This works fine but I was hoping to use a more elegant solution. Can anyone help with the following points:

    1. Can I improve the elvis operator? I tried the below instead of the above but it did not work. I thought that it would implicitly apply the params.q to my qStr variable if the condition was true?

    def qStr = params.q != "" ?: "*"
    This is the typical code you would use:
    Code:
    def qStr = params.q ?: "*"
    If params.q is a non-empty string (and hence also not null), then it's value will be used. Otherwise the "*" string will be assigned to qStr instead.

    Originally posted by andyrh3 View Post
    2. How can I perform a regExpression and check for all non-expected entries. I.e. If I enter a space into the field this also causes an error.
    I'm not quite sure what you're after, but you can do something like:
    Code:
    if (qStr =~ /\S+/) {
        // Everything is OK
    }
    If you want more information than that you will have to be more explicit as to what you're trying to do. Are you after a particular regular expression for example?

    Comment

    Working...
    X