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

  • SEARCH
  • Home
  • 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 8300197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:27:20+00:00 2026-06-08T16:27:20+00:00

SOLUTION: I could not figure our how to return the non-exists for of Option[User]

  • 0

SOLUTION: I could not figure our how to return the non-exists for of Option[User] so in the case of not user found I create a dummy user object and reason on it from the controller (feels awful but working…):
from Application.scala

val loginForm = Form(
tuple(
  "email" -> text,
  "password" -> text
) verifying ("Invalid email or password", result => result match {
  case (email, password)  => (User.authenticate(email, password).map{_.id}.getOrElse(0) != 0)
})

)

AS OPPOSED TO:

val loginForm = Form(
  tuple(
  "email" -> text,
  "password" -> text
) verifying ("Invalid email or password", result => result match {
  case (email, password) => User.authenticate(email, password).isDefined
})

)

++++++++++++++++++ ORIGINAL 2 ++++++++++++++++++
Thanks for the advice! I have made some change and seem to be getting closer however I can’t figure out how to return an undefinded Option[user]. I have also tried case _ => null, See below:

From User.scala

case class User(id: Int, email: String, name: String, password: String)

object User {

  // -- Parsers

  /**
   * Parse a User from a ResultSet
   */
  val userParser = {
            get[Option[Int]]("uid")~        
            get[Option[String]]("email")~
            get[Option[String]]("fname")~
            get[Option[String]]("pbkval") map {
            case (uid~email~name~pbkval) => validate(uid,email, name, pbkval)
            }
  }

  /**
   * Retrieve a User from email.
   */
  def findByEmail(email: String): Option[User] = {
    DB.withConnection { implicit connection =>
      SQL("select * from get_pbkval({email})").on(
                'email -> email         
            ).as(userParser.singleOpt)
    }
  }


  /**
   * Authenticated user session start.
   */
  def authenticate(email: String, password: String): Option[User] = {
    DB.withConnection { implicit connection =>
      SQL(
            """
                        select * from get_pbkval({email})
            """
            ).on(
                'email -> email
            ).as(userParser.singleOpt)

        }
    }

  /**
   * Validate entry and create user object.
   */
    def validate(uid: Option[Int], email: Option[String], fname: Option[String], pbkval: Option[String]): User = {
                val uidInt : Int = uid.getOrElse(0)
                val emailString: String = email.getOrElse(null)
                val fnameString: String = fname.getOrElse(null)
                val pbkvalString: String = pbkval.getOrElse(null)
                User(uidInt, emailString, fnameString, pbkvalString)
    }

I guess it is clear that I am not really getting something fundamental here.. I have read through http://www.playframework.org/modules/scala-0.9.1/anorm and searched around for hours.. any help would be much appreciated!

  • 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-08T16:27:26+00:00Added an answer on June 8, 2026 at 4:27 pm

    You didn’t specify which rows to map. After your row mapper, put a * to signify which rows to map. While you’re at it, I find it easier to define my row mapper in a separate val. Something like this.

     val user = { 
       get[Option[Int]]("uid")~        
       get[Option[String]]("email")~
       get[Option[String]]("fname")~
       get[Option[String]]("pbkval") map {
        case uid~email~name~password => validate(uid,email, name, password)
      }
    }
    
    def authenticate(email: String, password: String): Option[User] = {
    DB.withConnection { implicit connection =>
      SQL(
        """
            select * from get_pbkval({email})
        """
        ).on(
            'email -> email
        ).as(user *)
      }
     } 
    
        def validate(uid: Option[Int], email: Option[String], fname: Option[String], pbkval: Option[String]): Option[User] = {
        if (uid != None) {
                val uidInt : Int = uid.getOrElse(0)
                val emailString: String = email.getOrElse(null)
                val fnameString: String = fname.getOrElse(null)
                val pbkvalString: String = pbkval.getOrElse(null)
                User(uidInt, emailString, fnameString, pbkvalString)
        } else { return null}
    }
    

    Note the “as” method now has two arguments, your row mapper (which is now defined as a val “user”, and a “*” signifying that you want to map all of the rows.

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

Sidebar

Related Questions

i have this problem: ms-access could not delete and i found a potential solution
I have research about an issue and could not find a solution for it
Although I read dozens of answers I could not find a solution. I'm using
I have been trying to find a solution to this one but could not
It looks like a problem which could have simple solution, but I haven't found
I have already searched through SO and could not fins a workable solution for
I search for a working solution for a rather simple problem, but could not
Help, I've broken my Rails3 user model! I first noticed that I could not
I'm doing research to figure out what messaging solution to settle on for our
We have a quite common object in our application. In this case, we'll call

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.