Skip to content

Commit

Permalink
Numericality
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed Sep 1, 2016
1 parent 0f1445e commit 17e46bb
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tests/dummy/app/controllers/validators/numericality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import BaseContoller from './base';

export default BaseContoller.extend({
modelName: 'for-numericality'
});
44 changes: 44 additions & 0 deletions tests/dummy/app/models/specifics/for-numericality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import DS from 'ember-data';
import Validator from '../../mixins/model-validator';

export default DS.Model.extend(Validator,{
anInteger: DS.attr('number'),
anIntegerLessThan4: DS.attr('number'),
anIntegerGreaterThan4: DS.attr('number'),
anIntegerGreaterThanOrEqual7: DS.attr('number'),
anIntegerLessThanOrEqual6: DS.attr('number'),
aTenNumber: DS.attr('number'),
anOddNumber: DS.attr('number'),
anEvenNumber: DS.attr('number'),
anOptionalNumber: DS.attr('number'),

validations: {
anInteger:{
numericality: {onlyInteger: true }
},
anIntegerLessThan4:{
numericality: {lessThan: 4}
},
anIntegerGreaterThan4:{
numericality: {greaterThan: 4}
},
anIntegerGreaterThanOrEqual7:{
numericality: {greaterThanOrEqualTo: 7}
},
anIntegerLessThanOrEqual6:{
numericality: {lessThanOrEqualTo: 6}
},
aTenNumber:{
numericality: {equalTo: 10}
},
anOddNumber:{
numericality: {odd: true}
},
anEvenNumber:{
numericality: {even: true}
},
anOptionalNumber:{
numericality: {onlyInteger: true, allowBlank: true}
}
}
});
2 changes: 1 addition & 1 deletion tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Router.map(function() {
this.route('exclusion');
this.route('match');
this.route('date');

this.route('numericality');
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/templates/_menu.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
{{#link-to 'validators.exclusion' class='item'}}Exclusion{{/link-to}}
{{#link-to 'validators.match' class='item'}}Match{{/link-to}}
{{#link-to 'validators.date' class='item'}}Date{{/link-to}}
{{#link-to 'validators.numericality' class='item'}}Numericality{{/link-to}}
189 changes: 189 additions & 0 deletions tests/dummy/app/templates/validators/numericality.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<div class="ui masthead vertical segment">
<div class="ui container">
<div class="introduction">

<h1 class="ui header">
Numericality

<div class="sub header">
Validates the attributes' values by testing whether it is specifc numeric.
</div>
</h1>

<div class="ui hidden divider"></div>

</div>

</div>
</div>

<div class="ui main container">
<p>
<h3>Specific Options</h3>
<ul>
<li><code class="ui label">onlyInteger</code>. The value must be an integer.</li>
<li><code class="ui label">greaterThan</code>. The value must be greater than the supplied value.</li>
<li><code class="ui label">greaterThanOrEqualTo</code>. The value must be greater or equal to the supplied value.</li>
<li><code class="ui label">equalTo</code>. The value must be equal to the supplied value.</li>
<li><code class="ui label">lessThan</code>. The value must be less than the supplied value.</li>
<li><code class="ui label">lessThanOrEqualTo</code>. The value must be less or equal to the supplied value.</li>
<li><code class="ui label">odd</code>. The value must be odd.</li>
<li><code class="ui label">even</code>. The value must be even.</li>
</ul>

{{#ui-annotation showing=true type="javascript"}}
validations: {
anInteger:{
numericality: {onlyInteger: true }
},
anIntegerLessThan4:{
numericality: {lessThan: 4}
},
anIntegerGreaterThan4:{
numericality: {greaterThan: 4}
},
anIntegerGreaterThanOrEqual7:{
numericality: {greaterThanOrEqualTo: 7}
},
anIntegerLessThanOrEqual6:{
numericality: {lessThanOrEqualTo: 6}
},
aTenNumber:{
numericality: {equalTo: 10}
},
anOddNumber:{
numericality: {odd: true}
},
anEvenNumber:{
numericality: {even: true}
},
anOptionalNumber:{
numericality: {onlyInteger: true, allowBlank: true}
}
}
{{/ui-annotation}}
</p>

{{config-options defaultMessage=messages.wrongLengthMessage}}

{{#ui-example
header="Examples"
as |showing setCopyCode|}}

{{#ui-html showing=showing}}
<div class="ui two column grid">
<div class="column">
<h4>The Numericality Party</h4>
{{#object-form for=model classNames="segment"}}

<div class="four fields">
<div class="field">
{{#form-field for='anInteger' label="An integer" required=true}}
{{input value=model.anInteger placeholder="An integer"}}
{{/form-field}}
</div>
<div class="field">
{{#form-field for='anIntegerLessThan4' label="Less than 4" required=true}}
{{input value=model.anIntegerLessThan4 placeholder="Less than 4"}}
{{/form-field}}
</div>
<div class="field">
{{#form-field for='anIntegerGreaterThan4' label="Greater than 4" required=true}}
{{input value=model.anIntegerGreaterThan4 placeholder="Greater than 4"}}
{{/form-field}}
</div>
<div class="field">
{{#form-field for='aTenNumber' label="A ten" required=true}}
{{input value=model.aTenNumber placeholder="A ten"}}
{{/form-field}}
</div>
</div>

<div class="three fields">
<div class="field">
{{#form-field for='anOddNumber' label="Odd Number" required=true}}
{{input value=model.anOddNumber placeholder="Odd Number"}}
{{/form-field}}
</div>
<div class="field">
{{#form-field for='anEvenNumber' label="Even Number" required=true}}
{{input value=model.anEvenNumber placeholder="Even Number"}}
{{/form-field}}
</div>
<div class="field">
{{#form-field for='anOptionalNumber' label="Optional Number" required=true}}
{{input value=model.anOptionalNumber placeholder="Optional Number"}}
{{/form-field}}
</div>
</div>
<div class="two fields">
<div class="field">
{{#form-field for='anIntegerGreaterThanOrEqual7' label="Greater than or equal 7" required=true}}
{{input value=model.anIntegerGreaterThanOrEqual7 placeholder="Greater than or equal 7"}}
{{/form-field}}
</div>
<div class="field">
{{#form-field for='anIntegerLessThanOrEqual6' label="Less than or equal 6" required=true}}
{{input value=model.anIntegerLessThanOrEqual6 placeholder="Less than or equal 6"}}
{{/form-field}}
</div>
</div>
<div class="actions">
<div class="ui input submit">
<button class="ui button primary" {{action 'validate'}}>Validate</button>
<button class="ui button reset" {{action 'reset'}}>Reset</button>
</div>
</div>
{{/object-form}}

</div>
<div class="column">
<div class="ui segment">
{{model-errors-to-json errors=model.errors modelName=modelName}}
</div>
</div>
</div>
{{/ui-html}}

{{#ui-annotation showing=showing type="javascript" setCopyCode=setCopyCode}}
import DS from 'ember-data';
import Validator from '../mixins/model-validator';

export default DS.Model.extend(Validator,{
socialSecurity: DS.attr('string'),
nsaNumber: DS.attr('string'),
chuncaluchoNumber: DS.attr('string'),
hugeName: DS.attr('string'),
smallName: DS.attr('string'),

validations: {
socialSecurity: {
length: 5
},
nsaNumber: {
length: [3, 5]
},
chuncaluchoNumber: {
length: { is: 10, message: 'this is not the length of a chuncalucho' }
},
hugeName:{
length: {
minimum: 3,
maximum: 5
}
},
smallName:{
length: {
minimum: 1,
maximum: {
value: 2,
message: 'should be smaller'
}
}
}
}
});
{{/ui-annotation}}

{{/ui-example}}
</div>

0 comments on commit 17e46bb

Please sign in to comment.