Question

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?

Was it helpful?

Solution

The fieldId : msg string is the toString representation of the FieldError class. That toString method is only meant to be used for logging / debugging though. There is no reason you should see it appear anywhere within your app. Generally you will validate your object and delegate the display of FieldErrors to Lift using something like S.error(listOfFieldErrors) and Lift's Msgs snippet. If you use some of the built in helpers for form generation like Crudify or LiftScreen the validation of fields and addition of the errors is handled for you.

OTHER TIPS

object PortfolioSchema extends Schema {

object name extends StringField(this, 256) {
  override def validations = valUnique(S ? ("unique.name")) _ :: super.validations

  def valUnique(errorMsg: => String)(value: ValueType): List[FieldError] =
    if (SystemConfiguration.unique_?(value))
      Nil
    else
      FieldError(this, Text(errorMsg)) :: Nil
}

This should work. The main difference is how you reference the field value and field identifier itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top