groovy/grails - domain object and tooltips - grails

Given a grails domain object with properties like
Integer displayOrder
Boolean visible
... what would be the best way to associate meta information about these fields for display on tooltips in edit view?
Thanks a bunch.

I would probably just add a message to message.properties with the content:
// messages.properties
myDomain.myField.tooltip = Enter an awesome field value for this field
And then render it appropriately in the markup alongside the field, or wherever:
<g:message code="myDomain.myField.tooltip"/>

Related

MVC html string field 2 way binding

I have a controller that have a field HtmlContents with data like <input type=text>
I know how to render that text as html using:
#Html.Raw(Model.HtmlContent)
What I need now is that when the user type some thing and the form submit
the value of Model.HtmlContentupdate so I need some thing like #html.textboxfor(a=>a.HtmlContent) that return the updated value that what I mean by 2 way binding.
Thank you

Grails - Editable fields when Creating and Non-editable when Editing

I'm using Grails 2.3.x
Imagine the domain class: Book (String Author, String Title)
When I'm creating a book I need the Author field and the Title field editable.
When I'm editing a book I need the Author field NON-editable and the Title field editable
I know that there is a Domain Constraint (editable: false), but that doesn't work with the second case. It only adds this tag to the readonly="readonly"
I also tried this: How to conditionally disable a form input field
But it seems that: "${mode == 'edit'}" is always false.
Thank you very much
That link you provided assumes that a variable flag called mode will be passed into the view to determine whether you are editing or not. To do this, you will need to pass mode into the view from your controller like this:
def edit(Long id) {
[bookInstance: new Book(), mode: 'edit']
}
That will force the ${mode == 'edit'} portion to be true.

Is there a way I can simply use a data annotation attribute to add a JavaScript attribute to a form field?

I would like to add a data-other-for attribute to a text input, to link it to a select, so that it can be used to capture a value not present in the select when the user selects 'Other' in the select. The attribute's code will determine which value or description is in fact 'Other', and if so, enable the text input and maybe make it mandatory.
It seems like the only way to do this is by creating a new helper, because going via a ValidationAttribute I can only add preset validation HTML attributes to my text input. Or go large and write a whole new metadata provider.
You could try to implement a custom ModelBinder.
Say, in the select you would have:
new SelectListItem(Text = "Other", Value="bind:propertyName", Selected = False);
Then in the overriden BindModel, you simply look for bind: in the model properties and when found, copy your value from there.
After this, you should be able to add normal validation attributes to your select list.

How to display field based on another one

I have something like this:
Class person {
string name
string status
boolean working
boolean vacation
}
static constraints = {
name()
status(inList: ["Active","Inactive"])
}
What I need is to show the working and vacation fields in the create and edit views, only if Active is selected in status.
I searched and read alot but can't find a way, maybe I'm missing something since I'm new to grails. Any help is appreciated.
Thank you
This can not easily be done with Dynamic scaffolding. You will need to edit the generated views to add the logic in. See the GSP tag refference for if at
http://grails.org/doc/latest/ref/Tags/if.html
In your case something like
<g:if test="$person.active ==true">
Insert GSP code to edit data here.
</g:if>

In Grails GSP, what is the equivalent to Spring MVC's <input name="entity.list[0].field">?

I'm coming from a Spring MVC background on a new Grails app. I have an object that contains a list of dependent objects. On the create and edit screen, I want to edit that object and its list of objects at the same time. In Spring MVC, you could use special names to bind the form fields to items in a list. Example:
Entity { String name, List items }
<form:input name="entity.items[0].value" value="${entity.items[0].value}"/>
I've tried similar variations in my GSP create and edit forms, but no luck.
I haven't used this with tag (is it a Java taglib?), but what you are doing is along the right path. I don't think you need the entity in there, the name should be just "items[0].value"
Here is some code I have that does what you need (using HTML input tag):
<input type="text" name="subItems[0].date"/>

Resources