Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the year drop down with a textfield

Tags:

grails

I am creating a domain class named HOLIDAY in grails.I need to have a year dropdown in my Holiday year field. Hear is my domain:-

class Holiday extends CategoryMaster {

    String holidayName
    Date startDate
    int year
    Date dateCreated

    static constraints = {
        holidayName(blank:false, nullable:false)        
    }
}

I need to a year dropdown in year field, Should it come dynamically from a domain method that will discard the month & day and reflect only year in that dropdown adding +50 -50 to that?... How can I get it?

like image 418
Sanjib Karmakar Avatar asked Dec 28 '25 21:12

Sanjib Karmakar


1 Answers

Assuming you have an instance of Holiday named holiday in scope, you can create the dropdown using something like:

<g:select name="year" from="${holiday.year - 50..holiday.year + 50}"
    value="${holiday.year}"/>

This will set the current value of the dropdown to holiday.year and offer a range 50 years either side of this.

As an aside, if year is the year of startDate, there is no need to store this as a separate field, as it can easily be computed via:

startDate[Calendar.YEAR]
like image 118
Dónal Avatar answered Jan 01 '26 08:01

Dónal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!