-
Notifications
You must be signed in to change notification settings - Fork 41
Lite: Fix number field validation appearing when min/max values are empty #2664
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
Open
shervElmi
wants to merge
8
commits into
master
Choose a base branch
from
fix-number-validation-popup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
97c02b5
Add proIsConnected flag to global admin JavaScript strings
shervElmi f3ce3a8
Add utility functions to get field ID and type from settings elements
shervElmi 18de437
Add validateField utility function for field validation and UI feedback
shervElmi f92f1a5
Add validateRangeSettings module with validation functions for number…
shervElmi 0df7555
Add validation for number range and step settings in form builder cha…
shervElmi d0944d6
Add formidable_admin.js file
shervElmi bf245ed
Reorganize validation functions into settings.validate namespace and …
shervElmi 43eed37
Add formidable_admin.js file
shervElmi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * Gets the field ID from the single settings element or the closest single settings element to the field. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement} singleSettings The single settings element. | ||
| * @param {HTMLElement} field The field element to get field ID from. | ||
| * | ||
| * @return {string|undefined} The field ID or undefined if not found. | ||
| */ | ||
| export const getFieldId = ( singleSettings = null, field = null ) => | ||
| singleSettings ? singleSettings.dataset.fid : field?.closest( '.frm-single-settings' )?.dataset.fid; | ||
|
|
||
| /** | ||
| * Gets the field type from the single settings element. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement} singleSettings The single settings element. | ||
| * @param {HTMLElement} field The field element. | ||
| * | ||
| * @return {string|undefined} The field type or undefined if not found. | ||
| */ | ||
| export const getFieldType = ( singleSettings = null, field = null ) => { | ||
| if ( ! singleSettings ) { | ||
| singleSettings = field?.closest( '.frm-single-settings' ); | ||
| } | ||
|
|
||
| return singleSettings?.className.match( /frm-type-(\w+)/ )?.[ 1 ]; | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * Runs validation and handles UI feedback. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement} field The field element being validated. | ||
| * @param {Function} getError Function that returns error message or empty string. | ||
| * | ||
| * @return {string} The error message or empty string. | ||
| */ | ||
| export function validateField( field, getError ) { | ||
| const errorMessage = getError(); | ||
| if ( errorMessage ) { | ||
| frmAdminBuild.infoModal( errorMessage ); | ||
| field.classList.add( 'frm_invalid_field' ); | ||
| } else { | ||
| field.classList.remove( 'frm_invalid_field' ); | ||
| } | ||
|
|
||
| return errorMessage; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { validateField } from './validateField'; | ||
| import { getFieldId, getFieldType } from './utils'; | ||
|
|
||
| /** | ||
| * Gets the default values for range settings validation. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement} singleSettings The single settings element. | ||
| * | ||
| * @return {Object} The defaults object with maxNum, minNum, and step. | ||
| */ | ||
| export function getRangeSettingsDefaults( singleSettings ) { | ||
| const fieldType = getFieldType( singleSettings ) || 'number'; | ||
| const defaultSettings = { | ||
| maxNum: 9999999, | ||
| minNum: 0, | ||
| step: 1 | ||
| }; | ||
|
|
||
| /** | ||
| * Filters the default values for range settings validation. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {Object} defaultSettings The default settings. | ||
| * @param {Object} context Additional context. | ||
| * @param {HTMLElement} context.singleSettings The single settings element. | ||
| * @param {string} context.fieldType The field type. | ||
| * | ||
| * @return {Object} The filtered default settings. | ||
| */ | ||
| return wp.hooks.applyFilters( 'frm_range_settings_defaults', defaultSettings, { singleSettings, fieldType } ); | ||
|
shervElmi marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Validates number range setting. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement} field The field element being validated. | ||
| */ | ||
| export function validateNumberRangeSetting( field ) { | ||
| if ( ! field.closest( '.frm-number-range' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| const singleSettings = field.closest( '.frm-single-settings' ); | ||
| const fieldId = getFieldId( singleSettings ); | ||
| if ( ! fieldId ) { | ||
| return; | ||
| } | ||
|
|
||
| const minValueInput = document.querySelector( `[name="field_options[minnum_${ fieldId }]"]` ); | ||
| if ( ! minValueInput ) { | ||
| return; | ||
| } | ||
|
|
||
| const maxValueInput = document.querySelector( `[name="field_options[maxnum_${ fieldId }]"]` ); | ||
| if ( ! maxValueInput ) { | ||
| return; | ||
| } | ||
|
|
||
| return validateField( field, () => { | ||
| const { minNum, maxNum } = getRangeSettingsDefaults( singleSettings ); | ||
|
|
||
| return parseFloat( minValueInput.value || minNum ) >= parseFloat( maxValueInput.value || maxNum ) | ||
| ? __( 'Minimum value cannot be greater than or equal to maximum value.', 'formidable' ) | ||
| : ''; | ||
| } ); | ||
| } | ||
|
|
||
| /** | ||
| * Validates step setting. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement} field The field element being validated. | ||
| */ | ||
| export function validateStepSetting( field ) { | ||
| if ( ! field.closest( '.frm-step' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| const singleSettings = field.closest( '.frm-single-settings' ); | ||
| const fieldId = getFieldId( singleSettings ); | ||
| if ( ! fieldId ) { | ||
| return; | ||
| } | ||
|
|
||
| const stepInput = document.querySelector( `[name="field_options[step_${ fieldId }]"]` ); | ||
| if ( ! stepInput ) { | ||
| return; | ||
| } | ||
|
|
||
| return validateField( field, () => { | ||
| const { step, maxNum } = getRangeSettingsDefaults( singleSettings ); | ||
| const stepInputValue = parseFloat( stepInput.value || step ); | ||
| if ( stepInputValue <= 0 ) { | ||
| return __( 'Step value must be greater than 0.', 'formidable' ); | ||
| } | ||
|
|
||
| const maxValueInput = document.querySelector( `[name="field_options[maxnum_${ fieldId }]"]` ); | ||
| if ( ! maxValueInput ) { | ||
| return ''; | ||
| } | ||
|
|
||
| return stepInputValue > parseFloat( maxValueInput.value || maxNum ) | ||
| ? __( 'Step value must be less than maximum value.', 'formidable' ) | ||
| : ''; | ||
| } ); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.