Grails form validation on client - grails

I have the following domain class and gsp and can't seemed to work for validation on client gsp side.
The domain class:
class User {
String username
String password
String emailAddress
static hasMany = [memberships: Membership]
}
The form gsp:
<div class="error-details">
<g:hasErrors bean="${user}">
<ul>
<g:eachError var="err" bean="${user}">
<li>${err}</li>
</g:eachError>
</ul>
</g:hasErrors>
</div>
<form action="${raw(createLink(controller:'purchase', action:
'createSubscription'))}" method="POST">
<input type="text" name="username">
<input type="text" name="password">
<input type="text" name="emailAddress">
</form>
Is there anything I've missed out?

You should use the built in Taglibs for rendering fields. Don't use standard HTML. This way you allow Grails to determine the constraints based on your domain class.
You didn't specify the version of grails you're running:
The latest version 3.3.x uses the fields plugin, please see https://grails-fields-plugin.github.io/grails-fields/latest/ref/Tags/field.html
<f:field bean="user" property="username"/>
<f:field bean="user" property="password" />
<f:field bean="user" property="emailAddress"/>
or just use this
<f:all bean="user"/>
Which will render all the attributes of user.
Make sure you update your domain and include the following constraint
static constraints = {
password password: true
}
Additional constraints are possible. Please see https://docs.grails.org/latest/ref/Constraints/Usage.html
In older version of grails, please see https://grails.github.io/grails2-doc/2.4.3/ref/Tags/field.html
<g:field type="text" name="username" value="${userInstance?.username}"/>
<g:field type="password" name="password" value="${userInstance?.password}"/>
<g:field type="text" name="username" value="${userInstance?.username}"/>

Related

React Integration: How to connect react components with rails backend?

Apologies if there is a simple answer to this, but I'm very new to react.
I'm building an application in react and I'm trying to integrate two basic forms (register/login) with a rails backend. I don't really need to authenticate the user at this point, I just need it to remember the user and create a session.
Is there an easy way to do this that avoids using something like Devise? If somebody could point me in the right direction, it would be greatly appreciated.
As I mentioned the forms will be very basic. Something like this:
<form>
<label for='email'>Email Address:</label>
<input type="text" name="email" />
<label for='password'>Password:</label>
<input type="text" name="email" />
<button> Login</button>
</form>
and
<form>
<label for='email'>First Name:</label>
<input type="text" name="first-name" />
<label for='password'>Last Name:</label>
<input type="text" name="last-name" />
<label for='email'>Email Address:</label>
<input type="text" name="email" />
<label for='password'>Password:</label>
<input type="text" name="password" />
<label for='password'>Confirm Password:</label>
<input type="text" name="password" />
<button> Register</button>
</form>
Thanks in advance.
You need to create Rest Api on your Rails backend. Once you have your API endpoints you can make calls from react on those endpoints using fetch or axios whichever u like.
First, your rails backend need to be a rest API, if already is, you need to make the request of your react app, I recommend you to use Axios

Grails - %252F in the url

I am using spring security plugin for login in application. The spring security has default auth.gsp page for login. I have created my own auth.gsp page to add additional thing
auth.gsp
<g:form action='${postUrl.encodeAsURL()}' method='POST' id='loginForm' autocomplete='off' class="login-form">
<g:textField class="field" name="username" value="${params.userName}"/>
<g:passwordField class="field" name="password"/>
<button>login</button>
<p class="message">Forgot Password?</p>
<p class="message"><a id="next" name="next" href="#">Have Invitation Code?</a></p>
</g:form>
The problem is that ${postUrl} contains %252F in the url. Here is the complete url: http://localhost:8080/login/%252Flogin%252Fauthenticate/loginForm. I have tried every possible answer given on stackoverflow but nothing worked.
Try using
<g:form url='${postUrl}' method='POST' id='loginForm' autocomplete='off' class="login-form">
<g:textField class="field" name="username" value="${params.userName}"/>
<g:passwordField class="field" name="password"/>
<button>login</button>
<p class="message">Forgot Password?</p>
<p class="message"><a id="next" name="next" href="#">Have Invitation Code?</a></p>
</g:form>
When setting complete urls, you use 'url' attribute. If you use "action", then you must set a action name.
Also don't use encodeAsURL(). encodeAsURL means, you want to escape special characters like '/','?' which are replaced with '%..' entities.

