0

I've been trying to figure out how to do this without manually defining a validation but without any success so far.

I have a StringField

class Foo private() extends MongoRecord[Foo] with ObjectIdKey[Foo] {
  ...
  object externalId extends StringField(this, 255) {
    // none of these seem to have any effect on validation whatsoever:
    override def optional_? = false
    override def required_? = true
    override def defaultValueBox = Empty
  }
  ...
}

Now when I call .validate on a Foo, it returns no errors:

val foo = Foo.createRecord
foo.validate match {
  case Nil => foo.save
  ...
}

...and the document is saved into the (mongo) DB with no externalId.

So the question is: is there any way at all to have Lift automatically validate missing fields without me having to manually add stuff to validations?

EDIT: am I thinking too much in terms of the type of productivity that frameworks like Django and Rails provide out of the box? i.e. things like basic and very frequent validation without having to write anything but a few declarative attributes/flags. If yes, why has Lift opted to not provide this sort of stuff out of the box? Why would anybody not want .validate to automatically take into consideration all the def required_? = true/def optional_? = false fields?

4

1 回答 1

1

据我所知,如果不明确定义验证,您就无法验证字段。不提供验证的原因是optional_?required_?使用什么逻辑并不总是很清楚,尤其是对于非String字段。Crudify 使用该required_?值本身来确定是否在生成的 UI 中将字段标记为必需,但由您提供适当的逻辑来确定是否满足要求。

验证字段可以很简单

override def validations = super.validations :: valMinLen(1, "Required!")

或者在此处查看其他问题的答案,了解如何创建通用Required特征。

于 2014-03-14T14:46:06.383 回答