Lift中记录字段的临时验证

时间:2012-06-19 15:58:41

标签: scala mongodb lift

我正在我的电梯应用程序中开发我的用户类,并开发了两个表单,一个用于用户注册,一个用于用户登录。

我有以下用户类:

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"))
      }
    }
}

以下两个屏幕:

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)
    }

}

我需要确保在SignupWizard屏幕中,电子邮件地址是唯一的。我可以通过更改MongoUser类中的电子邮件字段来完成此操作:

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

但现在这意味着验证规则也适用于登录,这显然不是我想要的。

问题:在不同的屏幕上向我的MongoUser字段添加临时验证规则的最有效方法是什么?

1 个答案:

答案 0 :(得分:0)

不要将有问题的验证放在Record中,并使用向导中的自定义字段:

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