Skip to content

Instantly share code, notes, and snippets.

@edg-l
Last active February 4, 2018 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edg-l/c87d772d573c9a03f475717926fe87a4 to your computer and use it in GitHub Desktop.
Save edg-l/c87d772d573c9a03f475717926fe87a4 to your computer and use it in GitHub Desktop.
/*
Copyright 2018 Ryozuki
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* This is a generic bulma form handler.
* How to use:
* - The inputs must have a id like: #input-{name}
* - The inputs must have a help paragraph with a id like: #help-{name}
*/
class BulmaFormHandler extends FormHandler {
/**
* Turns the input green and removes the help message.
* @param {*} field
*/
turnGreen (field) {
// If the DOM has the danger class, remove it.
let input = $(`input[name=${field}]`)
let help = $('#help-' + field)
if (input.hasClass('is-danger')) {
input.toggleClass('is-danger')
}
// If the DOM doesn't have the success class, add it.
if (!input.hasClass('is-success')) {
input.toggleClass('is-success')
}
// Clean the help text.
help.text('')
}
handleErrors (errors, prefix = '') {
for (let fieldName in errors) {
let err = errors[fieldName]
let help = $('#help-' + prefix + fieldName)
// let input = $('#input-' + prefix + fieldName)
let input = $(`input[name=${fieldName}]`)
// Check if exists
if (!input.length) {
continue
}
help.text(err.message)
if (!input.hasClass('is-danger')) {
input.toggleClass('is-danger')
}
}
// Check if there is no error, then make it green
let data = this.objectifyForm()
// Get the first fields from the error list
let fields = Object.keys(errors)
for (let obj in data) {
if (!fields.includes(obj)) {
this.turnGreen(prefix + obj)
}
}
}
turnAllGreen () {
let data = this.objectifyForm()
for (let obj in data) {
this.turnGreen(obj)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment