Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

more description for the validations

  • Loading branch information...
commit a7274034da8ff70b65cd189d651beeb3240a51bc 1 parent d13b724
@dnagir dnagir authored
Showing with 17 additions and 2 deletions.
  1. +17 −2
@@ -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.
@@ -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):
+page = new @Page name: ''
+page.isValid() # false
+ = '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}`.
Please sign in to comment.
Something went wrong with that request. Please try again.