-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Input Fields validation message #438
base: v2-dev
Are you sure you want to change the base?
Changes from all commits
d01ba2a
2492cf2
db380cf
1940d63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
import { Utils } from "./utils"; | ||
|
||
export class Forms { | ||
/** | ||
* Checks if the label has validation and apply | ||
* the correct class and styles | ||
* @params textarea Textarea to be checked | ||
*/ | ||
static validate_field(textarea: HTMLInputElement) { | ||
// Checks if textarea is exist | ||
if (!textarea) { | ||
console.error('No textarea element found'); | ||
return; | ||
} | ||
|
||
let hasLength = textarea.getAttribute('data-length') !== null; | ||
let lenAttr = parseInt(textarea.getAttribute('data-length')); | ||
let len = textarea.value.length; | ||
|
||
if (len === 0 && textarea.validity.badInput === false && !textarea.required ) { | ||
if (textarea.classList.contains('validate')) { | ||
textarea.classList.remove('valid'); | ||
textarea.classList.remove('invalid'); | ||
} | ||
} else { | ||
|
||
if (textarea.classList.contains('validate')) { | ||
// Check for character counter attributes | ||
if (((textarea.validity.valid) && hasLength && len <= lenAttr) || textarea.validity.valid && !hasLength) { | ||
textarea.classList.remove('invalid'); | ||
textarea.classList.add('valid'); | ||
} else { | ||
textarea.classList.remove('valid'); | ||
textarea.classList.add('invalid'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Resizes the given TextArea after updating the | ||
|
@@ -81,6 +116,20 @@ export class Forms { | |
static Init(){ | ||
document.addEventListener("DOMContentLoaded", () => { | ||
|
||
document.addEventListener('change', (e: KeyboardEvent) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we attach the validate Function to the input field directely? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah kinda, here I find kinda wierd checking for Referencing here from v1.0.0:
Since the textbox are now being handled by CSS, I think we can drop the length check. Again, I need to confirm the CSS behavior again. |
||
const target = <HTMLInputElement>e.target; | ||
if (target instanceof HTMLInputElement) { | ||
if (target.value.length !== 0 || target.getAttribute('placeholder') !== null) { | ||
for (const child of target.parentNode.children) { | ||
if (child.tagName == "label") { | ||
child.classList.add("active"); | ||
} | ||
} | ||
} | ||
Forms.validate_field(target); | ||
} | ||
}) | ||
|
||
document.addEventListener('keyup', (e: KeyboardEvent) => { | ||
const target = <HTMLInputElement>e.target; | ||
// Radio and Checkbox focus class | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be sufficient to check weather or not the text are has a "data-length" Attribute?
If it has, it indicates that the user wants the textarea to be validated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that could work. I couldn't find anything referencing the
validate
attribute other than this part. As I stated, this is the code from previous version of it. Not sure if it could be impacting anything else so far.Referencing here from v1.0.0:
From the code above, this seems linked to the textbox that have character count. I might have to recheck the CSS behavior.