Ad-hoc validation for Record in Lift fields

I am developing my user class in my upgrade application and have developed two forms: one for user registration and one for user login.

I have the following user class:

class MongoUser private () extends MongoRecord[MongoUser]
  with MongoId[MongoUser] {

    def meta = MongoUser

    object firstname extends StringField(this, "")
    object lastname extends StringField(this, "")
    object password extends PasswordField(this, "")
    object email extends EmailField(this, 90)
    object business extends StringField(this, "")

    /*
    * validation method for checking email is unique
    */
    protected def emailUnique(emailVal:String) = {
      meta.findAll("email", emailVal) match {
        case Nil => Nil
        case _ => List(FieldError(email, "Email should be unique"))
      }
    }
}

And the following two screens:

object SignupWizard extends Wizard {
  object user extends WizardVar(MongoUser.createRecord)

  val person = new Screen {

    addFields(() => user.is.firstname)
    addFields(() => user.is.lastname)
    addFields(() => user.is.password)
    addFields(() => user.is.email)


    override def nextScreen = {
      //user.is.save 
      business
    }
  }

  val business = new Screen {
    addFields(() => user.is.business)
  }

  def finish() {
    user.is.save
  }

}

/*
 *Screen for user login
 */

object LoginScreen extends LiftScreen {
  object user extends ScreenVar(MongoUser)

  addFields(() => user.is.email)
  addFields(() => user.is.password)


  def finish() {
    S.notice(
      user.is.login(
        user.email.toString,
        user.password.toString).toString)
    }

}

I need to make sure that the RegistrationWizard screen has a unique email address. I can do this by changing the email field in the MongoUser class as follows:

object email extends EmailField(this, 90) {
      override def validations = {
        emailUnique _ ::
        super.validations
      }
 }

But now this means that the authorization rule is also applied to enter the system, which is clearly not what I want.

Question: What is the most efficient way to add ad-hoc validation rules to my MongoUser fields on different screens?

+5
source share
1 answer

:

object wizard extends Wizard {
   // don't add the email field with addField()
   val email = field("Email", user.is.email.is, trim, emailUnique _, ...)
}
0

All Articles