Grails dynamic scaffold with hasMany: is it a bug or am I misconfiguring? - grails

I'm a Grails noob and running into something that seems to be a bug, but it is entirely possible I'm not configuring everything correctly.
I've got two simple Domain Classes:
class Player {
String firstName
String lastName
static constraints = {
firstName(blank:false)
lastName(blank:false)
}
String toString() { lastName + ", " + firstName }
}
and
class Team {
String mascot;
static hasMany = [players:Player]
static constraints = {
mascot(blank:false)
}
}
I have controllers for both that do nothing beyond dynamic scaffold these two Domain Classes.
But even when I have a list of Players in my DB, I don't get a multi-select box for them when creating a new Team.
However, the multi-select shows up when I go to edit a Team
Is this a bug in the dynamic scaffolding for new items, do I misunderstand how this is supposed to work, or is there something else I need to declare here?
Any help is hugely appreciated! I've got screenshots that StackOverflow won't let me add because of my newness, but I'd be happy to show them another way if that'll help.

I finally figured this out and wanted to pass on what I did just in case someone else runs into it.
When I generated the views for Team, the form block in edit.gsp looks like this:
<input type="hidden" name="id" value="${teamInstance?.id}" />
<input type="hidden" name="version" value="${teamInstance?.version}" />
<div class="dialog">
<table>
<tbody>
<tr class="prop">
<td valign="top" class="name">
<label for="mascot">Mascot:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:teamInstance,field:'mascot','errors')}">
<input type="text" id="mascot" name="mascot" value="${fieldValue(bean:teamInstance,field:'mascot')}"/>
</td>
</tr>
<tr class="prop">
<td valign="top" class="name">
<label for="players">Players:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:teamInstance,field:'players','errors')}">
<g:select name="players"
from="${Player.list()}"
size="5" multiple="yes" optionKey="id"
value="${teamInstance?.players}" />
</td>
</tr>
</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><g:actionSubmit class="save" value="Update" /></span>
<span class="button"><g:actionSubmit class="delete" onclick="return confirm('Are you sure?');" value="Delete" /></span>
</div>
</g:form>
but the form block in create.gsp looks like this:
<g:form action="save" method="post" >
<div class="dialog">
<table>
<tbody>
<tr class="prop">
<td valign="top" class="name">
<label for="mascot">Mascot:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:teamInstance,field:'mascot','errors')}">
<input type="text" id="mascot" name="mascot" value="${fieldValue(bean:teamInstance,field:'mascot')}"/>
</td>
</tr>
</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><input class="save" type="submit" value="Create" /></span>
</div>
</g:form>
In other words, for this corner case, the default Create view omits the widget to properly display the multi-select list. When I did a copy and paste of the missing code, the dynamically scaffolded controller picked it up and persisted it as expected. So, it's definitely a bug in the view generation code.

Yes, the default scaffolding puts a parent selector in the child class' create/edit page.
I'm guessing it was just easier for them this way. It shouldn't be a multi-select though, just a pull-down single-select, as it's a One-to-Many.
As you've explained you wanted more of a Many-to-Many relationship, you might try adding:
static hasMany = [teams:Team]
to your Player class. I've found that Grails does better with bi-directional relationships. It's also useful to have when building search queries, and shouldn't require more than the one relationship table you'd already need.
If you're using Grails pre-v1.1, Many-to-Many relationships aren't directly supported, so even adding the static hasMany above won't be the complete solution, as you'll need to manage adding to the other list when you add to one direction. I haven't used v1.1 yet, so I can't talk about what is needed to specify the Many-to-Many in it.

I encountered the same problem using current version (v1.3.4) of Grails. Had to manually modify the create.gsp

Related

Dojo - Upload two files in one Form

I use Dojo and want to send two files and one String to a REST Service. The HTML for that is the following:
//...
<script>
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.CheckBox");
dojo.require("dojox.form.Uploader");
dojo.require("dojox.embed.Flash");
if(dojox.embed.Flash.available){
dojo.require("dojox.form.uploader.plugins.Flash");
}else{
dojo.require("dojox.form.uploader.plugins.IFrame");
}
</script>
//..
<form action="http://localhost:8080/service" enctype="multipart/form-data" method="POST" name="inputDataForm" id="inputDataForm">
<table border="1" cellpadding="1" cellspacing="1" height="88" width="925">
<tbody>
<tr>
<td>Dataset1</td>
<td><input name="train" type="file" value="Browse" data-dojo-type="dojox/form/Uploader"/></td>
</tr>
<tr>
<td>Dataset2</td>
<td><input name="test" type="file" value="Browse" data-dojo-type="dojox/form/Uploader"/></td>
</tr>
<tr>
<td>Number of Customers</td>
<td><input name="numberCustomers" size="40" type="text" data-dojo-type="dijit/form/TextBox" /></td>
</tr>
</tbody>
</table>
<p><input name="runButton" type="submit" value="Run" data-dojo-type="dijit/form/Button" id="submitButton" /></p>
</form>
However: If I press the submit button, I can see via Firebug that there are sent TWO POST-Requests. One with the first file and the "Number of Customers" field and one with the second file and the "Number of Customers" field. However, my REST Service wants to have both files and the "Number of Customers" fields in one POST Request. How can i do that? What am I doing wrong?
Thanks a lot in advance
Natan

