Component based validations for ember
Install with:
ember install ember-stickler
ember-stickler is comprised of two components validated-form
and validation-wrapper
. The validation-wrappers
wrap around each input and are nested inside a validated-form
.
validated-form
submits by calling sendAction
on whatever action attr you add and will default to submit
. It invokes the action passing the following arguments reset
, resolve
, reject
.
The validated-form
yields the following:
-
register
, this is used by thevalidation-wrappers
to register themselves with the form. -
submit
this will trigger the validations in each of thevalidation-wrappers
and will submit if they all pass. -
reset
sets all the validation state and errors back to their initial state and changes the form state frompending
toresolved
and reject takes an error object with keys being the name of the field and value an array of error message strings. -
formState
an object with the following properties:
isDefault
isPending
isResolved
text
// default, pending, resolved, rejected.disabled
// will be false when valid or isdisableDuringSubmit
attr is added to thevalidated-form
and the submit is pending.
All this allows you to do something like this in your route:
actions: {
submit(reset, resolve, reject) {
const self = this;
self.get('service').submit({
firstName: self.controller.get('firstName'),
lastName: self.controller.get('lastName'),
age: self.controller.get('age'),
})
.then(function() {
reset();
resolve();
})
.catch(function(errors) {
reject({
firstName: errors.firstName, //['first name is invalid!']
lastName: errors.lastName,
age: errors.age,
});
});
}
}
Errors are then yielded by the validated-form
and passed to each of the individual validation-wrappers
.
Validation rules are added to the validation-wrappers
with a rules attribute rules=''
(separated by spaces). Messages and other defaults can be overridden with additional camelCased attributes formatted like <validation-name><option>
.
The validated-wrapper
yields the following:
-
checkForValid
an action which will ignore errors and only change the state when it passes the validation. -
validate
which will check for both success and errors. -
errors
an array of error messages. -
validationState
object with the following properties:
valid
// null, true, falseisValid
// true, falseisInvalid
// true, falseisInitial
// true, falsetext
// valid, invalid, initial
stickler provides class-state
helper for managing error classes it takes valid
as the first param and then three classes first for true, the second for false and the third for null, the third param defaults to an empty string.
stickler also provides a first
helper which you can use to get the first error message from errors
Rule: email
Attrs: emailMessage
Rule: required
Attrs: emailMessage
Rule: min-length
Attrs: minLengthMessage
, minLength
Rule: max-length
Attrs: maxLengthMessage
, maxLength
Rule: exists
Attrs: existsMessage
Rule: url
Attrs: urlMessage
Rule: digits
Attrs: digitsMessage
Rule: number
Attrs: numberMessage
Rule: date
Attrs: dateMessage
Rule: dateISO
Attrs: dateISOMessage
Rule: credit-card
Attrs: creditCardMessage
Rule: max
Attrs: maxMessage
Rule: min
Attrs: minMessage
Rule: format
Attrs: formatMessage
, format
trim
digit
You can add more rules/transforms by creating a app/validations
directory and adding a
file in the following formats:
For Validations:
import Validation from 'ember-stickler/validation';
export default {
validate(value, errors) {
// add rule logic here
// push message to errors
// return errors
}
}
For Transforms:
import Transform from 'ember-stickler/transform';
export default Transform.create({
transform(value) {
// add transform logic here
// return new value
}
});
The this
context in all the rules will be the component that the
rule is validating allowing access to the value const value = this.get('value');
as well
as any other attrs const value = this.get('minLengthMessage');
.
The MIT License
Copyright (c) 2015 sethpollack, runspired
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.