I am working on a Grails form item that just populates a list of FaqCategories from an FaqCategory domain class. All I want the select dropdown to do is display the list. So far, this sort of works, but I'm not sure how to generate the list or what to put in the Value section if anything.
Form item:
<div class="col-sm-9">
<g:select name="Category" from="${FaqCategory.list()}" required="" class="form-control" aria-labelledby="faqCategory-label" value=""/>
</div>
Domain class #1
class Faq {
String question
String answer
FaqCategory faqCategory
static constraints ={
question nullable:false, maxSize:1000
answer nullable:false, maxSize:1000
faqCategory nullable:false
}
}
Domain class #2
class FaqCategory {
String categoryType
String toString(){
"$categoryType"
}
static constraints = {
categoryType nullable:true, maxSize:100, unique:true
}
}
Controller snippet
#Transactional(readOnly = true)
def create() {
respond new Faq(params)
}
Controller code :
yourAction(int id){
//do some
def faqInstance = Faq.get(id)
render[view:"form",model:[faqInstance:faqInstance]]
}
View code :
<g:select name="faqCategory"
from="${FaqCategory.list()}"
value="${faqInstance?.faqCategory?.categoryType}"
optionKey="id" />
Hope it helps..
You haven't provided enough information to show the context in which this select is being used so it is hard to say exactly what you need, but something like this should work:
<g:select name="faqCategory"
from="${FaqCategory.list()}"
value="${you only need this if you want to preselect a value}"
optionKey="id" />
See http://grails.org/doc/latest/ref/Tags/select.html for more details.
I hope that helps.
Related
Hi I am new in Grails and would like to find out how do I have filtered results of two fields in grails. Should i do it in view, or should i do it in domain class? How do i do it?
In the following example, I have 3 classes:
Country
State
Address
The field State should display only filtered results based on Country. Right now all the states in the State object is presented. The following screenshot depicts this problem.
All the states are listed
The following are my codes.
class Country {
static constraints = {
}
String countryName
static hasMany = [state : State ]
}
class State {
static constraints = {
}
String stateName
static belongsTo = [ country : Country ]
}
class Address {
static constraints = {
}
String line1
String line2
Country country
State state
}
Any help is appreciated. Thank you so much.
Are you using scaffolding?
If so Grails is going to render all of the appropriate form controls unless you exclude them e.g.
static constraints = {
country display:false
}
However I don't think you need to associate Country with Address because there's an association between Address and State which also has an association with a Country so the association between an Address and a country will be implicit after selecting a state.
EDIT 1
If you want to keep the current model you'll probably have to move away from scaffolding and implement the action and gsp yourself e.g.
controller:
def create() {
def states
if ( params.country ) {
states = State.findByCountry(params.country)
}
[countries: Country.all, states: states]
}
gsp:
<g:select class="form-control"
name="country"
from="${countries}"
optionKey="${it}"
value="${params.country}"
required="required"
onchange="submit()"
noSelection="['':'Select country']" />
<g:if test="${params.country}">
<g:select class="form-control"
name="state"
from="${states}"
optionKey="${it}"
value="${params.state}"
required="required"
noSelection="['':'Select state']" />
</g:if>
The above should be treated as pseudo code to give an idea of possible solution
I have the following POJO/POGO:
class Person {
String firstName
String lastName
int age
// ... lots of other fields
}
And a Grails 2.3.6 controller:
class PeopleController {
List<Person> people = new ArrayList<Person>()
def populatePeople() {
// Add lots of people to the 'people' list.
}
def doSomething() {
populatePeople()
render(
view: "people",
model:[
people: people,
]
)
}
}
And then in the GSP:
<div id="peopleSelector">
<g:select name="people" from="${people}" />
</div>
When I run my app I get the <select> element with com.me.myapp.domain.Person#398r4d99-looking values as <option>s. This is obviously Grails not deserializing my Person instances into pretty print form.
I want peoples' first and last names to appear as the select options. Hence, if one of the Person instances in the people list is:
Person smeeb = new Person(firstName: "Smeeb", lastNname: "McGuillocuty")
Then I would expect "Smeeb McGuillocuty" as a select option in the final HTML. How can I accomplish this?
Add the following method to your Person class:
#Override public String toString() {
"$firstName $lastName"
}
And, somewhat unrelated to the actual question, you may have to add an identifier to your option rows to uniquely identify the person. Assuming the Person class has an id property:
<g:select name="people" from="${people}" optionKey="id" />
so that you get the following HTML:
<select name="people" id="people">
<option value="123">Smeeb McGuillocuty</option>
:
Useful link to official doc: http://grails.org/doc/latest/ref/Tags/select.html:
"..The default behaviour is to call toString() on each element in the from attribute.."
If you can't/won't "sacrifice" toString() for rendering in HTML you can also tell the g:select how to render the options. Either by providing the name of a property in optionValue (e.g. optionValue="fullName" and then provide a String getFullName() method (watch out for transients, if you pass a GORM object)) or by providing it directly in the GSP:
<g:select name="person" optionKey="theId" optionValue='${{"$it.lastName, $it.firstName"}}' from="${people}" />
I am a beginner in grails and I have a basic question. I want to display a list of instances of a parent class in the child class form creation.
My Domain class are as follows. The Parent class is the Company.
class Company {
String name
static constraints = {
name(blank:false)
}
String toString(){name}
}
My Child Class is the Location of the company.
class Location {
String name
String address
static belongsTo= {companyLocation:Company}
static constraints = {
name(blank: false)
address blank:false
}
String toString(){"company:"+companyLocation+"Location:"+name}
}
Now in the _form template' of location view I have the code for thecompanyLocation dropdown`
<div class="fieldcontain ${hasErrors(bean: locationInstance, field: 'companyLocation', 'error')} required">
<label for="companyLocation">
<g:message code="location.companyLocation.label" default="companyLocation" />
<span class="required-indicator">*</span>
<g:select id="companyLocation" name="companyLocation.id" from="${first_project.Company.list()}" optionKey="id" required="" value="${locationInstance?.companyLocation?.id}" class="many-to-one"/>
</label>
</div>
When I go to the the create page I get the error:
Error 500: Internal Server Error
URI /first_project/location/create
Class groovy.lang.MissingPropertyException
Message No such property: companyLocation for class: first_project.Location
Why am I getting this error when I have a static variable companyLocation defined in the Location Domain class? Could some please let me know where I have gone wrong?
Thanks in advance.
This looks like a syntax issue,
static belongsTo= {companyLocation:Company}
should really be
static belongsTo= [companyLocation:Company]
There is one more OO method of doing this, instead of using has many and belongs to...
create another CompanyLocation Domain class.
Class CompanyLocation {
Company company
Location location
static constraints = {
company(blank:false, nullable:false)
location(blank:false, nullable:false)
}
public String toString() {
return "${company} ${location}"
}
}
I'm having difficulty auto-binding a one-to-many relationship in Grails without resorting to some hack in the controller. I understand that a one to many relationship in Grails is a set which is unordered and somehow affects binding.
When I save this form, sometimes the data saves correctly, and sometimes it does not. If an author has 3-4 books, it seems that it works less often.
In this example, I've tried to remove all non-relevant code to illustrate the issue.
Models:
class Author {
String name
static hasMany = [ books:Book ]
}
class Book {
String title
static belongsTo = [ author:Author ]
}
View:
<g:form method="post" class="form-horizontal">
<g:hiddenField name="id" value="${authorInstance?.id}" />
<g:hiddenField name="version" value="${authorInstance?.version}" />
<g:textField name='name' value='${authorInstance?.name}'/>
<g:each var="book" in="${authorInstance.books}" status="i">
<g:hiddenField name='book[${i}].id' value='${book.id}'/>
<g:textField name='book[${i}].title' value='${book.title}'/>
</g:each>
<g:actionSubmit action="update" value="Update" />
</g:form>
Controller:
def update(Long id, Long version) {
def author = Author.get(id)
// removed "Not Found" and "Version" validation for this example
author.properties = params
if (!author.save(flush: true)) {
render(view: "edit", model: [author: author])
return
}
flash.message = "Success"
redirect(action: "list"
}
How can I structure my model and view so I can leave the controller relatively untouched?
I've struggled with similar issues submitting one-to-many forms. I solved it in my app by converting the set to a bag.
So unless you specifically need books to be a set, try this:
class Author {
String name
Collection<Book> books
static hasMany = [ books:Book ]
}
I found that the easiest thing to do was force "Books" to be a List so it's ordered.
class Author {
List books <------- Added (by default this one-to-many relationship is a Set)
String name
static hasMany = [ books:Book ]
}
Then the view can remain the same and everything should work as expected.
I am having hard time getting hasErrors to work with indexed properties. For example
class Order {
String prop1
String prop2
static hasMany = [items: Item]
}
class Item {
String name
static constraints = {
name(blank:false)
}
}
Validation works properly and on item.name being blank I do get an error with
<g:renderErrors bean="${orderInstance}"/>
However, I am trying to have input box highlighted using hasErrors :
<g:each in="${orderIntsance.items}" status="i" var="item">
<span class="field ${hasErrors(bean: orderInstance, field: ????????? , 'errors')}">
<g:textField name="items[${i}].name" value="${item?.name}"/>
</span>
</g:each>
Not sure how to get to it with a field: property, any ideas?
Thanks
Found it, got to implement a custom validator per Grails Validation doc page (duh):
"In some situations (unusual situations), you might need to know how to transfer an error from a nested child object to a parent domain object. In some circumstances, if you validate the children objects before the parent object, then the errors on the children objects will get reset before the object is sent to the JSP." (http://www.grails.org/Validation)
static constraints = {
children( nullable:true, validator: {val, obj, errors ->
def errorFound = false;
val.each{ child ->
if(!child .validate()){
errorFound = true;
child .errors.allErrors.each{ error->
obj.errors.rejectValue('children', "parent.child.invalid",
[child, error.getField(), error.getRejectedValue()] as Object[],
"For source [${child}],
field [${error.getField()}] with value [${error.getRejectedValue()}] is invalid.")
}
}
}
if(errorFound) return false;
})
}
I had a similar requirement and tried the following way and it worked. Just wanted to share it
${hasErrors(bean: orderInstance, field: 'items['+ i +'].name', 'errors')