ATuin

Client Validation Form

Oct 12th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.48 KB | None | 0 0
  1.   trait Validable[R <: BrewerRecord]
  2.   {
  3.     val validator: RecordValidator
  4.  
  5.     def newFromValidator(v: Command with ValidationSupport): R
  6.    
  7.     trait Validated {
  8.       type RV = RecordValidator
  9.       val validated: RV
  10.  
  11.       trait ValidatedOrElse {
  12.     def |[A](f:RV => A)
  13.       }
  14.  
  15.       def fold[A](f:RV => A)(g:RV => A) = (validated.fieldErrors.size > 0).
  16.     fold(f(validated), g(validated))
  17.  
  18.       def ?[A](f:RV => A) = new ValidatedOrElse {
  19.     def |(g:RV => A):A = fold(f)(g)
  20.       }
  21.     }
  22.  
  23.     def apply(params:Params) = new Validated {
  24.       val validated = validator.doBinding(params)
  25.     }    
  26.   }
  27.  
  28. trait BrewerValidator extends Command with ValidationSupport {
  29.   def notNull[T]: FieldValidator[T] = {
  30.     case None => RejectField[T](None, "Field can't be null")
  31.   }
  32. }
  33.  
  34. object Client {
  35.   implicit val clientValidable: Validable[Client] = new Validable[Client] {
  36.     type ClientValidator = BrewerValidator
  37.       with BrewerPasswordValidator
  38.       with BrewerEmailValidator
  39.  
  40.     val validator = new BrewerValidator
  41.       with BrewerPasswordValidator
  42.       with BrewerEmailValidator {
  43.     import Command._
  44.  
  45.     val userEmail = bind[String]("umail") validate
  46.       (notNull[String] orElse isEmail)
  47.     val userPassword = bind[String]("upw") validate
  48.       (notNull[String] orElse securePassword)
  49.       }
  50.  
  51.     def newFromValidator(v:ClientValidator):Client = Client(
  52.       userName = v.userEmail.value.get,
  53.       userPassword = v.userPassword.value.get)
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment