Skip to content

Instantly share code, notes, and snippets.

@monzou
Last active December 17, 2015 15:29
Show Gist options
  • Save monzou/5632441 to your computer and use it in GitHub Desktop.
Save monzou/5632441 to your computer and use it in GitHub Desktop.
a little backbone-forms patch for supporting warning-level validation.
Form = Backbone.Form
Backbone.Form = class extends Form
commit: (options) ->
validationResults = @validate()
errors = _.filter validationResults, (result) -> result.level is "error"
return errors if errors and errors.length > 0
modelError = undefined
setOptions = _.extend {
error: (model, e) -> modelError = e if e.level is "error"
}, options
@model.set @getValue(), setOptions
return modelError if modelError
wrapEditor = (Editor) ->
class extends Editor
initialize: (options) ->
super options
@on "blur", => @form.fields[@key].validate()
getValidator: (validator) ->
v = super validator
(value, formValues) ->
result = v value, formValues
result.level or= validator.level or "error" if result
result
_.map Backbone.Form.editors, (Editor, key) -> Backbone.Form.editors[key] = wrapEditor Editor
Field = Backbone.Form.Field
Backbone.Form.Field = class extends Field
warningClassName: "warning"
validate: ->
validationResult = @editor.validate()
if validationResult
@setValidationResult validationResult
else
@clearValidationResult()
validationResult
setValidationResult: (result) ->
return if @editor.hasNestedForm
@clearValidationResult()
level = result.level
@$el.addClass level
element = @.$('[data-validation]')
element.data "validation-level", level
element.html(result.message);
clearValidationResult: ->
element = @.$('[data-validation]')
level = element.data "validation-level"
@$el.removeClass level
@.$('[data-validation]').empty();
setError: (msg) ->
throw new Error "Deprecated: you should use `setValidationResult`"
clearError: (msg) ->
throw new Error "Deprecated: you should use `clearValidationResult`"
class User extends Backbone.Model
schema:
code:
type: "Text"
validators: [
"required"
/^U.*/ # Regexp Validator
]
title:
type: "Select2"
options: [ "Mr", "Mrs", "Ms" ]
validators: [ "required" ]
name:
type: "Text"
validators: [ "required" ]
age:
type: "Integer"
validators: [
"required"
(value, formValues) -> { level: "warning", message: i18n.get "users.validationMessage.notAdult" } if value < 20 # Custom Validator
]
birthday:
type: "Datepicker"
validators: [
{ type: "required", level: "warning" } # Warning Validator
]
roles:
type: "Select2"
options: [ "Administrator", "Front", "Middle", "Back" ]
validators: [ "required" ]
editorAttrs:
multiple: "multiple"
email:
validators: [ "required", "email" ]
password:
type: "Password"
validators: [
"required"
type: "match", field: "passwordConfirmation", message: i18n.get "users.validationMessage.passwordMismatch" # Correlation Validator
]
passwordConfirmation:
type: "Password"
validators: [ "required" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment