Skip to content

Commit

Permalink
Merge pull request #9 from hacklanta/empty-validation
Browse files Browse the repository at this point in the history
Empty Validation: Allow validating fields that are not sent to the server

We have an issue where an empty field can't be marked as invalid because
it never gets a chance to run against validations. To fix this, we:

 - Separate the computation of the final, fully validated value of the field
   into its own method, and cache it in a `TransientRequestVar`.
 - Cache the field name in a `RequestVar` and abstract out the adding of
   a validation error for the field based on this cache.
 - Introduce a new concept, boxed validations, which are validations that
   can take boxes of the field value instead of just the field value.
 - Add overloads for `withValidation` and `?` to be able to add boxed
   validations.
 - Run those validators when computing the final value if there is no seen
   value for the field, allowing the boxed validators to run even if there is
   no submission for the field.
  • Loading branch information
Shadowfiend authored Nov 14, 2016
2 parents 98ed65f + 5b28513 commit ddb1ecd
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_Store
.DS_Store?
.ensime
.ensime_cache/
.ensime_lucene/
.history
.idea_modules/
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ A basic form snippet would look like this:
// Assuming a case class User(name: String, age: Int, phoneNumber: String, termsAndConditions: Boolean).
User(name, age, phoneNumber, termsAndConditions).save
}

"form" #> registrationForm.binder
```

Expand Down Expand Up @@ -142,6 +142,15 @@ the set of validations that are supported by
[Parsley.js](http://parsleyjs.org). These will have matching server-side
validation implementations.
In certain very specific cases, it can be useful to run a validation even
if the value in question isn't actually submitted in the form. Typically
this is used for a check that a required field was submitted, particularly
when it comes from a checkbox or radio button. Either way, in these cases
a validation can take a `Box[T]` instead of `T`. It will receive an `Empty`
if the field is not submitted, and a `Full` with the deserialized value if
the field is submitted. Lift-formality's own `notBlank`/`notEmpty` validators
will correctly handle unsubmitted fields.
### Event handling
Formality can also add server-side event handlers to form inputs:
Expand Down
Loading

0 comments on commit ddb1ecd

Please sign in to comment.