Skip to content

Commit

Permalink
Make validation more sensible
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuwen Qian authored and anthonykoerber committed Mar 2, 2016
1 parent 5c643f1 commit 4daeda0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/mm-repeater/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,11 @@
float: left;
}

#container {
max-width: 680px !important;
}
</style>
</head>
<body>
<div id='container'>
<!-- <mm-repeater id="repeater">
<div id='container' class="col c4">
<mm-repeater id="repeater">
<template preserve-content>
<mm-input name="name"></mm-input>
<mm-dropdown name="firstOption">
Expand All @@ -110,11 +107,11 @@
<mm-radio><label>Test 3</label></mm-radio>
</mm-group>
</template>
</mm-repeater> -->
</mm-repeater>

<hr>
<hr style="margin-bottom: 30px;">

<h3>Prepopulated</h3>
<mm-header>Prepopulated</mm-header>
<mm-repeater id="withData">
<template preserve-content>
<mm-input name="name" validation="alpha"></mm-input>
Expand All @@ -131,15 +128,21 @@ <h3>Prepopulated</h3>
</mm-group>
</template>
</mm-repeater>
<mm-button id="validateBtn"><label>Validate!</label></mm-button>
<mm-button style="margin: 10px 0;" id="validateBtn"><label>Validate!</label></mm-button>
</div>

<script>
var data = [
{
name: "John",
firstOption: "Test Item 2",
secondOption: "Test 1"
secondOption: "Test 1",
validation: function(model) {
console.log(arguments);
this.errorMessage = 'Setting error message in validation method';
return model.name === 'John';
},
errorMessage: "Name is not John!"
},
{
name: "Paul",
Expand Down
6 changes: 6 additions & 0 deletions src/mm-repeater/mm-repeater.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<link rel="import" href="../mm-action/mm-action.html"/>
<link rel="import" href="../mm-box/mm-box.html"/>
<link rel="import" href="../mm-icon/mm-icon.html"/>
<link rel="import" href="../mm-inline-box/mm-inline-box.html"/>

<dom-module id="mm-repeater">
<link rel="import" type="css" href="mm-repeater.css"/>
Expand All @@ -24,6 +25,11 @@
<mm-icon type="delete" on-tap="_removeRow" height="12" width="12"></mm-icon>
</mm-box>
</mm-box>
<template is="dom-if" if="{{item.error}}">
<mm-inline-box type="error">
{{item.errorMessage}}
</mm-inline-box>
</template>
</template>
</template>
</dom-module>
Expand Down
15 changes: 10 additions & 5 deletions src/mm-repeater/mm-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

behaviors: [
StrandTraits.Refable,
StrandTraits.Resolvable,
StrandTraits.Validatable
StrandTraits.Resolvable
],

get value() {
Expand Down Expand Up @@ -99,8 +98,14 @@
if(typeof item.validation === 'function') {
// Custom validation provided: call validation, passing name:value pairs as arguments
var elems = item._ref.querySelectorAll('[name]'),
rowData = Object.keys(elems).map(function(key) { return {name: elems[key].name, value: elems[key].value }; });
valid = item.validation.apply(rowData);
rowData = new Object();
Object.keys(elems).forEach(
function(key) {
var elem = elems[key];
rowData[elem.getAttribute('name')] = elem.value;
}
);
valid = item.validation.call(item, rowData);
} else {
// Default validation: call validate on each form element and fold them together
var fields = item._ref.querySelectorAll('[validation]');
Expand All @@ -111,6 +116,7 @@

// Reflect validation to the model for error messaging
this.set('data.'+index+'.error', !valid);
this.set('data.'+index+'.errorMessage', item.errorMessage);

}, this);
},
Expand All @@ -120,7 +126,6 @@
name = target.name || target.getAttribute('name'),
value = target.value || target.getAttribute('value');

console.log('_updateModel triggered');
if(name && value) {
var index = this.$.repeater.indexForElement(target);
this.set('data.'+(index)+'.'+name, value);
Expand Down
2 changes: 1 addition & 1 deletion src/mm-repeater/mm-repeater.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ mm-icon[type="delete"] {

mm-inline-box[type="error"] {
display: block;
margin-bottom: 8px;
margin: 8px 0;
width: 100%;
}

0 comments on commit 4daeda0

Please sign in to comment.