From c996e8a5f69c0ccb4714847e8e70a453c377df87 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 10 May 2011 06:59:41 +0200 Subject: [PATCH] Add warnings to options page if an option value was not validated. --- .../net/pterodactylus/sone/web/OptionsPage.java | 33 ++++++++++++++++++---- src/main/resources/i18n/sone.en.properties | 1 + src/main/resources/static/css/sone.css | 5 ++++ src/main/resources/templates/options.html | 12 ++++++++ 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java index bbc1084..51ca6cd 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -17,6 +17,9 @@ package net.pterodactylus.sone.web; +import java.util.ArrayList; +import java.util.List; + import net.pterodactylus.sone.core.Core.Preferences; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.Page.Request.Method; @@ -56,21 +59,38 @@ public class OptionsPage extends SoneTemplatePage { Preferences preferences = webInterface.getCore().getPreferences(); Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false); if (request.getMethod() == Method.POST) { + List fieldErrors = new ArrayList(); if (currentSone != null) { boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow"); currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow); webInterface.getCore().saveSone(currentSone); } Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16)); - preferences.setInsertionDelay(insertionDelay); + if (!preferences.validateInsertionDelay(insertionDelay)) { + fieldErrors.add("insertion-delay"); + } else { + preferences.setInsertionDelay(insertionDelay); + } Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null); - preferences.setPostsPerPage(postsPerPage); + if (!preferences.validatePostsPerPage(postsPerPage)) { + fieldErrors.add("posts-per-page"); + } else { + preferences.setPostsPerPage(postsPerPage); + } boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access"); preferences.setRequireFullAccess(requireFullAccess); Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3)); - preferences.setPositiveTrust(positiveTrust); + if (!preferences.validatePositiveTrust(positiveTrust)) { + fieldErrors.add("positive-trust"); + } else { + preferences.setPositiveTrust(positiveTrust); + } Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4)); - preferences.setNegativeTrust(negativeTrust); + if (!preferences.validateNegativeTrust(negativeTrust)) { + fieldErrors.add("negative-trust"); + } else { + preferences.setNegativeTrust(negativeTrust); + } String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256); if (trustComment.trim().length() == 0) { trustComment = null; @@ -83,7 +103,10 @@ public class OptionsPage extends SoneTemplatePage { boolean reallyClearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("really-clear-on-next-restart", 5)); preferences.setReallyClearOnNextRestart(reallyClearOnNextRestart); webInterface.getCore().saveConfiguration(); - throw new RedirectException(getPath()); + if (fieldErrors.isEmpty()) { + throw new RedirectException(getPath()); + } + templateContext.set("fieldErrors", fieldErrors); } if (currentSone != null) { templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get()); diff --git a/src/main/resources/i18n/sone.en.properties b/src/main/resources/i18n/sone.en.properties index 21ebc8f..0a44f2c 100644 --- a/src/main/resources/i18n/sone.en.properties +++ b/src/main/resources/i18n/sone.en.properties @@ -50,6 +50,7 @@ Page.Options.Option.SoneRescueMode.Description3=Note that when you use the Rescu Page.Options.Section.Cleaning.Title=Clean Up Page.Options.Option.ClearOnNextRestart.Description=Resets the configuration of the Sone plugin at the next restart. Warning! {strong}This will destroy all of your Sones{/strong} so make sure you have backed up everyhing you still need! Also, you need to set the next option to true to actually do it. Page.Options.Option.ReallyClearOnNextRestart.Description=This option needs to be set to “yes” if you really, {strong}really{/strong} want to clear the plugin configuration on the next restart. +Page.Options.Warnings.ValueNotChanged=This option was not changed because value you specified was not valid. Page.Options.Button.Save=Save Page.Login.Title=Login - Sone diff --git a/src/main/resources/static/css/sone.css b/src/main/resources/static/css/sone.css index 893072e..c0a7b6d 100644 --- a/src/main/resources/static/css/sone.css +++ b/src/main/resources/static/css/sone.css @@ -670,3 +670,8 @@ textarea { font-weight: bold; color: red; } + +#sone .warning { + color: red; + font-style: italic; +} diff --git a/src/main/resources/templates/options.html b/src/main/resources/templates/options.html index c5130d7..930d088 100644 --- a/src/main/resources/templates/options.html +++ b/src/main/resources/templates/options.html @@ -43,9 +43,15 @@

<%= Page.Options.Section.RuntimeOptions.Title|l10n|html>

<%= Page.Options.Option.InsertionDelay.Description|l10n|html>

+ <%if =insertion-delay|in collection=fieldErrors> +

<%= Page.Options.Warnings.ValueNotChanged|l10n|html>

+ <%/if>

<%= Page.Options.Option.PostsPerPage.Description|l10n|html>

+ <%if =posts-per-page|in collection=fieldErrors> +

<%= Page.Options.Warnings.ValueNotChanged|l10n|html>

+ <%/if>

@@ -56,9 +62,15 @@

<%= Page.Options.Section.TrustOptions.Title|l10n|html>

<%= Page.Options.Option.PositiveTrust.Description|l10n|html>

+ <%if =positive-trust|in collection=fieldErrors> +

<%= Page.Options.Warnings.ValueNotChanged|l10n|html>

+ <%/if>

<%= Page.Options.Option.NegativeTrust.Description|l10n|html>

+ <%if =negative-trust|in collection=fieldErrors> +

<%= Page.Options.Warnings.ValueNotChanged|l10n|html>

+ <%/if>

<%= Page.Options.Option.TrustComment.Description|l10n|html>

-- 2.7.4