Skip to content

Commit

Permalink
adding form utils and username validation
Browse files Browse the repository at this point in the history
  • Loading branch information
beczkowb committed Feb 23, 2017
1 parent 6358328 commit 005aefc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
20 changes: 20 additions & 0 deletions openchat/app/components/register-form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import Ember from 'ember';
import {TextInput, MaxLength} from '../utils/text-input';


export default Ember.Component.extend({
username: TextInput.create({
value: '',
validators: [new MaxLength(30)]
}),
email: '',
password: '',
confirmPassword: '',

usernameErrors: [],


actions: {
onUsernameChange() {
let usernameInput = this.get('username');
usernameInput.validate();
}
}

});
21 changes: 12 additions & 9 deletions openchat/app/templates/components/register-form.hbs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<form>
<div>
<label for="username">Username</label>
{{ input type="text" value=username id="username" class="form-control"
name="username" aria-describedby="usernameHelpBlock" }}
{{ input type="text" value=username.value id="username" class="form-control"
name="username" aria-describedby="usernameHelpBlock" key-up=(action 'onUsernameChange') }}

<div class="alert alert-danger">
<div>
Username is required.
{{#if username.errors}}
<div class="alert alert-danger">
<div>
Username is required.
</div>
</div>
</div>
{{/if}}

</div>

<div>
<label for="email">Email</label>
{{ input type="text" value=username id="email" class="form-control"
{{ input type="text" value=email id="email" class="form-control"
name="email" aria-describedby="emailHelpBlock" }}
<div class="alert alert-danger">
<div>
Expand All @@ -24,7 +27,7 @@

<div>
<label for="password">Password</label>
{{ input type="password" value=username id="password" class="form-control"
{{ input type="password" value=password id="password" class="form-control"
name="password" aria-describedby="passwordHelpBlock" }}
<div class="alert alert-danger">
<div>
Expand All @@ -35,7 +38,7 @@

<div>
<label for="confirmPassword">Confirm Password</label>
{{ input type="password" value=username id="confirmPassword" class="form-control"
{{ input type="password" value=confirmPassword id="confirmPassword" class="form-control"
name="confirmPassword" aria-describedby="confirmPasswordHelpBlock" }}
<div class="alert alert-danger">
<div>
Expand Down
40 changes: 40 additions & 0 deletions openchat/app/utils/text-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Ember from 'ember';

export const TextInput = Ember.Object.extend({
init() {
this._super(...arguments);
this.set('errors', {});
},

validate() {
let validators = this.get('validators');
let errors = {};
let value = this.get('value');
let hasErrors = false;

for (let i = 0; i < validators.length; i++) {
let validator = validators[i];

if (!validator.isValid(value)) {
errors[validator.validatorName] = validator;
hasErrors = true;
}
}

if (!hasErrors) {
errors = false;
}

this.set('errors', errors);
console.log(errors);
}
});

export function MaxLength(maxLength) {
this.validatorName = 'maxLength';
this.maxLength = maxLength;

this.isValid = function(value) {
return value.length <= this.maxLength;
};
}

0 comments on commit 005aefc

Please sign in to comment.