Skip to content

Commit

Permalink
blocking submit based on rules (#3677)
Browse files Browse the repository at this point in the history
  • Loading branch information
VachetVirginie authored Aug 1, 2024
1 parent 7ec7f49 commit ae5d8a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
28 changes: 12 additions & 16 deletions packages/synapse-bridge/src/patterns/DatePicker/DatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface DatePickerData {
calendarIcon: string
closeIcon: string
errorMessages: string[] | any
warningErrorMessages: string[] | any
birthdateFlow: DatePickerFlow
isCalOpen: boolean
lastTypeAddedDate: string
Expand Down Expand Up @@ -134,6 +135,7 @@ export default defineComponent({
calendarIcon: mdiCalendar,
closeIcon: mdiCloseCircle,
errorMessages: [] as string[],
warningErrorMessages: [] as string[],
birthdateFlow: ['year', 'month', 'calendar'],
isCalOpen: false,
lastTypeAddedDate: '',
Expand Down Expand Up @@ -166,9 +168,7 @@ export default defineComponent({
hasError() {
return (
this.errorMessages.includes(
"La date saisie n'est pas valide"
) ||
this.errorMessages.includes("La date saisie n'est pas valide") ||
this.errorMessages.includes('Une erreur est survenue') ||
this.errorMessages.includes(this.customErrorMessages)
)
Expand Down Expand Up @@ -481,18 +481,14 @@ export default defineComponent({
},
validate(value: any) {
const allRules = [
...(this.warningRules || []),
...(this.rules || []),
]
const ruleErrors = allRules
.map((rule: any) => rule(value))
.filter((result) => result !== true)
this.errorMessages = ruleErrors.length > 0 ? ruleErrors : []
// Check if the error prop is true
const applyRules = (rules: any[]) =>
rules.map(rule => rule(value)).filter(result => result !== true);
this.errorMessages = applyRules(this.rules || []);
this.warningErrorMessages = applyRules(this.warningRules || []);
if (this.error) {
// If it is, add an error message
this.errorMessages.push('Une erreur est survenue')
this.errorMessages.push('Une erreur est survenue');
}
},
buildTextFieldClasses() {
Expand Down Expand Up @@ -598,14 +594,14 @@ export default defineComponent({
:class="[
textFieldClasses,
{
'warning-style': errorMessages.length > 0,
'warning-style': warningErrorMessages.length > 0,
'error-style': hasError,
range: range,
},
]"
:clearable="clearable"
:disabled="disabled"
:error-messages="errorMessages"
:error-messages="[...errorMessages, ...warningErrorMessages]"
:hint="hint"
:label="label"
:persistent-hint="true"
Expand Down
43 changes: 22 additions & 21 deletions packages/synapse-bridge/src/rules/notAfterToday/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { ruleMessage } from '../../helpers/ruleMessage'
import { isDateAfter } from '../../functions/validation/isDateAfter/index.ts'
import {
ValidationRule,
ValidationResult,
ErrorMessages,
Value,
} from '../types'
import { defaultErrorMessages } from './locales'
import { TODAY } from '../../constants'
import { ruleMessage } from '../../helpers/ruleMessage';
import { isDateAfter } from '../../functions/validation/isDateAfter/index.ts';
import { ValidationRule, ValidationResult, ErrorMessages, Value } from '../types';
import { defaultErrorMessages } from './locales';
import { TODAY } from '../../constants';

/** Check that the value is not after today (DD/MM/YYYY format) */
export function notAfterTodayFn(
errorMessages: ErrorMessages = defaultErrorMessages
): ValidationRule {
function formatDateToDDMMYYYY(date: Date): string {
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}

/** Vérifie que la valeur n'est pas après aujourd'hui (format DD/MM/YYYY) */
export function notAfterTodayFn(errorMessages: ErrorMessages = defaultErrorMessages): ValidationRule {
return (value: Value): ValidationResult => {
if (!value) {
return true
return true;
}

return (
!isDateAfter(TODAY, value) || ruleMessage(errorMessages, 'default')
)
}
const formattedValue = typeof value === 'object' ? formatDateToDDMMYYYY(value) : value;
if (isDateAfter(TODAY, formattedValue)) {
return ruleMessage(errorMessages, 'default');
}
return true;
};
}

export const notAfterToday = notAfterTodayFn()
export const notAfterToday = notAfterTodayFn();
15 changes: 12 additions & 3 deletions packages/synapse-bridge/src/rules/notBeforeToday/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { defaultErrorMessages } from './locales'
import { isDateBefore } from '../../functions/validation/isDateBefore'
import { TODAY } from '../../constants'

function formatDateToDDMMYYYY(date: Date): string {
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
/** Check that the value is not before today (DD/MM/YYYY format) */
export function notBeforeTodayFn(
errorMessages: ErrorMessages = defaultErrorMessages
Expand All @@ -19,9 +25,12 @@ export function notBeforeTodayFn(
if (!value) {
return true
}
return (
!isDateBefore(TODAY, value) || ruleMessage(errorMessages, 'default')
)
const formattedValue = typeof value === 'object' ? formatDateToDDMMYYYY(value) : value;
if (isDateBefore(TODAY, formattedValue)) {
return ruleMessage(errorMessages, 'default');
}
return true;

}
}

Expand Down

0 comments on commit ae5d8a9

Please sign in to comment.