2012-08-29 137 views
0

我不知道如何從我的函數的validate()返回「無效的用戶名或密碼」的模式顯示錯誤消息時提交不是「有效」如何顯示錶單驗證錯誤

index.scala。 HTML

@(myForm: Form[models.profile.MUser]) 

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

@main("welcome") { 

    <h1>Login with your account</h1> 

@helper.form(action = routes.Application.loginAccount()) { 

    @helper.inputText(myForm("username"),'_showConstraints -> false) 
    @helper.inputPassword(myForm("password"),'_showConstraints -> false) 

    <input type="submit" value="Login"> 
}} 

Application.java

public static Result loginAccount() { 
     Form<MUser> filledform = loginForm.bindFromRequest(); 
     if (filledform.hasErrors()) { 
      Logger.debug("unsuccessfull loggin"); 
       return badRequest(index.render(filledform)); 
     } 
     MUser user = filledform.get(); 
     SessionCache.setCache(SessionCache.Constants.LOGGED_IN_USER, 
       UserController.findUserByUserName(user.username)); 
     return redirect("/home"); 

    } 

MUser.java 包models.profile;

import controllers.services.UserController; 
import models.entities.UserDTO; 
import play.data.validation.Constraints.*; 
import scala.Serializable; 

/** 
* @author fbranchetti 
* 
*/ 
public class MUser implements Serializable{ 

    @Required 
    public String username; 
    @Required 
    public String password; 


    public String validate() { 
     if (authenticate(username, password)) { 
      return "Invalid username or password"; 
     } 
     return null; 
    } 


    private boolean authenticate(String username, String password) { 
     UserDTO fUser = new UserDTO(); 
     fUser.setPassword(password); 
     fUser.setUsername(username); 

     return !UserController.logginUser(fUser); 
    } 

} 

回答

1

您可以將認證結果存儲在Flash變量中。 並在視圖中,您可以檢查變量的存在並顯示它。