Skip to content

Commit

Permalink
Errors n shiz
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Dec 12, 2024
1 parent 7261abd commit a05a980
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
23 changes: 8 additions & 15 deletions resources/js/calendar/collapsible_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class CollapsibleComponent {
outboundProperties = {};

validators = {};
errors = {};
errors = [];
calendar_settings = {};
is_valid = true;
collapsible_name = "Not set on the individual component?!?";
Expand Down Expand Up @@ -78,17 +78,12 @@ export default class CollapsibleComponent {
}

validate() {
this.errors = [];
for (let [localKey, validator] of Object.entries(this.validators)) {
let { error, message } = validator.bind(this)(localKey);
if (error) {
this.errors[localKey] = message;
} else if (this.errors?.[localKey]) {
// TODO: Remove error dispatch?
delete this.errors[localKey];
}
this.errors = this.errors.concat(validator.bind(this)(localKey));
}

this.is_valid = !Object.keys(this.errors).length;
this.is_valid = !this.errors.length;

return this.is_valid;
}
Expand All @@ -102,17 +97,15 @@ export default class CollapsibleComponent {
validationFailed() {
this.$dispatch("calendar-validation-failed", {
key: this.collapsible_name,
errors: Object.values(this.errors)
errors: this.errors.map(error => error.message)
})
}

getError(path) {

getErrorMessage(path) {
return this.errors.find(error => error.path === path)?.message ?? "";
}

hasError(path) {
console.log(path, _.get(this.errors, path));

return _.get(this.errors, path).length > 0;
return this.errors.some(error => error.path === path);
}
}
14 changes: 9 additions & 5 deletions resources/js/calendar/leap_days_collapsible.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,23 @@ class LeapDaysCollapsible extends CollapsibleComponent {
};

validateLeapDayIntervals() {
for (let leapDay of this.leap_days) {
let errors = [];

for (let [index, leapDay] of this.leap_days.entries()) {
let { interval } = leapDay;

interval = interval.trim().replace(/,\s*$/, "");

if (interval === "0") {
return { error: true, message: `${leapDay.name}'s interval is 0, please enter a positive number.` };
errors.push({ path: `leap_days.${index}.interval`, message: `${leapDay.name}'s interval is 0, please enter a positive number.` });
continue;
}

let invalid = this.interval_wide_regex.test(interval);

if (invalid) {
return { error: true, message: `${leapDay.name} has an invalid interval formula.` };
errors.push({ path: `leap_days.${index}.interval`, message: `${leapDay.name} has an invalid interval formula.` });
continue;
}

let values = interval.split(',');
Expand All @@ -127,11 +131,11 @@ class LeapDaysCollapsible extends CollapsibleComponent {
}

if (invalid) {
return { error: true, message: `${leapDay.name} has an invalid interval formula. The plus goes before the exclamation point.` };
errors.push({ path: `leap_days.${index}.interval`, message: `${leapDay.name} has an invalid interval formula. The plus goes before the exclamation point.` });
}
}

return { error: false, message: "" };
return errors;
}

getLeapDayIntervalText(leapDay) {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/alpine/text-input.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
>

<small class="text-red-600" x-show="hasError({{ $attributes->get('path') }})"
x-text="getError({{ $attributes->get('path') }})"></small>
x-text="getErrorMessage({{ $attributes->get('path') }})"></small>
2 changes: 1 addition & 1 deletion resources/views/components/leap-days-collapsible.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class='custom-select form-control full'
</div>
</div>
<div class='row no-gutters'>
<div class='col'>
<div class='col' x-show="!hasError(`leap_days.${index}.interval`)">
<div class='italics-text' x-text="interval_main_texts?.[index]"></div>
<ul class='italics-text list-disc pl-4'>
<template x-for="text in interval_subtexts?.[index]">
Expand Down

0 comments on commit a05a980

Please sign in to comment.