Last active
January 5, 2021 05:19
-
-
Save sukima/c4a253e136c548d20f56d5df045d26eb to your computer and use it in GitHub Desktop.
Validatable form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
import { action } from '@ember/object'; | |
export default class extends Component { | |
@action | |
validatedSubmit(event) { | |
event.preventDefault(); | |
let { target: form } = event; | |
let validateEvent = new CustomEvent('validate'); | |
for (let field of form.elements) { | |
field.dispatchEvent(validateEvent); | |
} | |
if (form.checkValidity()) { | |
this.args.onSubmit?.(new FormData(form)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
import { action } from '@ember/object'; | |
import { guidFor } from '@ember/object/internals'; | |
import { tracked } from '@glimmer/tracking'; | |
export default class extends Component { | |
fieldId = `${guidFor(this)}-field`; | |
@tracked hasValidated = false; | |
@tracked validationMessage = ''; | |
get inputElement() { | |
return document.getElementById(this.fieldId); | |
} | |
@action | |
validate() { | |
let { validate = () => [] } = this.args; | |
let [error = ''] = validate(this.inputElement); | |
this.inputElement.checkValidity(); | |
this.inputElement.setCustomValidity(error); | |
this.validationMessage = this.inputElement.validationMessage; | |
this.hasValidated = true; | |
} | |
@action | |
validityCheck({ type, target: { value } }) { | |
let checkableTypes = this.args.eager | |
? ['change', 'blur', 'input'] | |
: ['change']; | |
if (checkableTypes.includes(type)) { | |
this.validate(); | |
} | |
this.args.onUpdate?.(value); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
import { action } from '@ember/object'; | |
import FormData from '../utils/form-data'; | |
export default class ApplicationController extends Controller { | |
form = new FormData(); | |
@action | |
fakeSubmit(formData) { | |
alert([...formData.values()].join(', ')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function composeValidators(validators) { | |
return (element) => validators | |
.map(i => i(element)) | |
.reduce((a, b) => [...a, ...b], []); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function validateCapitalized() { | |
return ({ value }) => /^[A-Z][a-z]*$/.test(value) | |
? [] | |
: ['Name must be titleized.']; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function validateName([name]) { | |
return ({ value }) => value.toLowerCase() !== name.toLowerCase() | |
? [`Only '${name}' will work.`] | |
: []; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function validatePhone() { | |
return ({ value }) => /\d\d\d-\d\d\d-\d\d\d\d/.test(value) | |
? [] | |
: ['Phone number must be of format xxx-xxx-xxxx.']; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data:empty::before { | |
content: '–'; | |
} | |
form > * + * { | |
margin-top: 0.5rem; | |
} | |
fieldset { | |
display: grid; | |
grid-template-columns: repeat(3, 1fr); | |
grid-gap: 1rem; | |
} | |
[data-validated] :valid { | |
box-shadow: green 0px 0px 1.5px 1px; | |
} | |
:invalid + .validation-message::before { | |
content: attr(data-message); | |
color: red; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { tracked } from '@glimmer/tracking'; | |
export default class FormData { | |
@tracked janeName; | |
@tracked danName; | |
@tracked phoneNative; | |
@tracked phoneCustom; | |
update = new Proxy(this, { | |
get: (t, p) => Reflect.has(t, p) ? v => t[p] = v : undefined | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment