fix: add URL validation and error feedback for Tool Icon Source#6394
fix: add URL validation and error feedback for Tool Icon Source#6394xxiaoxiong wants to merge 1 commit into
Conversation
Fixes FlowiseAI#6382 Changes: - Add URL validation for Tool Icon Source field - Display error message when invalid URL is entered - Block saving when Tool Icon Source contains invalid URL - Allow empty Tool Icon Source (optional field) - Validate on input change for immediate feedback - Clear error state when dialog is reset The validation ensures: - Empty values are allowed (optional field) - Only http:// and https:// URLs are accepted - Clear error messages guide users to correct format - Saving is prevented until validation passes
There was a problem hiding this comment.
Code Review
This pull request introduces URL validation for the tool icon in the ToolDialog component, including error state management and UI feedback. The feedback suggests optimizing performance by wrapping the validation and change handlers in useCallback and ensuring the error state is reset across all dialog modes (ADD, EDIT, IMPORT, and TEMPLATE) to prevent stale error messages.
| const validateToolIconUrl = (url) => { | ||
| if (!url || url.trim() === '') { | ||
| setToolIconError('') | ||
| return true | ||
| } | ||
| try { | ||
| const urlObj = new URL(url) | ||
| if (urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') { | ||
| setToolIconError('Tool Icon Source must be a valid http or https URL') | ||
| return false | ||
| } | ||
| setToolIconError('') | ||
| return true | ||
| } catch (error) { | ||
| setToolIconError('Tool Icon Source must be a valid URL') | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| const handleToolIconChange = (e) => { | ||
| const value = e.target.value | ||
| setToolIcon(value) | ||
| validateToolIconUrl(value) | ||
| } |
There was a problem hiding this comment.
The validation and change handler functions are recreated on every render. Wrapping them in useCallback ensures stable function references, which is a best practice in React to prevent unnecessary re-renders of child components (like the OutlinedInput) and to maintain consistency when these functions are used as dependencies in other hooks or passed as props.
const validateToolIconUrl = useCallback((url) => {
if (!url || url.trim() === '') {
setToolIconError('')
return true
}
try {
const urlObj = new URL(url)
if (urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') {
setToolIconError('Tool Icon Source must be a valid http or https URL')
return false
}
setToolIconError('')
return true
} catch (error) {
setToolIconError('Tool Icon Source must be a valid URL')
return false
}
}, [])
const handleToolIconChange = useCallback((e) => {
const value = e.target.value
setToolIcon(value)
validateToolIconUrl(value)
}, [validateToolIconUrl])| setToolName('') | ||
| setToolDesc('') | ||
| setToolIcon('') | ||
| setToolIconError('') |
There was a problem hiding this comment.
Description
Fixes #6382
Adds URL validation and error feedback for the Tool Icon Source field in the Tools dialog.
Changes
Validation Rules
http://andhttps://URLs are acceptedTesting
Before
abc123)After
Screenshots
Error feedback for invalid URL:
abc123shows: "Tool Icon Source must be a valid URL"ftp://example.comshows: "Tool Icon Source must be a valid http or https URL"Valid cases:
https://example.com/icon.svg: ✅ No error, save allowedType of Change
Checklist