Search code examples
javagrailsscaffolding

How to hide a field in the scaffolding view but still enforce it when adding a new row in Grails?


I have a simple Grails app and I'd like to hide some columns in the general scaffolding view, prevent the user from editing them but still enforce specifying them when adding a new item. I looked at the possible constraints but it looks like none quite fit the bill:

  • column display: false hides the column from the view but hides it everywhere, new item adding view included
  • column editable: false does prevent the column from being edited in the edit view but you can't define it when adding a new item either (+ it's not hidden at all)

How should I tackle it, then? I'd like to still be able to take advantage of the dynamic scaffolding capabilities but seems like there's no way to specify the column to not show up in the general view and yet still enforce it when adding something new. Is there any way to specify which view to hide it from, instead of going all or nothing?


Solution

  • Let's say we have the following domain:

    class Thing {
        String userName
        String firstName
        Integer userId
    
        static constraints = {}
    }
    

    And we don't want the userName to be editable on the edit view, you create the following directory...

    \views\thing\edit\userName
    

    Then add a file named _widget.gsp to the directory above with the following contents...

    ${value}
    

    Which will show the text value only of the userName.

    As for the index view, I'm not 100% if you can hide the field easily. I know the fields plugin will render the first 7 fields in the table as determined by the order in the domain class definition. So, if you have > 7 fields you could give the one you want hidden at an order > 7, clearly there are massive limitations here, you may not even have 8 fields or you may want more than one field hidden...

    You can do as above for the list view but create a directory named \views\thing\index\userName then create a file named _displayWidget.gsp with nothing in it or maybe some text like 'Hidden', this will still render the column but show nothing as the value or whatever text you add to the file, again not great.