This project intends to show my approach to use the ember-cp-validations plugin.
This project is a simple user administration tool, where you can create, list, update and delete users.
A user has three attributes with individual validations:
- Name (presence, length)
- E-Mail (presence, format)
- Password (presence, length)
A Node.js Express server is used to serve user data and generate random backend validation errors. Saving data is not implemented, session data will be lost after page reload.
I have following requirements:
- When opening a new form, there should be no error shown
- After typing, errors should be shown for the according field
- When trying to submit an empty new form, all validation errors should be shown
- When opening an edit form, all errors should be shown immediately (backend might send data which must be updated before user can save)
- When a validation fails on backend side, the validation error should be presented identical to client validation errors
I created different Versions with Git Tags to show my progress and different approaches with different complexity.
Simple interface without any validation.
Failing all requirements.
- Check out git tag
1-no-validation
- Create and update users -> no validation is active.
Implementation very close to suggestion at bottom of ember-cp-validations/docs.
Failing requirements 1, 3, 5.
- Check out git tag
2-default-validation
- Click Add User -> validation errors appear immediately (failing requirement 1, 3)
- Click Edit for user Test -> validation error appears immediately (fulfilling requirement 4)
Show validations via components validationError
attribute, shadowing the plugin results. This attribute will be
updated by:
didRender
hook to show initial validation errors on new form.- Observer on "V-Get Helper Module message" of current attribute
- Observer on model
revalidation
flag, which is triggered on failed validation in new route.
Failing requirement 5
- Check out git tag
3-advanced-validation
- Click Add User -> validation errors do not appear (fulfilling requirement 1)
- Click Save -> validation errors appear (fulfilling requirement 3)
- Click Edit for user Test -> validation error appears immediately (fulfilling requirement 4)
Show server validation errors by watching model.errors.[]
which is updated by adapter and use serverValidationError
function in component to render these errors into template.
The "backend server" is configured to send random validation errors. The random error ratio can be adjusted in
/server/index.js
by changing randomErrors
.
Fulfilling all requirements.
- Check out git tag
4-server-validation
- Edit user Alice and click Save -> validation error(s) might appear (repeat several times or set "random error ratio" to 1.0 as described above)
- Repeat until validation errror(s) appeared
- Repeat until save was succesful appeared
Based on example at https://offirgolan.github.io/ember-cp-validations/ (full code)
- Better approach: no observers or watches only computed properties
- Still need to set a flag in new controller:
this.set('didValidate', true)
Failing requirement 5
- Check out git tag
5-advanced-validation-b
- Click Add User -> validation errors do not appear (fulfilling requirement 1)
- Click Save -> validation errors appear (fulfilling requirement 3)
- Click Edit for user Test -> validation error appears immediately (fulfilling requirement 4)
Show server validation errors by watching model.errors.[]
which is updated by adapter and use serverValidationError
function in component to render these errors into template.
The "backend server" is configured to send random validation errors. The random error ratio can be adjusted in
/server/index.js
by changing randomErrors
.
Fulfilling all requirements.
- Check out git tag
6-server-validation-b
- Edit user Alice and click Save -> validation error(s) might appear (repeat several times or set "random error ratio" to 1.0 as described above)
- Repeat until validation errror(s) appeared
- Repeat until save was succesful appeared
Show server validation with ds-error
validator.
The "backend server" is configured to send random validation errors. The random error ratio can be adjusted in
/server/index.js
by changing randomErrors
.
Fulfilling all requirements.
- Check out git tag
7-server-validation-c
- Edit user Alice and click Save -> validation error(s) might appear (repeat several times or set "random error ratio" to 1.0 as described above)
- Repeat until validation errror(s) appeared
- Repeat until save was succesful appeared
You will need the following things properly installed on your computer.
git clone <repository-url>
this repositorycd demo-cp-validatons
npm install
bower install
ember serve
- Open http://localhost:4200.
Be aware: app/adapters/application.js
configures host to localhost:4200
. If you start your server on a different
port, you have to adapt this file.