java - Field values are coming to validate() function as NULL -
i have interesting problem. i'm trying implement basic log in mechanism on play framework. however, login form field values not coming validate() method of userlogin class properly, seen null in method, although succesfully taken in bindformrequest() method (i know detail outputting form object screen).
am missing point? here's code:
for application.java:
package controllers; import play.*; import play.mvc.*; import play.data.dynamicform; import play.data.form; import static play.data.form.form; import views.html.*; import models.*; public class application extends controller { public static result index() { return ok(index.render("your new application ready.")); } public static result login() { if ( session().containskey( "nickname") ) { return redirect( routes.application.mainmenu() ); } else { return ok( login.render( form( userlogin.class) ) ); } } public static result mainmenu() { return ok( mainmenu.render() ); } public static result authenticate() { form<userlogin> loginform = form( userlogin.class).bindfromrequest(); if ( loginform.haserrors() ) { return badrequest( login.render( loginform) ); } else { return ok( index.render( "s.a.") ); /* session().clear(); session( "nickname", loginform.get().nick); return redirect( routes.application.mainmenu() ); */ } } public static result registermenu() { if ( session().containskey( "nickname") ) { return redirect( routes.application.mainmenu() ); } else { return ok( registrationform.render( form( userregistration.class) ) ); } } public static result registeruser() { form<userregistration> regform = form( userregistration.class).bindfromrequest(); if ( regform.haserrors() ) { return badrequest( registrationform.render( regform) ); } else { return redirect( routes.application.login() ); } } public static class userlogin { public string nick; public string pwd; public string validate() { system.out.println( nick + "-" + pwd); return null; /* if ( user.auth( nick, pwd) ) { return null; } else { return "invalid user or password!"; } */ } } public static class userregistration { public string name; public string pwd1; public string pwd2; public string nickname; public string validate() { if ( !( pwd1.equals( pwd2) ) ) { return "the passwords must match!"; } else { if ( user.create( name, nickname, pwd1) != null ) { return null; } else { return "the given nick name in use, please choose different one."; } } } } }
for user.java: package models;
import java.util.*; import play.modules.mongodb.jackson.mongodb; import net.vz.mongodb.jackson.jacksondbcollection; import net.vz.mongodb.jackson.id; import net.vz.mongodb.jackson.objectid; import net.vz.mongodb.jackson.writeresult; import org.codehaus.jackson.annotate.jsonproperty; import com.mongodb.basicdbobject; import javax.persistence.*; public class user { @id @objectid public string id; public string name; public string nickname; public string pwd; private static jacksondbcollection<user, string> collection = mongodb.getcollection( "users", user.class, string.class); public user() { } public user( string name, string nickname, string pwd) { this.name = name; this.nickname = nickname; this.pwd = pwd; } public static list<user> all() { return user.collection.find().toarray(); } public static string create( user user) { basicdbobject query = new basicdbobject( "nickname", user.nickname); if ( user.collection.find( query).toarray().size() == 0 ) { writeresult<user, string> insertion = user.collection.insert( user); return insertion.getsavedid(); } else { return null; } } public static string create( string name, string nickname, string pwd) { basicdbobject query = new basicdbobject( "nickname", nickname); /* system.out.println( "size = " + user.collection.find( query).toarray() );*/ if ( user.collection.find( query).toarray().size() == 0 ) { user user = new user( name, nickname, pwd); writeresult<user, string> insertion = user.collection.insert( user); return insertion.getsavedid(); } else { return null; } } public static void delete( string id) { user user = user.collection.findonebyid( id); if ( user != null) { user.collection.remove( user); } } public static boolean auth( string nickname, string pwd) { //system.out.println( "nickname = " + nickname + ", password = " + pwd); basicdbobject query = new basicdbobject( "nickname", nickname).append( "pwd", pwd); //system.out.println( "size = " + user.collection.find( query).toarray().size() ); //system.out.println( user.collection.find().toarray() ); return ( user.collection.find( query).toarray().size() ) == 1; } public static int edit( string id, string name, string nickname, string pwd, string pwd2) { user user = user.collection.findonebyid( id); if ( user != null) { if ( pwd.equals( pwd2) ) { user updated = new user( name, nickname, pwd); user.collection.updatebyid( id, updated); return 0; } else { return 1; } } else { return 2; } } public static void removeall() { user.collection.drop(); } public string tostring() { return "{\"id\":\"" + id + "\", \"nickname\":\"" + nickname + "\", \"pwd\":\"" + pwd + "\", \"name\":\"" + name + "\"}"; } }
lastly, template (login.scala.html):
@(form: form[application.userlogin]) <!doctype html> <html lang="en"> <head> <link rel="stylesheet" type="text/css" href='@routes.assets.at( "stylesheets/bootstrap.min.css")' media="screen" /> <script src='@routes.assets.at( "javascripts/jquery-1.9.0.min.js")'></script> <title>log in</title> <meta http-equiv="content-type" value="text/html; charset=utf-8;" /> </head> <body> <div class="container"> @if( form.hasglobalerrors) { <div class="panel panel-danger" style="width: 400px; margin: auto; margin-top: 200px;"> <div class="panel-heading"><span class="glyphicon glyphicon-log-in"></span> @form.globalerror.message</div> } @if( !( form.hasglobalerrors) ) { <div class="panel panel-primary" style="width: 400px; margin: auto; margin-top: 200px;"> <div class="panel-heading"><span class="glyphicon glyphicon-log-in"></span> log in</div> } <div class="panel-body"> @helper.form( routes.application.authenticate) { <form> <input type="text" name="nick" class="form-control" placeholder="nick_name" /><br /> <input type="password" name="pwd" class="form-control" placeholder="password" /><br /> <button type="submit" class="btn btn-primary btn-lg btn-block">log in</button> <a class="btn btn-link" href="register">register</a> </form> } </div> </div> </div> </body> </html>
thanks in advance ;)
i couldn't find exact answer browsing around page, had implement such turnaround, in application.java:
public static result authenticate() { form<userlogin> loginform = form( userlogin.class).bindfromrequest(); userlogin op = new userlogin(); op.nick = loginform.field( "nick").value(); op.pwd = loginform.field( "pwd").value(); system.out.println( ); if ( op.validate() == null ) { session().clear(); session().put( "nickname", loginform.field( "nick").value() ); return redirect( routes.application.mainmenu() ); } else if ( loginform.haserrors() ) { return badrequest( login.render( loginform) ); } else { session().clear(); session().put( "nickname", loginform.field( "nick").value() ); return redirect( routes.application.mainmenu() ); } }
Comments
Post a Comment