sam
sam

Reputation: 31

required field validation in grails

I am a newbie to Grails and would like to know how I can set a required field validation on Grails gsp or controller. For Example: if the user doesn't enter its username, then I should prompt a message saying

Username is required.

Where do I start?

Upvotes: 3

Views: 7348

Answers (3)

Michal_Szulc
Michal_Szulc

Reputation: 4177

Add taglib for

                    <fs:localisedLink class="Button transp"  elementId="voucherLink" mapping="voucherOffer">Gift Voucher</fs:localisedLink>

using exisitng logic for footer link

Upvotes: 0

clever_bassi
clever_bassi

Reputation: 2480

You could do it in your GSP. For example:

<g:textField name="username" required="true" value="${user?.login}"></g:textField>

Here, required=true ensures that you enter something in the field before submitting the form.

Upvotes: 3

user439828
user439828

Reputation: 29

Lookup Command objects: http://www.grails.org/Command+objects+and+Form+Validation

class MyController {

    def myAction = { MyCommand cmd ->
        if (cmd.hasErrors()) {
            // do fail things
        }
        else {
            // do success things
        }
    }

}

class MyCommand {
    String username

    static constraints = {
        username(nullable:false, blank:false, minSize:4)
    }
}

Upvotes: 2

Related Questions