Change the width of a SINGLE JQuery Mobile Input field?

The other two answers on this question propose overriding
div.ui-input-text {}
However, I have multiple input fields on this page and also within my project. So those answers won't work as they would effect everything on my page or project.
How can I modify a single text input?
A class designation in the input tag doesn't work. If I encapsulate the input with a div tag it still doesn't work. I'm trying to put an icon at the end but it seems all Jquery mobile text input takes the entire screen.
<input class="address" type="text" name="address" id="basic"
placeholder="Street Address, City, State" />
<input type="button" value="FindMe" data-icon="eye" data-iconpos="notext">
CSS, No Effect!
.address {
width: 200px !important;
}
Now, I could still switch to Bootstrap if that's the better way to go on this project. It seems to have .col-xs-* classes that solve this problem.
Thanks in advance.
Instead of directly setting the class on the input,jQM provides a data-attribute for inputs called data-wrapper-class (api doc: http://api.jquerymobile.com/textinput/#option-wrapperClass). This allows you to apply a class directly to the outermost wrapping DIV that jQM adds when enhancing the textbox.
<input data-wrapper-class="address" type="text" name="address" id="basic"
placeholder="Street Address, City, State" />
Working DEMO
It is maybe bit late to answer this, but maybe for others looking for the same thing (I was :-)
You can put your address in a div:
<div class="myContainer">
<label id="lbAddress">Provide the address</label>
<input class="address" type="text" name="address" id="address" />
<input class="address" type="text" name="Street" id="Street" />
<input class="address" type="text" name="City" id="City" />
<input type="button" value="FindMe" data-icon="eye" data-iconpos="notext">
</div>
<div><input class="other" type="text" name="other" id="other"/></div>
Then select your container and just the input inside that container in the CSS:
.myContainer > .ui-input-text
{
width:200px;
}
demo: https://jsfiddle.net/petitbarzun/cfazq5k5/
Reading your comments on the answer from ezanker, if you want all the inputs to appear on one line, there needs to be a container with ui-field-contain like this (the label should be there too):
<div class="ui-field-contain">
<label id="lbAddress" style="display:none"></label>
<input class="address" type="text" name="address" id="address" />
<input class="address" type="text" name="Street" id="Street" />
<input class="address" type="text" name="City" id="City" />
<input type="button" value="FindMe" data-icon="eye" data-iconpos="notext">
</div>
<div><input class="other" type="text" name="other" id="other"/></div>
The CSS then looks like this:
.ui-field-contain > #lbAddress~[class*=ui-input-text]
{
width:219px;
}
demo http://jsfiddle.net/petitbarzun/cfazq5k5/1/

MVC Razor String Concat

I am using Razor to generate a form. I want to create HTML elements based on some value from it's model property.
for example, if a model contains Id property, and I want to generate html tags as follows
<input type="hidden" name="1_chk" />
<input type="hidden" name="2_chk" />
<input type="hidden" name="3_chk" />
So I used the following syntax, and it failed. Can anyone help me out with this?
<input type="checkbox" name="#Id_chk" />
Thanks
I think this should work for you:
<input type="checkbox" name="#(Id)_chk" />
another option:
<input type="checkbox" name="#(Id + "_chk")" />

Lists as mvc controller method arguments?

I have 2 tables containing checkboxes in a MVC form. The names of the checkboxes are currently more or less random.
Can I name the items in any smart way so that I can retrieve them as two named lists as controller method arguments? Preferably if I could do with prefixing the names.
<div>
<input type="checkbox" name="xyz" />
<input type="checkbox" name="foo" />
<input type="checkbox" name="123" />
</div>
<div>
<input type="checkbox" name="bar" />
<input type="checkbox" name="456" />
<input type="checkbox" name="baz" />
</div>
Can I somehow get it as arguments, similar to this?
public ActionResult DoThis(BlablahViewModel model, string[] firstList, string[] secondList)
{
Currently I just check for their existence roughly like this:
Request.Form["xyz"].Contains("t")
Thanks!
You'll need to use the List Model Binding features of MVC:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
For a good guide of how this can all work together:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

Resources