Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client-side form validation in play framework

I want to do this type of validation in my form enter image description here

The field should be red with the message when user submits an empty form. My create.scala.html

@(signupForm: Form[models.Member])

@import helper._
@import helper.twitterBootstrap._

@main(Html("Create User")) {

    @form(action = routes.UserController.submit(), 'id -> "userCreationForm", 'class -> "form-horizontal", 'role->"form") {
        <fieldset>
           <legend><h1> Account Information</h1></legend>

<div class="form-group">
            @inputText(signupForm("firstName"),
            '_label -> "First name:",
            'class -> "form-control",
            '_help -> "Please enter your first name.")
            </div>

<div class="form-group">
            @inputText(signupForm("lastName"),
            '_label -> "Last name:",
            'class -> "form-control",
            '_help -> "Please enter your last name.")
            </div>

<div class="form-group">
            @inputText(signupForm("email"),
                        '_label -> "Email Address:",
                        'class -> "form-control",
                        '_help -> "Enter a valid email address.",
                        '_error -> signupForm.globalError)
                        </div>

<div class="form-group">
            @inputPassword(signupForm("password"),
                            '_label -> "Password:",
                            'class -> "form-control",
                            '_help -> "A password must be at least 6 characters.")
                            </div>
        </fieldset>

        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Sign Up">
            <a href="@routes.ApplicationController.index" class="btn">Cancel</a>
        </div>
    }

}

and added <script src="@routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"></script> to my main.scala.html

but its not giving the desired output means not showing the message and fields with red when user submits an empty form Thanks in advance.

like image 782
akku Avatar asked Dec 18 '25 22:12

akku


1 Answers

Play has no built-in solution for front-end form validation, you need to use some lib i.e. jQuery Validation Plugin

The validation you showed us from sample forms app is a backend one - it has constraints set in the model(s) and/or form(s) like in User.java model.

More about constraints in the Forms documentation

like image 78
biesior Avatar answered Dec 21 '25 13:12

biesior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!