X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Fjcoreee%2Fvalidator%2Fstring%2FBaseStringValidator.java;h=84fc78131e4fb5a675b73c743335f8c0f54d448a;hb=87ad78a7a37317c50fa1613c51159cb98772ba46;hp=83dc8a6ccd8ae163c62e0365b725fdee70699c45;hpb=7d2ca865768d5c10981feafbf8c270ff698a9478;p=jcore-utils.git diff --git a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java b/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java index 83dc8a6..84fc781 100644 --- a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java +++ b/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Roland Haeder + * Copyright (C) 2016, 2017 Roland Häder * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,48 +17,48 @@ package org.mxchange.jcoreee.validator.string; import java.text.MessageFormat; -import java.util.Arrays; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.ValidatorException; -import org.mxchange.jcoree.BaseEeSystem; import org.mxchange.jcoreee.validator.BaseObjectValidator; /** * A general string validation class. You normally want to inherit from this * class for many form fields, e.g. surname, street name, city name and such. - * - * @author Roland Haeder + *

+ * @author Roland Häder */ public abstract class BaseStringValidator extends BaseObjectValidator { + /** + * Serial number + */ + private static final long serialVersionUID = 15_484_578_781_760_287L; + @Override - protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException { + protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { // Trace message - this.getLogger().logTrace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N - // Pre-validate (e.g. on null) - super.preValidate(context, component, value, requiredFields); + // Pre-validate (example: on null) + super.preValidate(context, component, value, requiredFields, allowNull); // Get client id and init message + key String clientId = component.getClientId(); - FacesMessage facesMessage = null; - String errKey; + String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { // Debug message - this.getLogger().logDebug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N + //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N // Is it the same? if (clientId.endsWith(field)) { // Compare value's type - if (!(value instanceof String)) { + if ((!allowNull) && (!(value instanceof String))) { // Value is empty - errKey = String.format("error.%s.is_not_string", field); //NOI18N - - facesMessage = new FacesMessage(BaseEeSystem.getMessageStringFromKey(errKey)); + requiredMessage = MessageFormat.format("Field {0} is not String: {1}", field, value); //NOI18N } // Cast to string @@ -66,23 +66,22 @@ public abstract class BaseStringValidator extends BaseObjectValidator { // Is it empty? if (str.isEmpty()) { - // Value is empty - errKey = String.format("error.%s.is_empty", field); //NOI18N - - facesMessage = new FacesMessage(BaseEeSystem.getMessageStringFromKey(errKey)); + // Empty strings are currently not accepted, may indicate flaw in invoker + requiredMessage = MessageFormat.format("Field {0} is empty.", field); //NOI18N } } } // Debug message - this.getLogger().logDebug(MessageFormat.format("facesMessage={0}", facesMessage)); //NOI18N + //this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N // Is it not null? - if (null != facesMessage) { - throw new ValidatorException(facesMessage); + if (null != requiredMessage) { + // Then there was something wrong with it + throw new ValidatorException(new FacesMessage(requiredMessage)); } // Trace message - this.getLogger().logTrace("EXIT!"); //NOI18N + //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N } }