Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:37:04+00:00 2026-06-13T04:37:04+00:00

I have a simple User model whose fields are annotated with play validation annotations

  • 0

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);
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:37:05+00:00Added an answer on June 13, 2026 at 4:37 am

    Now I understand, createNewUser is not an action.
    So you can enforce object validation:

    public void createNewUser(User user) {
        final Validation.ValidationResult validationResult = validation.valid(user);
        if (validationResult.ok) {
            userDao.save(user);
        }   else {
            System.out.println("has errors");
        }
    }
    

    api: http://www.playframework.org/documentation/api/1.2.5/play/data/validation/Validation.html

    Old answer

    You forgot an annotation to validate the object and you must check if the form has errors.

    public void createNewUser(@Valid User user) {
         if(validation.hasErrors()) ...
    

    source: http://www.playframework.org/documentation/1.2.5/validation#objects

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
FOUND THE ANSWER MYSELF, see below... I'd like to have a very simple user
I have a simple User model which is associated to many Town objects using
I have a simple web application which lets the user upload local user data
I have a simple model setup in my Ruby on Rails app. (User {name,
I have a very simple user model. When I first created it, it had
Simple rails app: I have 2 models, user and intro [which is simply a
So I have a simple web app where I can create a user (UserModel)
I have a simple user production report where daily quotas are tracked. The sql
This is driving me crazy. I have a very simple user control: public int?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.