codegasmer
codegasmer

Reputation: 1468

Calling a javascript function in playframework view

This is my view

 @helper.form(action = routes.UserController.submit(), 'class -> "form-horizontal", 'onsubmit -> "return validate();") {
    <fieldset>
        <legend>Account information</legend>


        @inputText(signupForm("firstName"),
        '_label -> "First name:",
        'class -> "form-control",
        '_help -> "Please enter your first name.",
        '_error -> signupForm.globalError
        )



        @inputText(signupForm("lastName"),
        '_label -> "Last name:",
        'class -> "form-control",
        '_help -> "Please enter your last name.",
        '_error -> signupForm.globalError

        )


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


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

    </fieldset>

    <div class="actions">
        <input type="submit" class="btn btn-primary" value="Sign Up">
        <a href="@routes.ApplicationController.index" class="btn">Cancel</a>
    </div>
<script type="text/javascript" charset="utf-8">
    function validate()
    {
    fname=signupForm.firstName.value;
    lname=signupForm.lastName.value;
    mail=signupForm.email.value;
    pass=signupForm.password.value;

    if( (fname.length<=0 )||(lname.length<=0)||( mail.length<=0)||(pass.length<=0) )
    {
    alert('Please fill all the fields');
    return false;
    }



    }
</script>


}

I just wants to call a javascript function onsubmit on this form Is there any way to do that? or any alternate way to call javascript function in play? i have tried this link http://www.playframework.com/documentation/2.1.1/JavaGuide6
but unable to understand it. Any help would be appreciated

Upvotes: 2

Views: 1294

Answers (1)

biesior
biesior

Reputation: 55798

This Scala syntax just creates HTML code, there's nothing against using JS in templates

@helper.form(action = routes.UserController.submit(), 'class -> "form-horizontal" , 'onsubmit -> "return foo();") {
    ....
} 

<script type="text/javascript" charset="utf-8">
    function foo(){
        alert("bar");
        return false;
    }
</script>

Upvotes: 2

Related Questions