2 * Copyright (C) 2016, 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcoreee.validator.string;
19 import java.text.MessageFormat;
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.validator.ValidatorException;
24 import org.mxchange.jcoreee.validator.BaseObjectValidator;
27 * A general string validation class. You normally want to inherit from this
28 * class for many form fields, e.g. surname, street name, city name and such.
30 * @author Roland Häder<roland@mxchange.org>
32 public abstract class BaseStringValidator extends BaseObjectValidator<Object> {
37 private static final long serialVersionUID = 15_484_578_781_760_287L;
40 protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
42 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
44 // Pre-validate (example: on null)
45 super.preValidate(context, component, value, requiredFields, allowNull);
47 // Get client id and init message + key
48 String clientId = component.getClientId();
49 String requiredMessage = null;
51 // So far all fine, no check if the field is fine
52 for (final String field : requiredFields) {
54 //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
57 if (clientId.endsWith(field)) {
58 // Compare value's type
59 if ((!allowNull) && (!(value instanceof String))) {
61 requiredMessage = MessageFormat.format("Field {0} is not String: {1}", field, value); //NOI18N
65 String str = (String) value;
68 if ((!allowNull) && (null == str)) {
69 // Empty strings are currently not accepted, may indicate flaw in invoker
70 requiredMessage = MessageFormat.format("Field {0} is null.", field); //NOI18N
71 } else if ((!allowNull) && (str.isEmpty())) {
72 // Empty strings are currently not accepted, may indicate flaw in invoker
73 requiredMessage = MessageFormat.format("Field {0} is empty.", field); //NOI18N
79 //this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N
82 if (null != requiredMessage) {
83 // Then there was something wrong with it
84 throw new ValidatorException(new FacesMessage(requiredMessage));
88 //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N