如何为play2的表单定义StopOnFirstFail dsl? [英] How to define a StopOnFirstFail dsl for play2's form?

查看:101
本文介绍了如何为play2的表单定义StopOnFirstFail dsl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题中:

In this question: If a form field has multi validators, how to let play verify them one by one, not all?, Julien gave me a method named stopOnFirstFail to solve my problem:

def stopOnFirstFail[T](constraints: Constraint[T]*) = Constraint { field: T =>
  constraints.toList dropWhile (_(field) == Valid) match {
    case Nil => Valid
    case constraint :: _ => constraint(field)
  }
}

它的用法是:

val loginForm = Form(
  "name" -> (text verifying stopOnFirstFail( nonEmpty, minLength(4) ))
)

但是我希望定义一个dsl,它可以用作:

But I hope to define a dsl which can be used as:

val loginForm = Form(
  "name" -> (text verifying ( nonEmpty or minLength(4) ))
)

我试图为play.api.data.validation.Constraint定义一个隐式方法:

I tried to defined an implicit method for play.api.data.validation.Constraint:

import play.api.data.validation._

implicit def _Constraint[T](cons: Constraint[T]) = new {

  def or[T](other: Constraint[T]) = Constraint { field: T =>
    cons(field) match {              // (!)
      case Valid => other(field)
      case invalid => invlaid
    }
  }
}

但是它无法编译,(!)行中的错误是,消息是:

But it can't be compiled, the error in on the (!) line, and message is:

type mismatch; 
found: field.type (with underlying type T) required: T 
Note: implicit method _Constraint is not applicable here
      because it comes after the application point and it lacks an explicit result type

如何解决?

推荐答案

or方法不使用类型参数:

The or method does not take a type parameter:

implicit def toLazyOr[T](cons: Constraint[T]) = new {
  def or(other: Constraint[T]) = Constraint { field: T =>
    cons(field) match {
      case Valid => other(field)
      case Invalid => Invalid
    }
  }
}

这篇关于如何为play2的表单定义StopOnFirstFail dsl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