Grails - error 404 on form submit using g:actionSubmit - grails

I'm new on Grails and I'm having trouble to do a form submit.
Here is AnimaisController:
package jogoanimais
class AnimaisController {
def index() {
def animalsTreeObj = AnimaisTreeMap.list()
render(view: "show", model: [animalList: animalsTreeObj])
}
def addNode()
{
log.info "add node"
log.info params
}
}
Here is show.gsp
<g:form controller="animais" action="addNode">
<div>Pense em um animal</div>
<g:textField name="myField" value="${myValue}" />
<g:actionSubmit value="OK, próximo" />
<g:each in="${animalList}" var="row" status="i">
<h3> ${row.nodeDescription}, ${row.yesAnswerNode}</h3>
<br/>
</g:each>
</g:form>
After clicking at the submit button, the URL that is requested is "http://localhost:8080/jogoAnimais/animais/addNode" and I get a 404 error.
I've also tried adding "action" do g:actionSubmit but in this case, Grails requested a addNode.gsp.
Does anyone has any idea?

Here's the solution:
GSP:
Add "action" parameter of g:form and a input type "submit" as shown below:
<g:form controller="animais" action="addNode">
<div>Pense em um animal</div>
<div>
<label for="questionToUser">Questão:</label>
<g:textField name="questionToUser" maxlength="50"/>
</div>
<input type="submit" value="Submit">
<g:each in="${animalList}" var="row" status="i">
<h3> ${row.nodeDescription}, ${row.yesAnswerNode}</h3>
<br/>
</g:each>
</g:form>
CONTROLLER:
As mbaird has said, my "addNode" mehtod need to return something, as "render 'ok'"

Related

Grails - g:field reset and html reset won't clear form/params/session

Using Grails 3.0.9
Making a Clear Form/session button for the filter class. however nothing i do works.
The button is just stuck there, nothing happens
Any help will be appreciated
SupplyController
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
if (params.name)
session.name = params.name
else if (request.method != "POST")
params.name = session.name
else
session.name = null
def criteria = Supply.createCriteria()
def query = {
and{
if (params.name) {
like ("name", '%' + params.name + '%')
}
}
}
def results = criteria.list(params, query)
respond results, model:[supplyCount: results.getTotalCount()]
}
G:field type of code
<div class="filter">
<h3>Filter:</h3>
<g:form action="index" method="post" >
<label for='name'>Name:</label>
<input type="text" id="name" name="name" value="${session.name}"/><br/>
<span class="button">
<g:submitButton name="index" class="index" value="Apply Filter" /></span>
<g:field type="reset" name="myReset" value="Reset" />
</g:form>
</div>
HTML tag type
<div class="filter">
<h3>Filter:</h3>
<g:form action="index" method="post" >
<label for='name'>Name:</label>
<input type="text" id="name" name="name" value="${session.name}"/><br/>
<span class="button">
<g:submitButton name="index" class="index" value="Apply Filter" /></span>
<input type='reset' value='Reset' />
</g:form>
</div>
An HTML form reset button for a form rests the form to the initial state. In this case back to the values it had when the form loaded. A HTML form reset button does not CLEAR the form or CLEAR the session.
wondering why you are using session for where typicalls form params should be used? In order to clear HTML session you would need to trigger a call back to the originating controller just like you would to post but when it receives via this call. It does a session.invalidate() and then renders same view again. You should try to stick with params for forms. The fact that you have the session information already really no need to post it via a form. Your controller receiving the form would be aware of that same session value.
You could easily create a jquery functionality that you do with a button that triggers and a resetform. jQuery/Javascript function to clear all the fields of a form
Or if that does not work you could try for example in your case:
<g:form ..>
..
<button onClick="clearName();" name="clickme">
</g:form>
<g:javascript>
function clearName() {
$('#name').val();
}
</g:javascript>

How to prevent data input for being erase in Grails with reCaptcha

