Permalink
Browse files

Added support for java.lang.Boolean to BooleanField and updated Field…

….genericSetFromAny so that it properly handles Box/Option values that are not of the same type as the field. Fixed #951.
  • Loading branch information...
1 parent 28c8516 commit 7ed9d91a9d35192cf64ae2185baab2ffe6587f2e @davewhittaker davewhittaker committed Mar 29, 2011
@@ -273,9 +273,11 @@ trait TypedField[ThisType] extends BaseField {
case (value: String)::_ => setFromString(value)
case null|None|Empty => setBox(defaultValueBox)
case (failure: Failure) => setBox(failure)
+ case Some(other) => setFromString(String.valueOf(other))
+ case Full(other) => setFromString(String.valueOf(other))
case other => setFromString(String.valueOf(other))
}
-
+
/**
* Set the value of the field using some kind of type-specific conversion from a String.
* By convention, if the field is optional_?, then the empty string should be treated as no-value (Empty).
@@ -29,7 +29,14 @@ import S._
import JE._
trait BooleanTypedField extends TypedField[Boolean] {
- def setFromAny(in: Any): Box[Boolean] = genericSetFromAny(in)
+ def setFromAny(in: Any): Box[Boolean] = in match{
+ case b: java.lang.Boolean => setBox(Full(b.booleanValue))
+ case Full(b: java.lang.Boolean) => setBox(Full(b.booleanValue))
+ case Some(b: java.lang.Boolean) => setBox(Full(b.booleanValue))
+ case (b: java.lang.Boolean) :: _ => setBox(Full(b.booleanValue))
+ case _ => genericSetFromAny(in)
+ }
+
def setFromString(s: String): Box[Boolean] = setBox(tryo(toBoolean(s)))
private def elem(attrs: SHtml.ElemAttr*) =
@@ -214,6 +214,21 @@ object FieldSpec extends Specification("Record Field Specification") {
JBool(bool),
Full("<input checked=\"checked\" tabIndex=\"1\" value=\"true\" type=\"checkbox\" name=\".*\" id=\"mandatoryBooleanField_id\"></input><input value=\"false\" type=\"hidden\" name=\".*\"></input>")
)
+ "support java.lang.Boolean" in {
+ rec.mandatoryBooleanField.setFromAny(java.lang.Boolean.TRUE)
+ rec.optionalBooleanField.setFromAny(java.lang.Boolean.TRUE)
+ (rec.mandatoryBooleanField.is && (rec.optionalBooleanField.is getOrElse false)) must_== true
+ }
+ "support Full(java.lang.Boolean)" in {
+ rec.mandatoryBooleanField.setFromAny(Full(java.lang.Boolean.TRUE))
+ rec.optionalBooleanField.setFromAny(Full(java.lang.Boolean.TRUE))
+ (rec.mandatoryBooleanField.is && (rec.optionalBooleanField.is getOrElse false)) must_== true
+ }
+ "support Some(java.lang.Boolean)" in {
+ rec.mandatoryBooleanField.setFromAny(Some(java.lang.Boolean.TRUE))
+ rec.optionalBooleanField.setFromAny(Some(java.lang.Boolean.TRUE))
+ (rec.mandatoryBooleanField.is && (rec.optionalBooleanField.is getOrElse false)) must_== true
+ }
}
"CountryField" should {

0 comments on commit 7ed9d91

Please sign in to comment.