2 * Copyright (C) 2015 Roland Haeder
\r
4 * This program is free software: you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation, either version 3 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
17 package org.mxchange.jsfcore.validator.string;
\r
19 import java.text.MessageFormat;
\r
20 import java.util.Arrays;
\r
21 import javax.faces.application.FacesMessage;
\r
22 import javax.faces.component.UIComponent;
\r
23 import javax.faces.context.FacesContext;
\r
24 import javax.faces.validator.ValidatorException;
\r
25 import org.mxchange.jsfcore.validator.BaseObjectValidator;
\r
28 * A general string validation class. You normally want to inherit from this
\r
29 * class for many form fields, e.g. surname, street name, city name and such.
\r
31 * @author Roland Haeder
\r
33 public abstract class BaseStringValidator extends BaseObjectValidator {
\r
36 * Pre-validation of value, e.g. not null
\r
38 * @param context FacesContext instance
\r
39 * @param component UIComponent instance
\r
40 * @param value Value to check
\r
41 * @param fields Array of valid field names (ending with)
\r
42 * @throws ValidatorException If something more horrible went wrong
\r
45 protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] fields) throws ValidatorException {
\r
47 this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(fields)));
\r
49 // Pre-validate (e.g. on null)
\r
50 super.preValidate(context, component, value, fields);
\r
52 // Get client id and init message + key
\r
53 String clientId = component.getClientId();
\r
54 FacesMessage facesMessage = null;
\r
58 for (final String field : fields) {
\r
60 this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId));
\r
63 if (clientId.endsWith(field)) {
\r
64 if (!(value instanceof String)) {
\r
66 errKey = String.format("error.%s.is_not_string", field);
\r
68 facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
\r
72 String str = (String) value;
\r
75 if (str.isEmpty()) {
\r
77 errKey = String.format("error.%s.is_empty", field);
\r
79 facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
\r
85 this.getLogger().debug(MessageFormat.format("facesMessage={0}", facesMessage));
\r
88 if (null != facesMessage) {
\r
89 throw new ValidatorException(facesMessage);
\r
93 this.getLogger().trace("EXIT!");
\r