I have gsp with two textfield for firstname-lastname and reCaptcha. What I want is for every wrong captcha code the user's input for firstname and last name won't be erased.
snippet for controller:
***captcha_code****
if (result) {
def person = new Person(params)
person.save()
render "Success!"
} else {
flash.message = message(code: 'forgotPassword.captcha.wrong')
redirect(controller:'person', action:'form')
}
snipper for form.gsp
***captcha_code_here***
<g:form controller="person" action="save">
<label>First Name: </label>
<g:textField name="firstName"/><br/>
<label>Last Name: </label>
<g:textField name="lastName"/><br/>
<g:if test="${flash.message}">
<div class="message" role="status" style="font-size: medium;color: green;">${flash.message}</div>
</g:if>
***captcha_code_here***
<g:actionSubmit value="Save"/>
To repopulate the fields you can use the same flash scope you're using for the message. On error, add the first and last name to the flash scope, and then in your GSP use those values when they are available:
PersonController
class PersonController {
def save() {
...
if(/* recaptcha failed */) {
flash.firstName = params.firstName
flash.lastName = params.lastName
}
...
}
}
GSP
<label>First Name: </label>
<g:textField name="firstName" value="${flash.firstName ?: ''}"/><br/>
<label>Last Name: </label>
<g:textField name="lastName" value="${flash.lastName ?: ''}"/><br/>
In Controller Action, send back fields that you want to be repopulated.

grails error message in an independent gsp view

In my grails project I've built up a new view in which user can perform a search of entities.
I've created the gsp adding the method search() in controller and automatically creating the gsp as described here
In this gsp there is only one input field and a g:actionSubmit button. If I fill form with correct data everything works well, but if data does not have any correspondance I would see an error message in the view like the validation error messages with popups...but I don't know how to show it, because I'm not using any bean with this gsp.
In addition, after an error, I would render the same view, but with render(view: "search", model: [patientInstance: patientInstance]) the view is the same, but path is /index and not /search...
How can I show an error message? How can I have the right path?
here is the search()
def search()
{
def patientInstance = new Patient()
if(params.patient_textField == "" || params.patient_textField == " " || params.patient_id =="")
{
//here I would like to show message
//the redirect works correctly
redirect(controller: "patient", action: "search")
}
else {
def patientToShow = Patient.findById(params.patient_id)
redirect(controller: "patient", action: "show", params: [id: patientToShow?.id])
}
}
here is the snippet of gsp
<g:form>
<div id="patientDiv">
<label for="patient">
<g:message code="event.patient.label" default="Patient" />
</label>
<input style=" margin: 0px 10px 10px 0px;" type="text" name="patient_textField" id="patient_textField" value="" placeholder="${g.message(code: 'patient.choose', default: 'Insert Patient...')}" />
<input type="hidden" id="patient_id" name="patient_id" value="" />
<g:actionSubmit class="search" value="${g.message(code: 'default.search.label', default: 'Search Patient')}" action="search" ></g:actionSubmit>
</div>
</g:form>
EDIT:
solved problem of path changing render with redirect(controller: "patient", action: "search")
In the error portion of your code you can do flash.error = "Your error message here"
And in the gsp do something like:
<g:if test="${flash.error}">
<div class="alert alert-info">
${flash.message}
</div>
</g:if>
There is already a flash bean in scope. http://grails.org/doc/latest/ref/Controllers/flash.html

How to use Grails Searchable plugins in more than 2 domains

