feature/field-error
Björn 4 years ago
parent 34898a8bd1
commit 0b10fb9cb5

File diff suppressed because one or more lines are too long

@ -74,19 +74,17 @@ class FormValidator
*/ */
onFieldUpdate(event) onFieldUpdate(event)
{ {
// if value is a empty string make him undefined // workaround, make sure that value for single is undefined if it is empty
if (event.detail.value == '') { if (event.detail.value == '') {
event.detail.value = undefined event.detail.value = undefined
} }
let errors = validate.single(event.detail.value, this.constraits[event.detail.name]), let errors = validate.single(event.detail.value, this.constraits[event.detail.name])
element
// search for element by name and dispatch event // search for element by name and dispatch event
Array.from(this.elements).every((e) => { this.elements.forEach((element) => {
if (e.attributes.name.nodeValue == event.detail.name) { if (element.attributes.name.nodeValue == event.detail.name) {
this.dispatchCustomEvent(errors, e) this.dispatchCustomEvent(errors, element)
return false
} }
}) })
} }

@ -1,37 +0,0 @@
<demo>
<form onsubmit={ (event) => { state.validator.handle(event) }} novalidate>
<div class="field">
<label>
email
<input type="email" name="email" onkeyup={ (event) => { state.validator.handle(event, 'email') }} />
</label>
<field-error errors={ state.validator.errors('email') } ></field-error>
</div>
<div class="field">
<label>
password
<input type="password" name="email" onkeyup={ (event) => { state.validator.handle(event, 'password') }} />
</label>
<field-error errors={ state.validator.errors('password') } ></field-error>
</div>
<button type="submit">Send</button>
</form>
<script>
import Validator from './../validator'
export default {
onBeforeMount() {
this.state.validator = new Validator({
email: {
presence: true,
email: true
},
password: {
presence: true
}
}, this)
}
}
</script>
</demo>
Loading…
Cancel
Save