You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
validator/README.md

88 lines
2.1 KiB

3 years ago
# Tiny Components - Validator
4 years ago
Created with [Riot.js](https://riot.js.org)
Validate Form or a Single Form-Field, Error Messages can be show just in time
or after Submit entire Form.
For Validation this Component uses [Validate.js](https://validatejs.org/)
## Install
```bash
3 years ago
npm install @tiny-components/validator --save
4 years ago
```
3 years ago
## You can use it like this
4 years ago
4 years ago
```javascript
3 years ago
<form class="form" onsubmit={ (event) => ( state.validator.submit(event) ) }>>
4 years ago
<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.js'
export default {
3 years ago
state: {
validator: { }
},
4 years ago
onBeforeMount() {
3 years ago
// creating formValidator
this.state.validator = new FormValidator(this.$('.form'), {
'email': {
'presence': true,
'email': true
4 years ago
},
3 years ago
'password': {
'presence': true
4 years ago
}
3 years ago
})
// adding on success
this.state.validator.onSuccess((event, data) => {
this.handleSuccess(event, data)
})
// adding on error
this.state.validator.onError((event, errors, data) => {
this.handleError(event, errors, data)
})
},
/**
*
*/
handleSuccess(event, data)
{
event.preventDefault()
this.update()
},
/**
*
*/
handleError(event, errors, data)
{
this.update()
4 years ago
}
3 years ago
4 years ago
}
</script>
```