I am completely new in Grails, start learning grails from past couple of days. I am trying to add search feature by using searchable plugin in my demo grails application. I successfully added searchable plugins on user search where user can search other users and follow them. I am doing like this ..
grails install-plugin searchable
Domain Person.groovy --
package org.grails.twitter
class Person {
transient springSecurityService
String realName
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static hasMany = [followed:Person, status:Status]
static searchable = [only: 'realName']
static constraints = {
username blank: false, unique: true
password blank: false
}
static mapping = {
password column: '`password`'
}
Set<Authority> getAuthorities() {
PersonAuthority.findAllByPerson(this).collect { it.authority } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
view/searchable/index.gsp ---
<html>
<head>
<meta name="layout" content="main" />
<title>What Are You Doing?</title>
<g:javascript library="jquery" plugin="jquery" />
</head>
<body>
<h1>Search For People To Follow</h1>
<div class="searchForm">
<g:form controller="searchable">
<g:textField name="q" value=""/>
</g:form>
</div>
<h1>What Are You Doing?</h1>
<div class="updateStatusForm">
<g:formRemote onSuccess="document.getElementById('messageArea').value='';" url="[action: 'updateStatus']" update="messageLists" name="updateStatusForm">
<g:textArea name="message" value="" id="messageArea" /><br/>
<g:submitButton name="Update Status" />
</g:formRemote>
</div>
<div id="messageLists">
<g:render template="messages" collection="${messages}" var="message"/>
</div>
</body>
</html>
It works fine. Now My problem starts. Now I want to add this searchable in my Post domain also where user can search post items. I am doing like this ...
Domain Post.groovy --
package groovypublish
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
static searchable = [only: 'title']
static constraints = {
title(nullable:false, blank:false, length:1..50)
teaser(length:0..100)
content(nullable:false, blank:false)
lastUpdated(nullable:true)
published(nullable:false)
}
}
and here is form view
view/post/list.gsp --
------ some code -----
<g:form controller="searchable" class="navbar-search pull-left">
<g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
</g:form>
------ some code ------
Now when I try to search post by post title then it showing error. It overrides searchable action. How to solve this problem ?
You can implement your own search method using searchable, call a controller function from your search form and perform search in that:
Let say you have two search forms:
<g:form controller="postsController" action="postAction" class="navbar-search pull-left">
<g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
</g:form>
and
<g:form controller="searchable">
<g:textField name="q" value=""/>
</g:form>
then in the PostCOntroller you can have postAction method to perform search:
def postAction (Integer max) {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
params.sort = "id"
params.order = "desc"
if(params?.q){
def result = Post .search(params.q , order:"desc" )
return [searchResults: result.results, searchResultsCount: result.total, popup : params.popup?.toBoolean()]
}else{
[searchResults: Post .list(params), searchResultsCount: Post .count(), popup : params.popup?.toBoolean()]
}
and same you can have a different function for another search, if you use remote form then you need to have two different div's on the search page, and you can render the result page out there.
let say you have:
<g:formRemote name="postSearchForm" update="postSearchResultsDiv" url="[controller: 'post', action:'postAction' , params: [popup: false]]">
<label for="searchText">Search Post:</label>
<input name="q" type="text" id="searchText" class="input-medium search-query"/>
<input id="searchButton" type="submit" class="btn-info" value="Search"/>
</g:formRemote>
<div id="postSearchResultsDiv">--Your search result for the form will display here--</div>
This remote form will call postAction method in your controller, you can have postAction.gsp page on the controller's view folder and print the result out there.
and on your search page, postSearchResultsDiv will have the search result(postAction GSP page output)
I solved my own problem...
I have done like this ..
PostController ---
import org.compass.core.engine.SearchEngineQueryParseException
class PostController
{
def searchableService
def searchpost = {
if (!params.q?.trim()) {
return [:]
}
try {
return [searchResult: searchableService.search(params.q, params)]
} catch (SearchEngineQueryParseException ex) {
return [parseException: true]
}
render(view:'searchpost')
}
.......
}
Search form ---
<g:form controller="post" action="searchpost" class="navbar-search pull-left">
<g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
</g:form>
searchpost.gsp //for showing result
<html>
<head>
<r:require modules="bootstrap"/>
<meta name="layout" content="main"/>
</head>
<body>
<g:render template="/layouts/header" />
<div class="well">
<g:each var="post" in="${searchResult?.results}">
<div>
<h2>${post.title}</h2>
<p>${post.teaser}</p>
<p>Last Updated: ${post.lastUpdated}</p>
<g:link controller="post" action="view" id="${post.id}" class="btn btn-success">
View this post
</g:link>
<g:if test="${post.author == currentLoggedInUser }">
<g:link controller="post" action="edit" id="${post.id}" class="btn btn-danger">
Edit this post
</g:link>
<g:actionSubmit action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" class="btn btn-inverse" />
</g:if>
<g:form>
<g:hiddenField name="id" value="${post?.id}" />
</g:form>
</div>
</g:each>
</div>
</body>
</html>
And it works :)

grails g:field error not showing

I have this setup, but the validation is not working
index.gsp:
<g:form name="loginForm" autocomplete="off" controller="company" action ="save">
<table >
<tr>
<td><g:field type="text" name="company" required="true" value="${c?.companyName}" /></td>
</tr>
</table>
Controller:
def index = {
def c =new Company()
//c=params
return [c:c]
}
def save ={}
You need to check for errors using hasErrors method in your form:
<div class="fieldcontain ${hasErrors(bean: yourBean, field: 'yourField', 'error')} required">
<label for="yourField">
<g:message code="yourBean.yourField.label" default="yourField" />
<span class="required-indicator">*</span>
</label>
<g:textField name="content" required="" value="${yourBean?.yourField}"/>
</div>
Grails hasErrors documenttaion.
And check in your controller (save action) if the validation has passed using:
if (!yourBean.save(flush: true)) {
render(view: "create", model: [yourBean: yourBean])
return
}

Resources