Is it possible to set a form variable to an array in gsp? - grails

This is my current code:
<g:form method="post" >
<g:hiddenField name="id" value="${personInstance?.id}" />
<g:hiddenField name="version" value="${personInstance?.version}" />
<fieldset class="form">
<g:render template="form"/>
</fieldset>
<fieldset class="buttons">
<g:actionSubmit class="save" action="update" value="update', default: 'Update')}" />
<g:actionSubmit class="delete" action="delete" value="update", default: 'Delete')}" />
</fieldset>
</g:form>
In my person instance, I have a list variable that I want to set as a hidden field as well, but I am unable to figure out how to do so as the hiddenField tag can only take in one piece of text. This is what I want to create:
<g:hiddenField name="classes" value=["English", "Math"] />
Is this possible a different way?

Related

How to add validation to contact form?

I am trying to add a contact form for users to fill and it will automatically email to the main account. I am unsure as to how I can add validation to the form so users are not able to click "Send Email" without filling in the fields.
I have currently tried adding Website: <input type="url" name="website" required>
app/views/contact_mailer/contact_email.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Contact Us Form</h1>
<p>
From: <%= #params['name'] %>
</p>
<p>
Email: <%= #params['email'] %>
</p>
<p>
Message: <%= #params['message'] %>
</p>
</body>
</html>
Contact form:
<h1>Any enquiries:</h1>
<center><form name="htmlform" method="get" action="/pages/send_form">
<table width="700px">
<form id="contact_form" action="#" method="POST" enctype="multipart/form-data">
<div class="row">
<label for="name">Your Name:</label><br />
<input id="name" class="input" name="name" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="email">Your Email:</label><br />
<input id="email" class="input" name="email" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="message">Your Message:</label><br />
<textarea id="message" class="input" name="message" rows="7" cols="30"></textarea><br />
</div>
<input id="submit_button" type="submit" value="Send email" />
</form>
Currently, the email still sends to the account without any validation such as "You must complete this field"
Create a model for your contact message. Something like:
class ContactEmail
attr_accessor :name, :email, message
end
Then include ActiveModel::Model to get ActiveModel's validation and tell the model which fields to validate, like:
class ContactEmail
include ActiveModel::Model
attr_accessor :name, :email, message
validates :name, :email, message, presence: true
end
Finally, in your controller action, create a new ContactEmail with the form data and call the predicate method valid?. And use flash to add an error message.
new_mail = ContactEmail.new(params['name'], params['email'], params['message'])
if new_mail.valid?
...
else
flash.now[:error] = new_mail.errors.full_messages
end
Could you use the HTML form attributes? There is a required attribute so the user can't leave the field empty.
e.g.,
<input required id="email" class="input" name="email" type="email" value="" size="30" />
you can also use the type = "email" and it will ensure some of the proper formatting.
(https://www.w3schools.com/html/html_form_attributes.asp)
Looking at the code of your form i see several problems:
you have 2 <form> openings
i dont see any file inputs so you don't need the enctype="multipart/form-data"
here's the code revised (i've removed the first form element, i've adjusted the action of the second form, i've added required attributes to the inputs and textarea and i've updated the email input's type to "email" to make sure only valid emails are entered). Adding required should force modern browsers to prevent form submission if the inputs are not filled in properly.
<h1>Any enquiries:</h1>
<center>
<table width="700px">
<form id="contact_form" action="/pages/send_form" method="POST">
<div class="row">
<label for="name">Your Name:</label><br />
<input id="name" class="input" name="name" type="text" value="" size="30" required /><br />
</div>
<div class="row">
<label for="email">Your Email:</label><br />
<input id="email" class="input" name="email" type="email" value="" size="30" required /><br />
</div>
<div class="row">
<label for="message">Your Message:</label><br />
<textarea id="message" class="input" name="message" rows="7" cols="30" required></textarea><br />
</div>
<input id="submit_button" type="submit" value="Send email" />
</form>

Update or change user data form in Grails

I must write an update or change user data form in Grails. This is my form:
<g:form class="form-signin" controller="VisitorSpace">
<label for="login" class="sr-only">Login</label>
<g:textField id="login" class="form-control" name="login" placeholder="login" required="" autofocus=""/>
<label for="firstName" class="sr-only">Your name</label>
<g:textField id="firstName" class="form-control" name="firstName" placeholder="Your name" required="" type="text"/>
<label for="lastName" class="sr-only">Your lastname</label>
<g:textField id="lastName" class="form-control" name="lastName" placeholder="Your lastname" required="" type="text"/>
<label for="inputEmail" class="sr-only">e-mail</label>
<g:textField id="inputEmail" class="form-control" name="email" placeholder="e-mail" required="" autofocus="" data-translatable-string="Email address" type="email"/>
<label for="inputPassword" class="sr-only">Password</label>
<g:passwordField id="inputPassword" class="form-control" name="password" placeholder="Password" required="" data-translatable-string="Password"/>
<label for="confirmPassword" class="sr-only">Confirm password</label>
<g:passwordField id="confirmPassword" class="form-control" name="controlPassword" placeholder="Confirm password" required="" data-translatable-string="Password"/>
<g:actionSubmit value="Commit change" action="updateUserData" class="btn btn-lg btn-primary btn-block">Commit change</g:actionSubmit>
</g:form>
The old data must be shown in the fields, so it must be sent from the database to the moment when the view-form will be loaded. I know, what may be possible to call required controller-method to do this and such method must be called before or while view-form will be loading. But how to do this?
Add the value attribute to your input field elements with the current data you want from the controller. Is that what you are asking for?
<g:textField id="lastName" value="${objectInstance.lastName}" ... />

