Flexible DDAU form validation handling
Ember Stickler will handle errors from your server as easily and gracefully as errors from the client. If you've ever skimped on form validation or form UX, and felt guilty, this library is your new guilty pleasure.
Stickler is comprised of two components: validated-form
and
and the contextual component form.validation
, which manage your data
and error routing, and provide an easy access point to utilize validations.
ember install ember-stickler
Validations are added to form.validation
components by specifying the rules
attribute. Separate multiple rules by a space, rules will be run left to right,
and special transform
rules (such as trim
can manipulate the value for cleaner
comparisons.
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 a hash with the following properties:
-
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. -
state
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.
-
errors
-
validation
a contextual component you can use to localize error handling.
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 form.validation
components.
The form.validated
component yields the following:
-
check
an action which will ignore errors and only change the state when it passes the validation. -
validate
an action which will check for both success and errors. -
errors
an array of error messages. -
state
object with the following properties:
valid
// null, true, falseisValid
// true, falseisInvalid
// true, falseisInitial
// true, falsetext
// valid, invalid, initial
Stickler provides a for-bool
helper for managing error classes.
It takes valid
as the first param and then three classes.
- first for true
- second for false
- third for null (defaults to an empty string)
Stickler also provides a first
helper which you can use to get the first error message from errors
And a join
helper which you can use when you need to combine strings
with a separator (it's first arg).
{{input
class=(join ' ' 'class-a' 'class-b' 'class-c')
}}
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 your own rules and transforms.
generate a transform
ember g stickler-transform <name>
generate a rule
ember g stickler-rule <name>
The MIT License
Copyright (c) 2015-2016 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.