]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with email address validator (cherry-pick/rename this)
authorRoland Häder <roland@mxchange.org>
Fri, 13 May 2016 10:03:34 +0000 (12:03 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 13 May 2016 19:48:09 +0000 (21:48 +0200)
- added email address validator with regex validation and checking if the contact EJB reports an exisiting account
- this validator can be used in JSF pages/templates like: validator="EmailAddressValidator"
- in addition you may want to use a simple regex validator in addition to this to prevent ugly messages being shown
- added message output for missing privacy/terms checkboxes
- added required-message for email address

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/localization/bundle_de_DE.properties
src/java/org/mxchange/localization/bundle_en_US.properties
src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java
src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java
src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java [new file with mode: 0644]
web/WEB-INF/templates/guest/guest_email_address_repeat_fields.tpl
web/WEB-INF/templates/guest/guest_privacy_terms.tpl
web/WEB-INF/templates/login/user/user_change_email_address_repeat_fields.tpl

index 01981fa723b7ab6454ef9642c22e99d2fa6bbadf..6429fd1e3c5ccc8dd7204c6bf3b95fae9c92f5b6 100644 (file)
@@ -499,3 +499,5 @@ ERROR_GUEST_USER_LOGIN_DEACTIVATED=Fehler: Der Benutzerbereich wurde administrat
 ERROR_GUEST_USER_RECOVER_PASSWORD_DEACTIVATED=Fehler: Wiederherstellen des Passwortes wurde administrativ deaktiviert.
 ERROR_GUEST_USER_RESEND_CONFIRMATION_DEACTIVATED=Fehler: Erneutes Aussenden des Best\u00e4tigungslinked ist administrativ deaktiviert.
 ERROR_GUEST_USER_PROFILE_DEACTIVATED=Fehler: \u00d6ffentliche Benutzerprofile sind administrativ deaktiviert.
+EMAIL_ADDRESS_NOT_ENTERED=Bitte geben Sie Ihre Email-Adresse ein.
+EMAIL_ADDRESS_REPEAT_NOT_ENTERED=Bitte wiederholen Sie Ihre Email-Adresse.
index 8acdba379f5dbeb662617b2f7326762f1a2627ac..b0d5a1d0fe378e2b698b7c41e01c29e58a74de65 100644 (file)
@@ -460,3 +460,5 @@ ERROR_GUEST_USER_LOGIN_DEACTIVATED=Error: User area is deactivated by administra
 ERROR_GUEST_USER_RECOVER_PASSWORD_DEACTIVATED=Error: password recovery is deactivated by administrators.
 ERROR_GUEST_USER_RESEND_CONFIRMATION_DEACTIVATED=Error: Resending confirmation link is deactivated by administrators.
 ERROR_GUEST_USER_PROFILE_DEACTIVATED=Error: Public user profiles are deactivated by administrators.
+EMAIL_ADDRESS_NOT_ENTERED=Please enter your email address.
+EMAIL_ADDRESS_REPEAT_NOT_ENTERED=Please repeat your email address.
index b05478a184215a28cfb9728597d5ef9902df1342..9cb2d2bfd7db4395eccc408946497f4acc12ae9c 100644 (file)
@@ -368,6 +368,7 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P
 
                // Debug message
                //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
+
                // Copy all data from registered->user
                this.copyContact(registeredContact);
 
index 7bbe9d8e3711a85be6c0039d33f152fc6c8cefbb..25dee4e6fe9b3f5115880826a3a9e696e64f27dc 100644 (file)
@@ -64,7 +64,7 @@ public class PizzaUserRegisterWebSessionBean extends BasePizzaController impleme
        private PizzaAdminUserWebRequestController adminUserController;
 
        /**
-        * User controller
+        * Contact controller
         */
        @Inject
        private PizzaContactWebSessionController contactController;
@@ -149,7 +149,7 @@ public class PizzaUserRegisterWebSessionBean extends BasePizzaController impleme
                        User registeredUser = this.registerBean.registerUser(user);
 
                        // The id number should be set
-                       assert (registeredUser.getUserId() instanceof Long) : "registerUser.userId is null after registerUser() was called."; //NOI18N
+                       assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
 
                        // Fire event
                        this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
diff --git a/src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java b/src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java
new file mode 100644 (file)
index 0000000..3d0c9c3
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2016 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.pizzaapplication.validator.emailaddress;
+
+import java.text.MessageFormat;
+import java.util.regex.Pattern;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.FacesValidator;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+import javax.faces.view.facelets.FaceletException;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
+import org.mxchange.jcoreee.validator.string.BaseStringValidator;
+
+/**
+ * A validator for email address validation
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@FacesValidator ("EmailAddressValidator")
+public class PizzaEmailAddressValidator extends BaseStringValidator implements Validator {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 187_536_745_607_192L;
+
+       /**
+        * Contact session bean
+        */
+       private final ContactSessionBeanRemote contactBean;
+
+       /**
+        * Default constructor
+        */
+       public PizzaEmailAddressValidator () {
+               // Try it
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Try to lookup
+                       this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
+               } catch (final NamingException e) {
+                       // Throw again
+                       throw new FaceletException(e);
+               }
+       }
+
+       @Override
+       public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
+               // Trace message
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
+
+               // The required field
+               String[] requiredFields = {"emailAddress", "emailAddressRepeat"}; //NOI18N
+
+               // Pre-validation (example: not null, not a string, empty string ...)
+               super.preValidate(context, component, value, requiredFields, false);
+
+               // Get string from object ... ;-)
+               String emailAddress = String.valueOf(value);
+
+               // Checks if the email address matches a regex ("low-level" check, should also be done by <f:validatorRegex />)
+               boolean matches = Pattern.matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", emailAddress); //NOI18N
+
+               // Is the email address valid?
+               if (!matches) {
+                       // Generate message
+                       String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
+
+                       // Not matching
+                       throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
+               }
+
+               // Is the email address already registered?
+               if (this.contactBean.isEmailAddressRegistered(emailAddress)) {
+                       // Generate message
+                       String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
+
+                       // No, then abort here
+                       throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
+               }
+
+               // Trace message
+               //* NOISY-DEBUG: */ System.out.println("validate: EXIT!"); //NOI18N
+       }
+
+}
index a3a69c0ca2eae5d83ab2661f68fda0e274e067a9..357f9157dd26d9f4b25d3c8737f701818e0816c1 100644 (file)
@@ -12,7 +12,9 @@
                </div>
 
                <div class="table_right">
