Permalink
Browse files

more description for the validations

  • Loading branch information...
dnagir committed Dec 20, 2011
1 parent d13b724 commit a7274034da8ff70b65cd189d651beeb3240a51bc
Showing with 17 additions and 2 deletions.
  1. +17 −2 README.md
View
@@ -75,7 +75,6 @@ Now let's see how we can show the validation errors on the page and bind everyth
If you are using the model, you can also take advantage of the client-side validation framework.
-
The piece of code below should explain everything, including some of the options.
```coffee
@@ -118,8 +117,12 @@ It is recommended to avoid custom inline validations and create your own validat
ko.validators.funky = (model, fields, options) ->
# options - is an optional set of options passed to the validator
word = options.word || 'funky'
+ result = {}
fields.each (field) ->
- model.error[field]("should be #{word}") if model[field]().indexOf(word) < 0
+ result[field] = "should be #{word}" if model[field]().indexOf(word) < 0
+
+ # you MUST return a hash of field-error or empty hash/null for no errors
+ return result
```
so that you can use it like so:
@@ -129,6 +132,18 @@ validates: (page) ->
funky 'name', {word: 'yakk'}
```
+Here's how you would check whether the model is valid or not (assuming presence validation on `name` field):
+
+```coffee
+page = new @Page name: ''
+page.isValid() # false
+
+page.name = 'Home'
+page.isValid() # true
+
+page.performValidation() # To force the validation, you don't need to do that really
+```
+
Every validator has its own set of options.
The general format of the validator is: `validatorName field1, field2, field3, {optional: options, asA: hash}`.

0 comments on commit a727403

Please sign in to comment.