-
Notifications
You must be signed in to change notification settings - Fork 1
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
Validation Fixes #649
base: develop
Are you sure you want to change the base?
Validation Fixes #649
Conversation
WalkthroughThe pull request modifies the Changes
Suggested labels
Suggested reviewers
Poem
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
wp-content/plugins/civicrm/civicrm/ext/afform/core/ang/af/afForm.component.js (1)
338-371
: Consider splitting validation logic into separate functionsThe
customValidateFields
function has grown to handle multiple types of validations (email, phone, dates, postal code). This violates the Single Responsibility Principle and makes the code harder to maintain.Consider refactoring into separate validation functions:
function validateEmail(element) { // Email validation logic } function validatePhone(element) { // Phone validation logic } function validateCollectionCampDates(element) { // Collection camp dates validation logic } function validatePostalCode(element) { // Postal code validation logic } function customValidateFields() { const validators = [ validateEmail, validatePhone, validateCollectionCampDates, validatePostalCode ]; const results = validators .map(validator => validator($element)) .filter(result => !result.isValid); if (results.length > 0) { CRM.alert(results.map(r => r.message).join('\n'), ts("Form Error")); return false; } return true; }This approach would:
- Make the code more maintainable
- Make it easier to add new validations
- Make it easier to test individual validation rules
- Improve code reusability
🧰 Tools
🪛 Biome (1.9.4)
[error] 343-343: Shouldn't redeclare 'today'. Consider to delete it or rename it.
'today' is defined here:
(lint/suspicious/noRedeclare)
[error] 350-350: Shouldn't redeclare 'errorMessage'. Consider to delete it or rename it.
'errorMessage' is defined here:
(lint/suspicious/noRedeclare)
[error] 351-351: Shouldn't redeclare 'isValid'. Consider to delete it or rename it.
'isValid' is defined here:
(lint/suspicious/noRedeclare)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
wp-content/plugins/civicrm/civicrm/ext/afform/core/ang/af/afForm.component.js
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
wp-content/plugins/civicrm/civicrm/ext/afform/core/ang/af/afForm.component.js
[error] 343-343: Shouldn't redeclare 'today'. Consider to delete it or rename it.
'today' is defined here:
(lint/suspicious/noRedeclare)
[error] 350-350: Shouldn't redeclare 'errorMessage'. Consider to delete it or rename it.
'errorMessage' is defined here:
(lint/suspicious/noRedeclare)
[error] 351-351: Shouldn't redeclare 'isValid'. Consider to delete it or rename it.
'isValid' is defined here:
(lint/suspicious/noRedeclare)
if (ctrl.getFormMeta().name === 'afformCollectionCampIntentDetails') { | ||
var startDateValue = $element.find("af-field[name='Collection_Camp_Intent_Details.Start_Date'] .crm-form-date-wrapper input.crm-form-date").val(); | ||
var endDateValue = $element.find("af-field[name='Collection_Camp_Intent_Details.End_Date'] .crm-form-date-wrapper input.crm-form-date").val(); | ||
|
||
var institutionStartDateValue = $element.find("af-field[name='Institution_Collection_Camp_Intent.Collections_will_start_on_Date_'] .crm-form-date-wrapper input.crm-form-date").val(); | ||
var institutionEndDateValue = $element.find("af-field[name='Institution_Collection_Camp_Intent.Collections_will_end_on_Date_'] .crm-form-date-wrapper input.crm-form-date").val(); | ||
|
||
if ((startDateValue && endDateValue) || (institutionStartDateValue && institutionEndDateValue)) { | ||
|
||
if (startDateValue && endDateValue) { | ||
var today = new Date(); | ||
today.setHours(0, 0, 0, 0); | ||
|
||
var startDateParts = startDateValue ? startDateValue.split('/') : institutionStartDateValue.split('/'); | ||
var endDateParts = endDateValue ? endDateValue.split('/') : institutionEndDateValue.split('/'); | ||
|
||
var startDateParts = startDateValue.split('/'); | ||
var endDateParts = endDateValue.split('/'); | ||
var startDate = new Date(startDateParts[2], startDateParts[1] - 1, startDateParts[0]); | ||
var endDate = new Date(endDateParts[2], endDateParts[1] - 1, endDateParts[0]); | ||
|
||
var errorMessage = ''; | ||
var isValid = true; | ||
|
||
// Check if the start date is in the past or today | ||
if (startDate <= today) { | ||
errorMessage += `Collections cannot start (${startDateValue || institutionStartDateValue}) today or in the past.\n`; | ||
errorMessage += `Collections cannot start (${startDateValue}) today or in the past.\n`; | ||
isValid = false; | ||
} | ||
|
||
// Check if the end date is in the past, today | ||
if (endDate <= today) { | ||
errorMessage += `Collections cannot end (${endDateValue || institutionEndDateValue}) today or in the past.\n`; | ||
errorMessage += `Collections cannot end ( ${endDateValue}) today or in the past.\n`; | ||
isValid = false; | ||
|
||
} | ||
|
||
// Check if End Date is before Start Date | ||
if (endDate < startDate) { | ||
errorMessage += `Collections cannot end (${endDateValue || institutionEndDateValue}) before start (${startDateValue || institutionStartDateValue}).\n`; | ||
errorMessage += `Collections cannot end (${endDateValue}) before start (${startDateValue}).\n`; | ||
isValid = false; | ||
} | ||
} | ||
} | ||
|
||
} |
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.
🛠️ Refactor suggestion
Refactor validation logic to improve maintainability and reduce duplication
The collection camp date validation logic has several issues that should be addressed:
- Variable redeclarations
- Repeated date parsing logic
- Hard-coded DOM selectors
- No input sanitization
Here's a suggested refactor:
- if (ctrl.getFormMeta().name === 'afformCollectionCampIntentDetails') {
- var startDateValue = $element.find("af-field[name='Collection_Camp_Intent_Details.Start_Date'] .crm-form-date-wrapper input.crm-form-date").val();
- var endDateValue = $element.find("af-field[name='Collection_Camp_Intent_Details.End_Date'] .crm-form-date-wrapper input.crm-form-date").val();
-
- if (startDateValue && endDateValue) {
- var today = new Date();
- today.setHours(0, 0, 0, 0);
- var startDateParts = startDateValue.split('/');
- var endDateParts = endDateValue.split('/');
- var startDate = new Date(startDateParts[2], startDateParts[1] - 1, startDateParts[0]);
- var endDate = new Date(endDateParts[2], endDateParts[1] - 1, endDateParts[0]);
-
- var errorMessage = '';
- var isValid = true;
+ if (ctrl.getFormMeta().name === 'afformCollectionCampIntentDetails') {
+ const SELECTORS = {
+ startDate: "af-field[name='Collection_Camp_Intent_Details.Start_Date'] .crm-form-date-wrapper input.crm-form-date",
+ endDate: "af-field[name='Collection_Camp_Intent_Details.End_Date'] .crm-form-date-wrapper input.crm-form-date"
+ };
+
+ const parseDate = (dateStr) => {
+ if (!dateStr || !/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateStr)) return null;
+ const [day, month, year] = dateStr.split('/');
+ return new Date(year, month - 1, day);
+ };
+
+ const startDateValue = $element.find(SELECTORS.startDate).val();
+ const endDateValue = $element.find(SELECTORS.endDate).val();
+
+ if (startDateValue && endDateValue) {
+ const today = new Date();
+ today.setHours(0, 0, 0, 0);
+
+ const startDate = parseDate(startDateValue);
+ const endDate = parseDate(endDateValue);
+
+ if (!startDate || !endDate) {
+ return {
+ isValid: false,
+ message: 'Invalid date format. Please use DD/MM/YYYY format.'
+ };
+ }
- // Check if the start date is in the past or today
- if (startDate <= today) {
- errorMessage += `Collections cannot start (${startDateValue}) today or in the past.\n`;
- isValid = false;
- }
-
- // Check if the end date is in the past, today
- if (endDate <= today) {
- errorMessage += `Collections cannot end ( ${endDateValue}) today or in the past.\n`;
- isValid = false;
- }
-
- // Check if End Date is before Start Date
- if (endDate < startDate) {
- errorMessage += `Collections cannot end (${endDateValue}) before start (${startDateValue}).\n`;
- isValid = false;
- }
+ const validations = [
+ {
+ condition: startDate <= today,
+ message: `Collection start date (${startDateValue}) must be in the future.`
+ },
+ {
+ condition: endDate <= today,
+ message: `Collection end date (${endDateValue}) must be in the future.`
+ },
+ {
+ condition: endDate < startDate,
+ message: `Collection end date (${endDateValue}) cannot be before start date (${startDateValue}).`
+ }
+ ];
+
+ const failures = validations.filter(v => v.condition);
+ return {
+ isValid: failures.length === 0,
+ message: failures.map(f => f.message).join('\n')
+ };
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (ctrl.getFormMeta().name === 'afformCollectionCampIntentDetails') { | |
var startDateValue = $element.find("af-field[name='Collection_Camp_Intent_Details.Start_Date'] .crm-form-date-wrapper input.crm-form-date").val(); | |
var endDateValue = $element.find("af-field[name='Collection_Camp_Intent_Details.End_Date'] .crm-form-date-wrapper input.crm-form-date").val(); | |
var institutionStartDateValue = $element.find("af-field[name='Institution_Collection_Camp_Intent.Collections_will_start_on_Date_'] .crm-form-date-wrapper input.crm-form-date").val(); | |
var institutionEndDateValue = $element.find("af-field[name='Institution_Collection_Camp_Intent.Collections_will_end_on_Date_'] .crm-form-date-wrapper input.crm-form-date").val(); | |
if ((startDateValue && endDateValue) || (institutionStartDateValue && institutionEndDateValue)) { | |
if (startDateValue && endDateValue) { | |
var today = new Date(); | |
today.setHours(0, 0, 0, 0); | |
var startDateParts = startDateValue ? startDateValue.split('/') : institutionStartDateValue.split('/'); | |
var endDateParts = endDateValue ? endDateValue.split('/') : institutionEndDateValue.split('/'); | |
var startDateParts = startDateValue.split('/'); | |
var endDateParts = endDateValue.split('/'); | |
var startDate = new Date(startDateParts[2], startDateParts[1] - 1, startDateParts[0]); | |
var endDate = new Date(endDateParts[2], endDateParts[1] - 1, endDateParts[0]); | |
var errorMessage = ''; | |
var isValid = true; | |
// Check if the start date is in the past or today | |
if (startDate <= today) { | |
errorMessage += `Collections cannot start (${startDateValue || institutionStartDateValue}) today or in the past.\n`; | |
errorMessage += `Collections cannot start (${startDateValue}) today or in the past.\n`; | |
isValid = false; | |
} | |
// Check if the end date is in the past, today | |
if (endDate <= today) { | |
errorMessage += `Collections cannot end (${endDateValue || institutionEndDateValue}) today or in the past.\n`; | |
errorMessage += `Collections cannot end ( ${endDateValue}) today or in the past.\n`; | |
isValid = false; | |
} | |
// Check if End Date is before Start Date | |
if (endDate < startDate) { | |
errorMessage += `Collections cannot end (${endDateValue || institutionEndDateValue}) before start (${startDateValue || institutionStartDateValue}).\n`; | |
errorMessage += `Collections cannot end (${endDateValue}) before start (${startDateValue}).\n`; | |
isValid = false; | |
} | |
} | |
} | |
} | |
if (ctrl.getFormMeta().name === 'afformCollectionCampIntentDetails') { | |
const SELECTORS = { | |
startDate: "af-field[name='Collection_Camp_Intent_Details.Start_Date'] .crm-form-date-wrapper input.crm-form-date", | |
endDate: "af-field[name='Collection_Camp_Intent_Details.End_Date'] .crm-form-date-wrapper input.crm-form-date" | |
}; | |
const parseDate = (dateStr) => { | |
if (!dateStr || !/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateStr)) return null; | |
const [day, month, year] = dateStr.split('/'); | |
return new Date(year, month - 1, day); | |
}; | |
const startDateValue = $element.find(SELECTORS.startDate).val(); | |
const endDateValue = $element.find(SELECTORS.endDate).val(); | |
if (startDateValue && endDateValue) { | |
const today = new Date(); | |
today.setHours(0, 0, 0, 0); | |
const startDate = parseDate(startDateValue); | |
const endDate = parseDate(endDateValue); | |
if (!startDate || !endDate) { | |
return { | |
isValid: false, | |
message: 'Invalid date format. Please use DD/MM/YYYY format.' | |
}; | |
} | |
const validations = [ | |
{ | |
condition: startDate <= today, | |
message: `Collection start date (${startDateValue}) must be in the future.` | |
}, | |
{ | |
condition: endDate <= today, | |
message: `Collection end date (${endDateValue}) must be in the future.` | |
}, | |
{ | |
condition: endDate < startDate, | |
message: `Collection end date (${endDateValue}) cannot be before start date (${startDateValue}).` | |
} | |
]; | |
const failures = validations.filter(v => v.condition); | |
return { | |
isValid: failures.length === 0, | |
message: failures.map(f => f.message).join('\n') | |
}; | |
} | |
} |
🧰 Tools
🪛 Biome (1.9.4)
[error] 343-343: Shouldn't redeclare 'today'. Consider to delete it or rename it.
'today' is defined here:
(lint/suspicious/noRedeclare)
[error] 350-350: Shouldn't redeclare 'errorMessage'. Consider to delete it or rename it.
'errorMessage' is defined here:
(lint/suspicious/noRedeclare)
[error] 351-351: Shouldn't redeclare 'isValid'. Consider to delete it or rename it.
'isValid' is defined here:
(lint/suspicious/noRedeclare)
Summary by CodeRabbit
New Features
Bug Fixes
Documentation