ldx-widgets
Version:
widgets
60 lines (41 loc) • 1.34 kB
text/coffeescript
React = require 'react'
{li} = React.DOM
###
Form Validation
@props.showValidationErrors - OPTIONAL - Boolean
indicator to determine whether validation errors should be visible on the element
@props.validation - OPTIONAL - Array
Array of validation evaluation rules. Required if validation is being used.
###
module.exports =
elementValid: true
validationErrors: []
validationErrorIds: []
getDefaultProps: ->
{
validation: []
showValidationErrors: false
}
isValid: ->
return @elementValid
getErrors: ->
{validation, showValidationErrors} = @props
@validationErrors = []
@validationErrorIds = []
errors = []
for evaluation, index in validation when evaluation?.valid is false
{error, id} = evaluation
# Track the IDs of the errors, if being used
@validationErrorIds.push(id) if id
# Add the validation errors for display
@validationErrors.push(li {
className: 'field-error'
key: error
}, error)
# Since there are validation errors, run the validate method again
@validateElement()
@invalidClass = if @validationErrors.length and showValidationErrors then ' invalid' else ''
validateElement: ->
@elementValid = @validationErrors.length is 0
componentDidUpdate: ->
@validateElement()