Play! 1.2.5 with mongodb | Model Validation not happening
Posted
by
TGV
on Stack Overflow
See other posts from Stack Overflow
or by TGV
Published on 2012-10-16T17:04:47Z
Indexed on
2012/10/17
5:02 UTC
Read the original article
Hit count: 206
I have a simple User model whose fields are annotated with play validation annotations and morphia annotations like below.
import play.data.validation.*;
import play.modules.morphia.Model;
import com.google.code.morphia.annotations.*;
@Entity
public class User extends Model{
@Id
@Indexed(name="USERID", unique=true)
public ObjectId userId;
@Required public String userName;
@Email
@Indexed(name="USEREMAIL", unique=true)
@Required public String userEmail;
}
Now I have a service which has a CreateNewUser method responsible for persisting the data. I have used Morphia plugin for the dao support. But the problem is that User Document gets persisted in mongo-db even if userName or userEmail is NULL. Also @Email validation does not happen
// Below code is in app/controllers/Application.java
User a = new User();
a.userName = "user1";
// calling bean to create user, userService is in app/service/UserService
userService.createNewUser(a);
It does not work even after adding @valid and validation.hasErrors() check.Below code is in app/service/UserService
public void createNewUser(@Valid User user) {
if (Validation.hasErrors()) {
System.out.println("has errors");
} else {
// TODO Auto-generated method stub
userDao.save(user);
}
}
© Stack Overflow or respective owner