Repository navigation
Show min/max messages for number fields on blur #3730
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
base: master
Are you sure you want to change the base?
Changes from all commits
1efdf84
768075d
bf3f239
a6ca0f2
9a3c5cd
a646ef2
fe042df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { readFileSync } from 'node:fs'; | ||
| import { test } from 'node:test'; | ||
| import { runInNewContext } from 'node:vm'; | ||
|
|
||
| const source = readFileSync( new URL( '../../js/formidable.js', import.meta.url ), 'utf8' ); | ||
|
|
||
| /** | ||
| * Run JavaScript validation for a form holding one number field. | ||
| * | ||
| * @param {string} value The typed value. | ||
| * @param {Object} validity The field's ValidityState flags. | ||
| * @param {Object} attrs The field's data attributes. | ||
| * @return {string|undefined} The error for the field. | ||
| */ | ||
| function getNumberErrors( value, validity, attrs ) { | ||
| const field = { | ||
| type: 'number', | ||
| name: 'item_meta[12]', | ||
| value, | ||
| validity: { valid: ! Object.values( validity ).includes( true ), ...validity }, | ||
| pattern: null, | ||
| classList: { contains: () => false }, | ||
| getAttribute: name => attrs[ name ] ?? null | ||
| }; | ||
| const form = { | ||
| classList: { contains: () => false }, | ||
| querySelector: () => null, | ||
| querySelectorAll: selector => selector === 'input,select,textarea' ? [ field ] : [] | ||
| }; | ||
| function jQuery() { | ||
| return { ready() {}, hasClass: () => true, querySelectorAll: () => [] }; | ||
| } | ||
| const context = { | ||
| window: {}, | ||
| document: { body: { classList: { contains: () => false } }, dispatchEvent() {} }, | ||
|
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.
|
||
| jQuery, | ||
| CustomEvent() {} | ||
|
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.
|
||
| }; | ||
|
|
||
| runInNewContext( source, context ); | ||
| context.frmFrontForm = context.window.frmFrontForm; | ||
|
|
||
| return context.window.frmFrontForm.getAjaxFormErrors( form )[ 12 ]; | ||
| } | ||
|
|
||
| const attrs = { | ||
| 'data-invmsg': 'Enter a number', | ||
| 'data-minmsg': 'Please select a higher number', | ||
| 'data-maxmsg': 'Please select a lower number' | ||
| }; | ||
|
|
||
| test( 'A number below the minimum shows the minimum message', () => { | ||
| assert.equal( getNumberErrors( '1', { rangeUnderflow: true }, attrs ), 'Please select a higher number' ); | ||
| } ); | ||
|
|
||
| test( 'A number above the maximum shows the maximum message', () => { | ||
| assert.equal( getNumberErrors( '5000', { rangeOverflow: true }, attrs ), 'Please select a lower number' ); | ||
| } ); | ||
|
|
||
| test( 'A number within range has no error', () => { | ||
| assert.equal( getNumberErrors( '10', {}, attrs ), undefined ); | ||
|
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.
|
||
| } ); | ||
|
|
||
| test( 'A range failure without range messages falls back to the invalid message', () => { | ||
| assert.equal( getNumberErrors( '1', { rangeUnderflow: true }, { 'data-invmsg': 'Enter a number' } ), 'Enter a number' ); | ||
| } ); | ||
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.
Having empty functions hurts readability, and is considered a code-smell. There's almost always a way to avoid using them. If you must use one, consider adding a comment to inform the reader of its purpose.