Is there a way for me to strip away div classes and other CSS stuff when generating grails views?

Well, there goes my question. I really don't like the UI, and if there was a way to auto generate only the essential things like the fields but no divs and classes. I know about removing the css, but I really just want the raw html like what rails gives me. Anybody know how?
For example, grails generate-views myStuff generates code that looks like this:
<table>
<tbody>
<tr class="prop">
<td valign="top" class="name">
<label for="name">Name:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:feedback,field:'name','errors')}">
<input type="text" id="name" name="name" value="${fieldValue(bean:feedback,field:'name')}"/>
</td>
</tr>
<tr class="prop">
<td valign="top" class="name">
<label for="feedback">Feedback:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:feedback,field:'feedback','errors')}">
<input type="text" id="feedback" name="feedback" value="${fieldValue(bean:feedback,field:'feedback')}"/>
</td>
</tr>
</tbody>
</table>
When I'm only interested in getting this:
<input type="text" id="name" name="name" value="${fieldValue(bean:feedback,field:'name')}"/>
<input type="text" id="feedback" name="feedback" value="${fieldValue(bean:feedback,field:'feedback')}"/>
Well, I could always do stuff manually and not auto-generate, but, no it's not practical when the fields are too many. :(
Use the install-templates command to install the templates. Then you can edit/modify the templates to meet your needs.

reading from a file using the input type=file in grails

Hi I have the following code in my grails gsp
<form action="upload-script-url" method="post" enctype="multipart/form-data">
<table class="table"style="width: 75%">
<tr>
<td>
<span style="font-weight: bold; ">Select the Source File:</span>
<input size="75" type="file" id="payload" name="payload"/>
</td>
</tr>
<tr>
<td>
<input type="submit" class="red" id="Run">Run</button>
</td>
</tr>
</table>
</form>
I read the form parameters from: here
Are those right parameters in the html form?
Now how should I proceed to read the data from the selected file? do I have to use the apache commons fileupload api ?
Thanks
request.getFile("payload")
and you will get a CommonsMultipartFile
If you take some time to (again) actually look at the documentation you will see how to do it...

Grails mapping questionnaire data to a bean

This is my first development using Grails. I have a requirement to create a questionnaire. This is my GSP page in which the questions are listed, possible answers for each question is listed and depending upon the type of answer a checkbox or radio button is displayed.It works fine till here
<div class="body">
<h1><g:message code="default.edit.label" args="[entityName]" /></h1>
<g:if test="${message}">
<div class="message">${message}</div>
</g:if>
<g:form action="createDonation" >
<div class="dialog">
<table>
<tbody>
<g:each in="${questionList}" status="i" var="questionInstance">
<tr>
<td>${fieldValue(bean: questionInstance, field: "text")}</td>
</tr>
<tr>
<g:each in="${questionInstance?.answers?}" status="j" var="a">
<td >
<g:if test="${fieldValue(bean: a, field: 'ansType.name') == 'Multiple'}"><g:checkBox name="myGroup" value="${false}" /></g:if>
<g:if test="${fieldValue(bean: a, field: 'ansType.name') == 'Single'}"><g:radio name="myGroup" value="1"/></g:if>
</td>
<td >${fieldValue(bean: a, field: "text")}</td>
</g:each>
</tr>
</g:each>
</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><g:submitButton name="return" class="save" value="${message(code: 'default.button.backtodonorlogin.label', default: 'Back')}" /></span>
<span class="button"><g:submitButton name="submit" class="save" value="${message(code: 'default.button.saveandcontinue.label', default: 'Create')}" /></span>
</div>
</g:form>
</div>
</body>
Now i want to save the response of the user i.e what is the answer selected by each user for each question. For Multiple choice multiple answers can be selected. I am having a hard time to figure out how to create a model for that.
Help is requested.
Thanks
how about this: you create a QuestionResponse entity with the following properties:
question (linking to the question being answered)
response (answer given by user)
responder (responding user)
You can find out about the current user by using spring security for instance.
You can determine the question being responded from the hidden ID in your form.

How do I use Asp.net MVC to validate a list property has a minimum number of items (count=N)?

I have a view model that has a property that looks like this
Property SelectedGroups() as List(of string)
In the view I have something like this
<table>
<tr>
<th>Description</th>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="one" />
description one
</td>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="two" />
description two
</td>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="three" />
description three
</td>
</tr>
</table>
The table rows are added and removed with jquery. Is there a way to create a validation attribute on SelectedGroups property that will require a minimum number of items for the list? This can be done with javascript but I would like it to work with
<% Html.EnableClientValidation()%>
<%: Html.ValidationSummary(False)%>
You would have to write a custom validator. The built-in validators aren't that sophisticated.
ScottGu has written a good article on custom validators: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

Resources