Skip to content

Commit

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


export default Ember.Component.extend({
username: TextInput.create({
value: '',
validators: [new MaxLength(30)]
validators: [new MaxLength(30), new MinLength(6)]
}),
email: '',
password: '',
Expand All @@ -16,8 +16,7 @@ export default Ember.Component.extend({

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

Expand Down
19 changes: 15 additions & 4 deletions openchat/app/templates/components/register-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
name="username" aria-describedby="usernameHelpBlock" key-up=(action 'onUsernameChange') }}

{{#if username.errors}}
<div class="alert alert-danger">
<div>
Username is required.
{{#if username.errors.maxLength}}
<div class="alert alert-danger">
<div>
Username must contain max 30 characters.
</div>
</div>
</div>
{{/if}}

{{#if username.errors.minLength}}
<div class="alert alert-danger">
<div>
Username must contain min 6 characters.
</div>
</div>
{{/if}}

{{/if}}

</div>
Expand Down
16 changes: 13 additions & 3 deletions openchat/app/utils/text-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Ember from 'ember';
export const TextInput = Ember.Object.extend({
init() {
this._super(...arguments);
this.set('errors', {});
this.set('errors', false);
},

validate() {
Expand All @@ -16,8 +16,10 @@ export const TextInput = Ember.Object.extend({
let validator = validators[i];

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

Expand All @@ -26,7 +28,6 @@ export const TextInput = Ember.Object.extend({
}

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

Expand All @@ -38,3 +39,12 @@ export function MaxLength(maxLength) {
return value.length <= this.maxLength;
};
}

export function MinLength(minLength) {
this.validatorName = 'minLength';
this.minLength = minLength;

this.isValid = function(value) {
return value.length >= this.minLength;
};
}

0 comments on commit 090104a

Please sign in to comment.