So, as a first stab at validation for category names in the image
gallery example I posted yesterday, and basing myself on the "Starting
with Lift" tutorial, I replaced:

  object name extends MappedPoliteString(this, 128)

with

  object name extends MappedPoliteString(this, 128) {
    override def defaultValue = ""

    private def noSlashes(s: String) : List[FieldError] =
      if (s.contains("/")) {
        List(FieldError(this, Text("Category name \"" + s + "\" may not contain 
\"/\"")))
      } else {
        Nil
      }

    override def validations = valMinLen(1, "Category name must not be empty") 
_ ::
      valUnique("Category name must be unique") _ ::
      noSlashes _ ::
      super.validations
  }

in my model, and replaced the snippet to create a category with:

  private def saveCategory(i: ImageCategory): Unit =
    i.validate match {
      case Nil =>
        i.saveMe
        S.notice("Changes saved")
      case err =>
        S.error(err)
    }

  def doNewCategory(in: NodeSeq): NodeSeq = {
    var newCat = ImageCategory.create
    bind("nc", in,
         // XXX XXX need to validate (at least no /)
         "name" -> newCat.name.toForm,
         "create" -> SHtml.submit("Create", {() => saveCategory(newCat)})
       )
  }

(and made the matching change to the category edit snippet).

However, although the new defaultValue is honored, the new validations
don't seem to get run -- I can create duplicate, empty, or illegal
category names without triggering a FieldError.

What am I missing?

As always, thanks for all help with this -- the quick and helpful
answers I've gotten on this list are certainly one of my favorite parts
of the Lift framework!

-- 
                                Jim Wise
                                [email protected]

Attachment: pgpSCOcrrxaBT.pgp
Description: PGP signature

Reply via email to