I'm new to both Lift
and Squeryl
.
I was following the example on the lift cookbook on how to create a Schema and some tables. I managed to do so and to insert records, this is my schema code:
object PortfolioSchema extends Schema {
val systemConfigurations = table[SystemConfiguration]
on(systemConfigurations) {
sc =>
declare(
sc.name defineAs (unique)
)
}
class SystemConfiguration extends Record[SystemConfiguration] with KeyedRecord[Long] {
override def meta: MetaRecord[SystemConfiguration] = SystemConfiguration
override val idField = new LongField(this)
val name = new StringField(this, 256) {
override def validations = valUnique("unique.name") _ :: super.validations
override def setFilter = trim _ :: super.setFilter
}
private def valUnique(errorMsg: => String)(name: String): List[FieldError] = SystemConfiguration.unique_?(name) match {
case false => FieldError(this.name, S ? errorMsg) :: Nil
case true => Nil
}
}
object SystemConfiguration extends SystemConfiguration with MetaRecord[SystemConfiguration] {
def unique_?(name: String) = from(systemConfigurations) {
p => where(lower(p.name) === lower(name)) select (p)
}.isEmpty
}
}
Long story short, I just have an entity with a field name that is unique and a validation function that checks this property and returns a FieldError
that is defined form the Lift
library like follows:
case class FieldError(field: FieldIdentifier, msg: NodeSeq) {
override def toString = field.uniqueFieldId + " : " + msg
}
object FieldError {
import scala.xml.Text
def apply(field: FieldIdentifier, msg: String) = new FieldError(field, Text(msg))
}
Basically what it does it attaches the field.uniqueFieldId
to the error message I specify, so if I use it like this:
valUnique("unique.name") _
What I get in the List[FieldError]
is Full(name_id) : unique.name
This doesn't look right to me because to get my error message I'll have to split the string and remember the field identifier, is there any better way to handle this error case?