You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(Settings): enhance validation and error handling for settings
- Added validation methods for default Swiper configuration and CSS variables in Settings.php.
- Updated SettingsController to utilize new validation methods and handle errors appropriately.
- Enhanced Twig templates to display validation errors for various settings fields, improving user feedback.
@@ -186,9 +187,8 @@ public function actionSave(): ?Response
186
187
// Ensure each preset is an object/array of CSS vars
187
188
foreach ($decodedas$handle => $vars) {
188
189
if (!is_string($handle) || $handle === '' || !is_array($vars)) {
189
-
$settings->addError('swiperCssVars', Craft::t('slideshow-manager', 'Style presets must be an object keyed by style handle, with each value as an object.'));
190
+
$settings->addError('swiperCssVars._styles', Craft::t('slideshow-manager', 'Style presets must be an object keyed by style handle, with each value as an object.'));
190
191
Craft::$app->getSession()->setError(Craft::t('slideshow-manager', 'Could not save settings.'));
$this->addError('defaultSwiperConfig.zoom.maxRatio', Craft::t(static::pluginHandle(), 'Zoom max ratio must be between 1 and 20.'));
192
+
}
193
+
if ($this->isNumeric($zoomMin) && $this->isNumeric($zoomMax) && (float)$zoomMax < (float)$zoomMin) {
194
+
$this->addError('defaultSwiperConfig.zoom.maxRatio', Craft::t(static::pluginHandle(), 'Zoom max ratio must be greater than or equal to zoom min ratio.'));
$this->addError('defaultSwiperConfig.breakpoints', Craft::t(static::pluginHandle(), 'Breakpoints must be an array.'));
201
+
} else {
202
+
foreach ($breakpointsas$index => $breakpoint) {
203
+
if (!is_array($breakpoint)) {
204
+
$this->addError('defaultSwiperConfig.breakpoints', Craft::t(static::pluginHandle(), 'Breakpoint #{index} must be an object.', ['index' => (string)($index + 1)]));
$this->addError($attribute, Craft::t(static::pluginHandle(), 'CSS vars configuration must be an object.'));
235
+
return;
236
+
}
237
+
238
+
$activeHandle = $cssVars['_active'] ?? '';
239
+
if ($activeHandle !== '' && !preg_match('/^[a-z0-9_-]+$/i', (string)$activeHandle)) {
240
+
$this->addError('swiperCssVars._active', Craft::t(static::pluginHandle(), 'Active style handle may only contain letters, numbers, hyphens, and underscores.'));
241
+
}
242
+
243
+
$styles = $cssVars['_styles'] ?? null;
244
+
if ($styles !== null && !is_array($styles)) {
245
+
$this->addError('swiperCssVars._styles', Craft::t(static::pluginHandle(), 'Style presets must be an object keyed by style handle.'));
246
+
}
247
+
248
+
$lengthFields = [
249
+
'navigationSize',
250
+
'navigationTopOffset',
251
+
'navigationSidesOffset',
252
+
'navigationPadding',
253
+
'navigationRadius',
254
+
'paginationBulletSize',
255
+
'paginationBulletWidth',
256
+
'paginationBulletHeight',
257
+
'paginationBulletHorizontalGap',
258
+
'paginationBulletVerticalGap',
259
+
'paginationProgressbarSize',
260
+
'paginationLeft',
261
+
'paginationRight',
262
+
'paginationTop',
263
+
'paginationBottom',
264
+
'scrollbarBorderRadius',
265
+
'scrollbarTop',
266
+
'scrollbarBottom',
267
+
'scrollbarLeft',
268
+
'scrollbarRight',
269
+
'scrollbarSidesOffset',
270
+
'scrollbarSize',
271
+
];
272
+
$colorFields = [
273
+
'themeColor',
274
+
'navigationColor',
275
+
'navigationInactiveColor',
276
+
'navigationBg',
277
+
'navigationBgHover',
278
+
'navigationBorderColor',
279
+
'navigationBorderColorHover',
280
+
'paginationColor',
281
+
'paginationBulletInactiveColor',
282
+
'paginationFractionColor',
283
+
'paginationProgressbarBgColor',
284
+
'scrollbarBgColor',
285
+
'scrollbarDragBgColor',
286
+
'thumbActiveColor',
287
+
'slideBgColor',
288
+
];
289
+
$opacityFields = [
290
+
'paginationBulletInactiveOpacity',
291
+
'paginationBulletOpacity',
292
+
];
293
+
294
+
foreach ($lengthFieldsas$field) {
295
+
$value = trim((string)($cssVars[$field] ?? ''));
296
+
if ($value === '') {
297
+
continue;
298
+
}
299
+
if (!$this->isValidCssLengthOrKeyword($value, ['auto', 'inherit'])) {
300
+
$this->addError(
301
+
"swiperCssVars.{$field}",
302
+
Craft::t(
303
+
static::pluginHandle(),
304
+
'Invalid value. Use a CSS length/percentage (for example: 44px, 0.5rem, 50%), `auto`, `inherit`, or `var(...)`/`calc(...)`.'
305
+
)
306
+
);
307
+
}
308
+
}
309
+
310
+
foreach ($colorFieldsas$field) {
311
+
$value = trim((string)($cssVars[$field] ?? ''));
312
+
if ($value === '') {
313
+
continue;
314
+
}
315
+
if (!$this->isValidCssColor($value)) {
316
+
$this->addError(
317
+
"swiperCssVars.{$field}",
318
+
Craft::t(
319
+
static::pluginHandle(),
320
+
'Invalid color. Use hex, rgb()/rgba(), hsl()/hsla(), `transparent`, `currentColor`, or `var(...)`.'
0 commit comments