let grails generate without render template

is it possible that the grails generate-all command is generating the create/edit.gsp views in a way that all fields are genereated seperatly and not just using
Like this:
<label for="myTicketNo">Ticket no.:</label>
<input type="text" id="myTicketNo" name="myTicketNo" value="${fieldValue(bean:myInstance,field:'myTicketNo')}"/>
<label for="description">Description:</label>
<textarea id="description" name="description" cols="180" rows="10">
${fieldValue(bean:myInstance,field:'description')}
</textarea>
<span class="button"><input class="save" type="submit" value="Create" /></span>
and not like:
<g:form url="[resource:userInstance, action:'save']" >
<fieldset class="form">
<g:render template="form"/>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>
It would make it easier for me to adjust fields seperatly and not writing that manually for each and every field I want the user to be able to change.
I'm on current grails .2.4.4.
Thanks in advance.
Use grails install-templates command so you will be able to modify edit.gsp to your liking.
http://grails.org/doc/latest/ref/Command%20Line/install-templates.html
After modifying the installed tempĺates all generate code will follow its rules.

Struts2: Hidden field and anchor tag <s:a> values In Action Class

I am working on a struts2 application. In my jsp page I have 2-3 anchor tag and 2-3 hidden fields like
<s:a href="#">File 1</s:a>
<s:a href="#">File 2</s:a>
<s:a href="#">File 3</s:a>
and
<s:hidden name=" hidden1" />
<s:hidden name=" hidden2" />
<s:hidden name=" hidden3" />
Now please let me know, In my action class how can I get value of all the hidden fields and anchor tag which was clicked.
I had tried following
<s:a href="#" action=”someAction”>File 1</s:a>
Its working but didn’t transfer value of hidden fileds.
Also
<s:a href="#" name=”File1” onclick=”submit”>File 1</s:a>
But no gain.
Looking for your reply.
Like Boris said, you need to put the hidden fields inside a form, and submit that form, OR you can add them as URL parameters to your links. The best method is probably using a form with POST so the hidden fields aren't on your browser's location bar.
Here's an example
<s:form id="myform" name="myform" action="someAction" method="POST">
<s:hidden name=" hidden1" value="first value"/>
<s:hidden name=" hidden2" value="second value"/>
<s:hidden name=" hidden3" value="third value"/>
Submit with link
<s:submit value="%{'Submit with button'}" />
</s:form>
Since this really has nothing to do with struts2, here's an example with pure HTML:
<form id="myform" name="myform" action="someAction.action" method="POST">
<input type="hidden" name=" hidden1" value="first value"/>
<input type="hidden" name=" hidden2" value="second value"/>
<input type="hidden" name=" hidden3" value="third value"/>
Submit with a link
<br/>
<input type="submit" value="Submit with a button"/>
</form>

Basic Grails question: How does the following Grails code define personInstance?

This is the GSP code generated by Grails for the view of the edit action for a Person domain object which is part of my model, and also happens to be the primary class for authentication by the ACEGI security plug-in. I have snipped out a bunch of properties to keep it short. The file resides in the standard location, grails-app/views/person/edit.gsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
<g:set var="entityName" value="${message(code: 'person.label', default: 'Person')}" />
<title><g:message code="default.edit.label" args="[entityName]" /></title>
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLink(uri: '/')}">Home</a></span>
<span class="menuButton"><g:link class="list" action="list"><g:message code="default.list.label" args="[entityName]" /></g:link></span>
<span class="menuButton"><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></span>
</div>
<div class="body">
<h1><g:message code="default.edit.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<g:hasErrors bean="${personInstance}">
<div class="errors">
<g:renderErrors bean="${personInstance}" as="list" />
</div>
</g:hasErrors>
<g:form method="post" >
<g:hiddenField name="id" value="${personInstance?.id}" />
<g:hiddenField name="version" value="${personInstance?.version}" />
<div class="dialog">
<table>
<tbody>
<tr class="prop">
<td valign="top" class="name">
<label for="username"><g:message code="person.username.label" default="Username" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: personInstance, field: 'username', 'errors')}">
<g:textField name="username" value="${personInstance?.username}" />
</td>
</tr>
...SNIP...
a bunch of props
</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" /></span>
<span class="button"><g:actionSubmit class="delete" 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?')}');" /></span>
</div>
</g:form>
</div>
</body>
</html>
My question is, how does the field personInstance get set up and populated?
I suspect this is a basic question which belies a fundamental lack of understanding on my part about how Grails works, but I need to know nonetheless.
This stems from the desire to create my own composite pages which access a Person object and its associated data, which is the heart of my app. I was expecting to be able to create a new page alongside this one, let's call it map.gsp, and get at the personInstance in some magic way. I can't figure out how to do that in spite of trying the obvious, and I think I have a gap right at the centre of my understanding.
PersonInstance will be populated on the controller.
When you submit your form, the associated controller will receive a map containing the fields present on your form.
So, in your controller you'll find a command like
personInstance.properties = params
where params is a map containing the fields submited for the controller, which the keys are the names of the input elements you defined on your gsp file.

Resources