Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft of validation binding for chip #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = function(grunt) {
'src/filter.coffee',
'src/bindings.coffee',
'src/filters.coffee',
'src/diff.coffee'
'src/diff.coffee',
'src/validation.coffee'
];

var libFiles = [
Expand Down
122 changes: 122 additions & 0 deletions src/validation.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# ## validate-*
chip.binding 'validate-*', (element,attr,controller) ->
controller.parent.hasValidation = true
controller.parent.validation = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for forms that have portions inside of partials, perhaps several deep, or forms that have repeating sections which create controllers for that section.

We'll need to have a binding at the form level that will create the validation object on that controller. Here we can throw an error if the validation object doesn't exist indicating that it must be created using bind-validate or whatever we call the aggregating binding.

type = attr.camel
# Initially tested for a validation object with typeof, but that felt too
# rigid, so I simplfied it a bit.
if attr.value.indexOf(':') < 1
# Loading options as an object passed from the controller
options = controller.eval attr.value
else
# Attempt to parse options directly from validation attribute
# We will probably need to spend more time working out the best format here
# since passing an object string in the attribute isn't going to work.
#
# Currently using the following 'shortcode' for validation options:
# option:value -e 'This is an optional message'
#
# Seperate options with | character (do not love this).
options = {}
optsArry = attr.value.split '|'
for opt in optsArry
optArry = opt.split ':'
optionKey = optArry[0].trim()
optionValMsg = optArry[1].split '-e'
options[optionKey] = {}
options[optionKey].value = optionValMsg[0].trim()
options[optionKey].errorMessage = optionValMsg[1]?.trim()

# maybe store custom validations to check validation type against
# customValidations = {}

# Some preset validations
# Focusing less on the actual validations themselves and more on a way to
# evaluate preset and custom validations since every app has different needs.
#
# When defining a validation rule, use the following:
# {x} = field value, {y} = validation option value
validations =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great, maybe in a second pass, to have this at a more global level and add validations via chip.validation('name', rules) or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that was the idea here. I left that there as a reminder, but would want it to be something set globally in your app config.

all:
required:
evaluation: '{x} != ""'
errorMessage: 'This is a required field.'
text:
minLength:
evaluation: '{x}.length >= {y}'
errorMessage: 'This field must be at least {y} characters long.'
maxLength:
evaluation: '{x}.length <= {y}'
errorMessage: 'This field must be at most {y} characters long.'
# numerical:
# default: '{x}.match(/0-9/)'
minValue:
evaluation: '{x} >= {y}'
errorMessage: 'This value must be greater than {y}.'
maxValue:
evaluation: '{x} <= {y}'
errorMessage: 'This value must be less than {y}.'
email:
default:
evaluation: '{y}.match(/@/)'
errorMessage: 'Please enter a valid email address.'

element.on 'change', ->
value = element.val()
validResponse = validateField(value,type,options)
if validResponse.valid
# Do some happy, positive things
console.log 'Hey Slugger, you did it right!'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the debugging code before merge.

element.removeClass('chip_validation_invalid')
element.addClass('chip_validation_valid')
else
# Scorch the earth with shameful red error messages
console.log 'Nope, not even close. Here\'s where you went wrong, Sparky:'
console.error validResponse.errorMsgs
element.removeClass('chip_validation_valid')
element.addClass('chip_validation_invalid')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can add an invalid object to controller.validation that errors can be added to by element.attr('name') if set, so that fancier things can be done in the UI when a field is invalid that can't be done with just a CSS class.

<input name="firstName" validation-text="required"> ...somewhere else on the page... <help-text bind-if="validation.invalid.firstName">You must include your first name</help-text>

controller.validation.valid = validResponse.valid
controller.validation.errorMsgs = validResponse.errorMsgs

validateField = (value,type,options) ->
response = {
valid:true
message:''
errorMsgs:[]
}

validateVal = (validation) ->
if validation.value?
return validation.value
else
return validation

validateMsg = (validation) ->
if validation.message?
return validation.message
else
return null

isValid = true
errorMsgs = []
for optionName, optionValue of options
validation = validations[type][optionName] || validations.all[optionName]
if validation?
rule = validation.evaluation.replace('{x}','"' + value + '"')
.replace('{y}',optionValue.value)
if eval rule
isValid = true;
else
isValid = false
if validation.errorMessage
errorMsgs.push validation.errorMessage.replace '{y}',
optionValue.value
else
validation.errorMessage 'Input for this field is invalid.'
else
'validation missing'
response.valid = true;
console.warn 'Validation method: ' + optionName + ' not found!'
response.valid = isValid
response.errorMsgs = errorMsgs
return response