-                       <h:inputText styleClass="input" id="emailAddress" size="20" maxlength="255" value="#{contactController.emailAddress}" required="true" validator="EmailAddressValidator" />
+                       <h:inputText styleClass="input" id="emailAddress" size="20" maxlength="255" value="#{contactController.emailAddress}" required="true" requiredMessage="#{msg.EMAIL_ADDRESS_NOT_ENTERED}">
+                               <f:validator validatorId="EmailAddressValidator" />
+                       </h:inputText>
                </div>
 
                <div class="clear"></div>
                </div>
 
                <div class="table_right">
-                       <h:inputText styleClass="input" id="emailAddressRepeat" size="20" maxlength="255" value="#{contactController.emailAddressRepeat}" required="true" validator="EmailAddressValidator" />
+                       <h:inputText styleClass="input" id="emailAddressRepeat" size="20" maxlength="255" value="#{contactController.emailAddressRepeat}" required="true" requiredMessage="#{msg.EMAIL_ADDRESS_REPEAT_NOT_ENTERED}" />
                </div>
 
                <div class="clear"></div>
        </div>
+
+       <div>
+               <h:message for="emailAddress" errorClass="errors" warnClass="warnings" fatalClass="errors" />
+       </div>
+
+       <div>
+               <h:message for="emailAddressRepeat" errorClass="errors" warnClass="warnings" fatalClass="errors" />
+       </div>
 </ui:composition>
index 385dfc850f6529318e74558eb1a05c6f2b83c943..b060e38f4e943d3f182382b225f51d2bb4055f80 100644 (file)
@@ -25,6 +25,8 @@
                                <div class="clear"></div>
                        </div>
 
+                       <h:message for="privacy" errorClass="errors" warnClass="warnings" fatalClass="errors" />
+
                        <div class="table_row">
                                <div class="table_left25">
                                        <h:selectBooleanCheckbox styleClass="input" id="terms" required="true" requiredMessage="#{msg.TERMS_NOT_ACCEPTED_MESSAGE}">
@@ -40,6 +42,8 @@
 
                                <div class="clear"></div>
                        </div>
+
+                       <h:message for="terms" errorClass="errors" warnClass="warnings" fatalClass="errors" />
                </fieldset>
        </div>
 </ui:composition>
index 3ca9f1281721fa4a2bf7a02f19a26315e499c185..b11235440c1b52c7d8f4551c3df7535e258e0dae 100644 (file)
@@ -12,7 +12,9 @@
                </div>
 
                <div class="table_right">
-                       <h:inputText class="input" id="emailAddress" size="20" maxlength="255" value="#{emailChangeController.emailAddress}" required="true" validator="EmailAddressValidator" />
+                       <h:inputText class="input" id="emailAddress" size="20" maxlength="255" value="#{emailChangeController.emailAddress}" required="true" requiredMessage="#{msg.EMAIL_ADDRESS_NOT_ENTERED}">
+                               <f:validator validatorId="EmailAddressValidator" />
+                       </h:inputText>
                </div>
 
                <div class="clear"></div>
                </div>
 
                <div class="table_right">
-                       <h:inputText class="input" id="emailAddressRepeat" size="20" maxlength="255" value="#{emailChangeController.emailAddressRepeat}" required="true" validator="EmailAddressValidator" />
+                       <h:inputText class="input" id="emailAddressRepeat" size="20" maxlength="255" value="#{emailChangeController.emailAddressRepeat}" required="true" requiredMessage="#{msg.EMAIL_ADDRESS_REPEAT_NOT_ENTERED}" />
                </div>
 
                <div class="clear"></div>
        </div>
+
+       <div>
+               <h:message for="emailAddress" errorClass="errors" warnClass="warnings" fatalClass="errors" />
+       </div>
+
+       <div>
+               <h:message for="emailAddressRepeat" errorClass="errors" warnClass="warnings" fatalClass="errors" />
+       </div>
 </ui:composition>