问题描述
我试图定义一个trait Required
来封装逻辑,以验证所需的Record
Field
的存在,但是,我无法弄清楚应该是什么自我类型.我的目标是能够写出尽可能接近例如object foo extends SomeField(...) with Required
,但是我意识到我可能必须显式地将某些类型参数传递给Required
.
I'm trying to define a trait Required
to encapsulate the logic to validate the presence of required Record
Field
s, however, I haven't been able to figure out what the self type should be. My goal is to be able to write something as close as possible to e.g. object foo extends SomeField(...) with Required
, however I do realize I might have to explicitly pass in some type parameters to Required
.
到目前为止,我的不称职是:
My incompetent take so far has been:
导入net.liftweb.record.Field导入net.liftweb.util.FieldError
import net.liftweb.record.Fieldimport net.liftweb.util.FieldError
trait Required[ThisType, OwnerType] {
this: Field[ThisType, OwnerType] =>
def errMsg = "is required"
override def validations = {
val required =
(x: String) => if (x.isEmpty) List(FieldError(this, errMsg)) else Nil
// this asInstanceOf call also seems fishy
// --why's it even required if we already have the self type in place?
required :: super.asInstanceOf[Field[ThisType, OwnerType]].validations
}
}
但是,这会导致与存在类型相关的编译错误和警告:
however, this leads to compilation errors and warnings related to existential types from:
myfield = object SomeField(...) with Required[SomeField[SomeModel], SomeModel]
更何况是它距简明with Required
的距离.
not to mention it's as far as it gets from the concise with Required
.
我想出了这个:
trait Required[OwnerType] extends Field[String, OwnerType] {
def errMsg = "is required"
override def validations = {
val required =
(x: String) => if (x.isEmpty) List(FieldError(this, errMsg)) else Nil
required :: super.validations
}
}
但是,它不允许我在required
之前加上required
,因为它期望的是this.type.ValueType => List[FieldError]
而不是String => List[FieldError]
,我觉得很奇怪,因为在Field[String, ...]
,ValueType
的情况下是 String
.
however, it doesn't allow me to prepend required
to super.validations
because it expects this.type.ValueType => List[FieldError]
not String => List[FieldError]
, which I find odd because in the case of Field[String, ...]
, ValueType
is String
.
如果我将required
更改为ValueType => ...
,则会编译,但with Required[SomeModel]
会出现以下错误:
If I change required
to be ValueType => ...
, it compiles, but with Required[SomeModel]
errors out with:
...尽管StringField.ThisType
是String
并且String.OwnerType
是Record[SomeModel]
的子类,SomeModel
是MongoRecord[SomeModel]
的子类.我迷路了.
...even though StringField.ThisType
is String
and String.OwnerType
is a subclass of Record[SomeModel]
, SomeModel
being a subclass of MongoRecord[SomeModel]
. —I'm lost.
P.S.这与升降记录:空值有关必填字段,但没有验证错误
推荐答案
以下内容在SBT控制台中进行编译.
The following compiles in the SBT console.
import net.liftweb.util._
import net.liftweb.record._
import net.liftweb.record.field._
trait Required extends StringTypedField {
override def validations: List[ValidationFunction] = valMinLen(1, "Required!") _ :: super.validations
}
class TestRecord extends Record[TestRecord] {
val meta = TestRecord
object testField extends StringField(this, 255) with Required
}
object TestRecord extends TestRecord with MetaRecord[TestRecord]
这篇关于Lift中具有特征的通用字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!