Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Licensed to the Apache Software Foundation (ASF) under one or more
    * contributor license agreements.  See the NOTICE file distributed with
    * this work for additional information regarding copyright ownership.
    * The ASF licenses this file to You under the Apache License, Version 2.0
    * (the "License"); you may not use this file except in compliance with
    * the License.  You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.wicket.markup.html.form;
  
  import java.text.Format;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Locale;
  import java.util.Map;
  import java.util.Set;
  
  import org.slf4j.Logger;
An HTML form component knows how to validate itself. Validators that implement IValidator can be added to the component. They will be evaluated in the order they were added and the first Validator that returns an error message determines the error message returned by the component.

FormComponents are not versioned by default. If you need versioning for your FormComponents, you will need to call Form.setVersioned(true), which will set versioning on for the form and all form component children.

If this component is required and that fails, the error key that is used is the "Required"; if the type conversion fails, it will use the key "IConverter" if the conversion failed in a converter, or "ConversionError" if type was explicitly specified via setType(java.lang.Class) or a org.apache.wicket.model.IPropertyReflectionAwareModel was used. Notice that both "IConverter" and "ConversionError" have a more specific variant of "key.classname" where classname is the type that we failed to convert to. Classname is not full qualified, so only the actual name of the class is used. Property expressions that can be used in error messages are:

  • ${input}: the input the user did give
  • ${name}: the name of the component that failed
  • ${label}: the label of the component

Parameters:
<T> The model object type
Author(s):
Jonathan Locke
Eelco Hillenius
Johan Compagner
Igor Vaynberg (ivaynberg)
  
  public abstract class FormComponent<T> extends LabeledWebMarkupContainer
  	implements
  {
  	private static final Logger logger = LoggerFactory.getLogger(FormComponent.class);

Visitor for traversing form components
 
 	public static abstract class AbstractVisitor implements IVisitor
 	{
 
 		{
 			if (component instanceof FormComponent<?>)
 			{
 			}
 		}
 
 		protected abstract void onFormComponent(FormComponent<?> formComponent);
 	}

Typesafe interface to code that is called when visiting a form component.
 
 	public static interface IVisitor
 	{
Called when visiting a form component

Parameters:
formComponent The form component
Returns:
component
 
 		public Object formComponent(IFormVisitorParticipant formComponent);
 	}

org.apache.wicket.validation.IErrorMessageSource used for error messages against this form components.

Author(s):
ivaynberg
 
 	private class MessageSource implements IErrorMessageSource
 	{
 		private final Set<StringtriedKeys = new LinkedHashSet<String>();

 
 		public String getMessage(String key)
 		{
 			final FormComponent<T> formComponent = FormComponent.this;
 
 			// Use the following log4j config for detailed logging on the property resolution
 			// process
 			// log4j.logger.org.apache.wicket.resource.loader=DEBUG
 			// log4j.logger.org.apache.wicket.Localizer=DEBUG
 
 			final Localizer localizer = formComponent.getLocalizer();
 
 			// retrieve prefix that will be used to construct message keys
 			String prefix = formComponent.getValidatorKeyPrefix();
 			String message = null;
 
 			// first try the full form of key [form-component-id].[key]
 			String resource = getId() + "." + prefix(prefixkey);
 			message = getString(localizerresourceformComponent);
 
 			// if not found, try a more general form (without prefix)
 			// [form-component-id].[prefix].[key]
 			if (Strings.isEmpty(message) && Strings.isEmpty(prefix))
 			{
 				resource = getId() + "." + key;
 				message = getString(localizerresourceformComponent);
 			}
 
 			// If not found try a more general form [prefix].[key]
 			if (Strings.isEmpty(message))
 			{
 				resource = prefix(prefixkey);
 				message = getString(localizerkeyformComponent);
 			}
 
 			// If not found try the most general form [key]
 			if (Strings.isEmpty(message) && Strings.isEmpty(prefix))
 			{
 				// Try a variation of the resource key
 				message = getString(localizerkeyformComponent);
 			}
 
 			// convert empty string to null in case our default value of "" was
 			// returned from localizer
 			if (Strings.isEmpty(message))
 			{
 				message = null;
 			}
 			return message;
 		}
 
 		private String prefix(String prefixString key)
 		{
 			if (!Strings.isEmpty(prefix))
 			{
 				return prefix + "." + key;
 			}
 			else
 			{
 				return key;
 			}
 		}

Parameters:
localizer
key
component
Returns:
string
 
 		private String getString(Localizer localizerString keyComponent component)
 		{
 			.add(key);
 
 			// Note: It is important that the default value of "" is
 			// provided to getString() not to throw a MissingResourceException or to
 			// return a default string like "[Warning: String ..."
 			return localizer.getString(keycomponent"");
 		}

 
 		public String substitute(String stringMap<StringObjectvars)
 		{
 			return new MapVariableInterpolator(stringaddDefaultVars(vars), Application.get()
 		}

Creates a new params map that additionally contains the default input, name, label parameters

Parameters:
params original params map
Returns:
new params map
 
 		private Map<StringObjectaddDefaultVars(Map<StringObjectparams)
 		{
 			// create and fill the new params map
 			final HashMap<StringObjectfullParams;
 			if (params == null)
 			{
 				fullParams = new HashMap<StringObject>(6);
 			}
 			else
 			{
 				fullParams = new HashMap<StringObject>(params.size() + 6);
 				fullParams.putAll(params);
 			}
 
 			// add the input param if not already present
 			if (!fullParams.containsKey("input"))
 			{
 				fullParams.put("input"getInput());
 			}
 
 			// add the name param if not already present
 			if (!fullParams.containsKey("name"))
 			{
 				fullParams.put("name"getId());
 			}
 
 			// add the label param if not already present
 			if (!fullParams.containsKey("label"))
 			{
 				fullParams.put("label"getLabel());
 			}
 			return fullParams;
 		}

Returns:
value of label param for this form component
 
 		private String getLabel()
 		{
 			final FormComponent<T> fc = FormComponent.this;
 			String label = null;
 
 			// first try the label model ...
 			if (fc.getLabel() != null)
 			{
 				label = fc.getLabel().getObject();
 			}
 			// ... then try a resource of format [form-component-id] with
 			// default of '[form-component-id]'
 			if (label == null)
 			{
 
 				label = fc.getLocalizer().getString(fc.getId(), fc.getParent(), fc.getId());
 			}
 			return label;
 		}
 	}

Change object to capture the required flag change

Author(s):
Igor Vaynberg (ivaynberg)
 
 	private final class RequiredStateChange extends Change
 	{
 		private static final long serialVersionUID = 1L;
 
 		private final boolean required = isRequired();

 
 		public void undo()
 		{
 		}
 	}

Adapter that makes this component appear as org.apache.wicket.validation.IValidatable

Author(s):
ivaynberg
 
 	private class ValidatableAdapter implements IValidatable<T>
 	{
 
 		public void error(IValidationError error)
 		{
 			FormComponent.this.error(error);
 		}

 
 		public T getValue()
 		{
 			return getConvertedInput();
 		}

 
 		public boolean isValid()
 		{
 			return FormComponent.this.isValid();
 		}
 	}

The value separator
 
 	public static String VALUE_SEPARATOR = ";";
 
 	private static final String[] EMPTY_STRING_ARRAY = new String[] { "" };

Whether this form component should save and restore state between sessions. This is false by default.
 
 	private static final short FLAG_PERSISTENT = ;

Whether or not this component's value is required (non-empty)
 
 	private static final short FLAG_REQUIRED = ;
 
 	private static final String NO_RAW_INPUT = "[-NO-RAW-INPUT-]";
 
 	private static final long serialVersionUID = 1L;

Make empty strings null values boolean. Used by AbstractTextComponent subclass.
 
 	protected static final short FLAG_CONVERT_EMPTY_INPUT_STRING_TO_NULL = ;

Visits any form components inside component if it is a container, or component itself if it is itself a form component

Parameters:
component starting point of the traversal
visitor The visitor to call
 
 	public static final void visitFormComponentsPostOrder(Component component,
 		final FormComponent.IVisitor visitor)
 	{
 		if (visitor == null)
 		{
 			throw new IllegalArgumentException("Argument `visitor` cannot be null");
 		}
 
 		visitFormComponentsPostOrderHelper(componentvisitor);
 	}

Parameters:
component
visitor
Returns:
Object
 
 	private static final Object visitFormComponentsPostOrderHelper(Component component,
 		final FormComponent.IVisitor visitor)
 	{
 		if (component instanceof MarkupContainer)
 		{
 			final MarkupContainer container = (MarkupContainer)component;
 			if (container.size() > 0)
 			{
 				boolean visitChildren = true;
 				if (container instanceof IFormVisitorParticipant)
 				{
 					visitChildren = ((IFormVisitorParticipant)container).processChildren();
 				}
 				if (visitChildren)
 				{
 					final Iterator<? extends Componentchildren = container.iterator();
 					while (children.hasNext())
 					{
 						final Component child = children.next();
 						Object value = visitFormComponentsPostOrderHelper(childvisitor);
 						{
 							return value;
 						}
 					}
 				}
 			}
 		}
 
 		if (component instanceof FormComponent<?>)
 		{
 			final FormComponent<?> fc = (FormComponent<?>)component;
 			return visitor.formComponent(fc);
 		}
 
 		return null;
 	}

Visits any form components inside component if it is a container, or component itself if it is itself a form component

Parameters:
component starting point of the traversal
visitor The visitor to call
 
 	public static final void visitComponentsPostOrder(Component component,
 		final Component.IVisitor<Componentvisitor)
 	{
 		if (visitor == null)
 		{
 			throw new IllegalArgumentException("Argument `visitor` cannot be null");
 		}
 
 		visitComponentsPostOrderHelper(componentvisitor);
 	}

Parameters:
component
visitor
Returns:
Object
 
 	private static final Object visitComponentsPostOrderHelper(Component component,
 		final Component.IVisitor<Componentvisitor)
 	{
 		if (component instanceof MarkupContainer)
 		{
 			final MarkupContainer container = (MarkupContainer)component;
 			if (container.size() > 0)
 			{
 				boolean visitChildren = true;
 				if (container instanceof IFormVisitorParticipant)
 				{
 					visitChildren = ((IFormVisitorParticipant)container).processChildren();
 				}
 				if (visitChildren)
 				{
 					final Iterator<? extends Componentchildren = container.iterator();
 					while (children.hasNext())
 					{
 						final Component child = children.next();
 						Object value = visitComponentsPostOrderHelper(childvisitor);
 						{
 							return value;
 						}
 						else if (value == ..)
 						{
 							// noop
 						}
 						{
 							// noop
 						}
 						else
 						{
 							return value;
 						}
 					}
 				}
 			}
 		}
 		return visitor.component(component);
 	}
 
 
 	private transient T convertedInput;

Raw Input entered by the user or NO_RAW_INPUT if nothing is filled in.
 
 	private String rawInput = ;

Type that the raw input string will be converted to
 
 	private String typeName;

The list of validators for this form component as either an IValidator instance or an array of IValidator instances.
 
 	private Object validators = null;

See also:
org.apache.wicket.Component.org.apache.wicket.Component.(java.lang.String)
 
 	public FormComponent(final String id)
 	{
 		super(id);
 		// the form decides whether form components are versioned or not
 		// see Form.setVersioned
 		setVersioned(false);
 	}

Parameters:
id
model
See also:
org.apache.wicket.Component.org.apache.wicket.Component.(java.lang.String,org.apache.wicket.model.IModel)
 
 	public FormComponent(final String idIModel<T> model)
 	{
 		super(idmodel);
 		// the form decides whether form components are versioned or not
 		// see Form.setVersioned
 		setVersioned(false);
 	}

Adds a validator to this form component

Parameters:
validator validator to be added
Returns:
this for chaining
Throws:
java.lang.IllegalArgumentException if validator is null
See also:
org.apache.wicket.validation.IValidator
org.apache.wicket.validation.IValidatorAddListener
 
 	public final FormComponent<T> add(final IValidator<T> validator)
 	{
 		if (validator == null)
 		{
 			throw new IllegalArgumentException("validator argument cannot be null");
 		}
 		// add the validator
 		validators_add(validator);
 
 		// see whether the validator listens for add events
 		if (validator instanceof IValidatorAddListener)
 		{
 			((IValidatorAddListener)validator).onAdded(this);
 		}
 		return this;
 	}

Adds a validator to this form component.

Parameters:
validators The validator(s) to be added
Returns:
This
Throws:
java.lang.IllegalArgumentException if validator is null
See also:
org.apache.wicket.validation.IValidator
org.apache.wicket.validation.IValidatorAddListener
 
 	public final FormComponent<T> add(final IValidator<T>... validators)
 	{
 		if (validators == null)
 		{
 			throw new IllegalArgumentException("validator argument cannot be null");
 		}
 
 		for (IValidator<T> validator : validators)
 		{
 			add(validator);
 		}
 
 		// return this for chaining
 		return this;
 	}

Checks if the form component's 'required' requirement is met by first checking isRequired() to see if it has to check for requirement. If that is true then by default it checks if the input is null or an empty String org.apache.wicket.util.string.Strings.isEmpty(java.lang.CharSequence)

Subclasses that overwrite this method should also call isRequired() first.

Returns:
true if the 'required' requirement is met, false otherwise
See also:
org.apache.wicket.util.string.Strings.isEmpty(java.lang.CharSequence)
isInputNullable()
 
 	public boolean checkRequired()
 	{
 		if (isRequired())
 		{
 			final String input = getInput();
 
 			// when null, check whether this is natural for that component, or
 			// whether - as is the case with text fields - this can only happen
 			// when the component was disabled
 			if (input == null && !isInputNullable() && !isEnabledInHierarchy())
 			{
 				// this value must have come from a disabled field
 				// do not perform validation
 				return true;
 			}
 
 			// peform validation by looking whether the value is null or empty
 			return !Strings.isEmpty(input);
 		}
 		return true;
 	}

Clears the user input.
 
 	public final void clearInput()
 	{
 	}

Reports a validation error against this form component. The actual error is reported by creating a ValidationErrorFeedback object that holds both the validation error and the generated error message - so a custom feedback panel can have access to both.

Parameters:
error validation error
 
 	public void error(IValidationError error)
 	{
 		if (error == null)
 		{
 			throw new IllegalArgumentException("Argument [[error]] cannot be null");
 		}
 		MessageSource source = new MessageSource();
 		String message = error.getErrorMessage(source);
 
 		if (message == null)
 		{
 			StringBuffer buffer = new StringBuffer();
 			buffer.append("Could not locate error message for component: ");
 			buffer.append(Classes.simpleName(getClass()));
 			buffer.append("@");
 			buffer.append(" and error: ");
 			buffer.append(error.toString());
 			buffer.append(". Tried keys: ");
 			Iterator<Stringkeys = source.triedKeys.iterator();
 			while (keys.hasNext())
 			{
 				buffer.append(keys.next());
 				if (keys.hasNext())
 				{
 					buffer.append(", ");
 				}
 			}
 			buffer.append(".");
 			message = buffer.toString();
 			.warn(message);
 		}
 		error(new ValidationErrorFeedback(errormessage));
 	}

Gets the converted input. The converted input is set earlier though the implementation of convertInput().

Returns:
value of input possibly converted into an appropriate type
 
 	public final T getConvertedInput()
 	{
 		return ;
 	}

Sets the converted input. This method is typically not called by clients, unless they override convertInput(), in which case they should call this method to update the input for this component instance.

Parameters:
convertedInput the converted input
 
 	public final void setConvertedInput(T convertedInput)
 	{
 		this. = convertedInput;
 	}

Returns:
The parent form for this form component
 
 	public Form<?> getForm()
 	{
 		Form<?> form = Form.findForm(this);
 		if (form == null)
 		{
 			throw new WicketRuntimeException("Could not find Form parent for " + this);
 		}
 		return form;
 	}


Gets the request parameter for this component as a string.

Returns:
The value in the request for this component
 
 	public String getInput()
 	{
 		String[] input = getInputAsArray();
 		if (input == null || input.length == 0)
 		{
 			return null;
 		}
 		else
 		{
 			return trim(input[0]);
 		}
 	}

Gets the request parameters for this component as strings.

Returns:
The values in the request for this component
 
 	public String[] getInputAsArray()
 	{
 		if (!isInputNullable())
 		{
 			if (values != null && values.length == 1 && values[0] == null)
 			{
 				// we the key got passed in (otherwise values would be null),
 				// but the value was set to null.
 				// As the servlet spec isn't clear on what to do with 'empty'
 				// request values - most return an empty string, but some null -
 				// we have to workaround here and deliberately set to an empty
 				// string if the the component is not nullable (text components)
 			}
 		}
 		return values;
 	}

Gets the string to be used for the name attribute of the form element. Generated using the path from the form to the component, excluding the form itself. Override it if you want even a smaller name. E.g. if you know for sure that the id is unique within a form.

Returns:
The string to use as the form element's name attribute
 
 	public String getInputName()
 	{
 		// TODO: keep this in sync with AbstractSubmitLink#getInputName
 		String id = getId();
 		final PrependingStringBuffer inputName = new PrependingStringBuffer(id.length());
 		Component c = this;
 		while (true)
 		{
 			inputName.prepend(id);
 			c = c.getParent();
 			if (c == null || (c instanceof Form<?> && ((Form<?>)c).isRootForm()) ||
 				c instanceof Page)
 			{
 				break;
 			}
 			id = c.getId();
 		}
 
 		// having input name "submit" causes problems with javascript, so we
 		// create a unique string to replace it by prepending a path separator
 		if (inputName.equals("submit"))
 		{
 		}
 		Form<?> form = findParent(Form.class);
 
 		if (form != null)
 		{
 			return form.getInputNamePrefix() + inputName.toString();
 		}
 		else
 		{
 			return inputName.toString();
 		}
 	}

Use hasRawInput() to check if this component has raw input because null can mean 2 things: It doesn't have rawinput or the rawinput is really null.

Returns:
The raw form input that is stored for this formcomponent
 
 	public final String getRawInput()
 	{
 		return .equals() ? null : ;
 	}

Returns:
the type to use when updating the model for this form component
 
 	@SuppressWarnings("unchecked")
 	public final Class<T> getType()
 	{
 		return  == null ? null : (Class<T>)Classes.resolveClass();
 	}

Returns:
prefix used when constructing validator key messages
See also:
Form.getValidatorKeyPrefix()
 
 	{
 		Form<?> form = findParent(Form.class);
 		if (form != null)
 		{
 		}
 		return null;
 	}

Gets an unmodifiable list of validators for this FormComponent.

Returns:
List of validators
 
 	public final List<IValidator<T>> getValidators()
 	{
 		final int size = validators_size();
 		if (size == 0)
 		{
 			return Collections.emptyList();
 		}
 		else
 		{
 			final List<IValidator<T>> list = new ArrayList<IValidator<T>>(size);
 			for (int i = 0; i < sizei++)
 			{
 				list.add(validators_get(i));
 			}
 			return Collections.unmodifiableList(list);
 		}
 	}

Gets current value for a form component, which can be either input data entered by the user, or the component's model object if no input was provided.

Returns:
The value
 
 	public final String getValue()
 	{
 		{
 			return getModelValue();
 		}
 		else
 		{
 			if (getEscapeModelStrings() &&  != null)
 			{
 				return Strings.escapeMarkup().toString();
 			}
 			return ;
 		}
 	}

Returns whether this component has raw input. Raw input is unconverted input straight from the client.

Returns:
boolean whether this component has raw input.
 
 	public final boolean hasRawInput()
 	{
 	}

Used by Form to tell the FormComponent that a new user input is available
 
 	public final void inputChanged()
 	{
 		{
 			// Get input as String array
 			final String[] input = getInputAsArray();
 
 			// If there is any input
 			if (input != null && input.length > 0 && input[0] != null)
 			{
 				// join the values together with ";", for example, "id1;id2;id3"
 				 = StringList.valueOf(input).join();
 			}
 			else if (isInputNullable())
 			{
 				// no input
 				 = null;
 			}
 			else
 			{
 			}
 		}
 	}

Indicate that validation of this form component failed.
 
 	public final void invalid()
 	{
 	}

Gets whether this component's input can be null. By default, components that do not get input will have null values passed in for input. However, component TextField is an example (possibly the only one) that never gets a null passed in, even if the field is left empty UNLESS it had attribute disabled="disabled" set.

Returns:
True if this component's input can be null. Returns true by default.
 
 	public boolean isInputNullable()
 	{
 		return true;
 	}

Returns:
True if this component encodes data in a multipart form submit
 
 	public boolean isMultiPart()
 	{
 		return false;
 	}

Returns:
True if this component supports persistence AND it has been asked to persist itself with setPersistent().
 
 	public final boolean isPersistent()
 	{
 	}

Returns:
whether or not this component's value is required
 
 	public boolean isRequired()
 	{
 	}

Gets whether this component is 'valid'. Valid in this context means that no validation errors were reported the last time the form component was processed. This variable not only is convenient for 'business' use, but is also necessary as we don't want the form component models updated with invalid input.

Returns:
valid whether this component is 'valid'
	public final boolean isValid()
		class IsValidVisitor implements IVisitor
			boolean valid = true;
				final FormComponent<?> fc = (FormComponent<?>)formComponent;
					 = false;
		return tmp.valid;
	}

	public boolean processChildren()
		return true;
	}

This method will retrieve the request parameter, validate it, and if valid update the model. These are the same steps as would be performed by the form. This is useful when a formcomponent is used outside a form.
	public final void processInput()
		else
	}

The value will be made available to the validator property by means of ${label}. It does not have any specific meaning to FormComponent itself.

Parameters:
labelModel
Returns:
this for chaining
	public FormComponent<T> setLabel(IModel<StringlabelModel)
		setLabelInternal(labelModel);
		return this;
	}

Sets the value for a form component this value will be split the string with VALUE_SEPARATOR and calls setModelValue(String[]) with that.

Deprecated:
call or override setModelValue(String[])
Parameters:
value The value
	public void setModelValue(final String value)
	}

Sets the value for a form component.

Parameters:
value The value
	public void setModelValue(final String[] value)
	}

Sets whether this component is to be persisted.

Parameters:
persistent True if this component is to be persisted.
Returns:
this for chaining
	public final FormComponent<T> setPersistent(final boolean persistent)
		else
			throw new UnsupportedOperationException("FormComponent " + getClass() +
				" does not support cookies");
		return this;
	}

Sets the required flag

Parameters:
required
Returns:
this for chaining
	public final FormComponent<T> setRequired(final boolean required)
		if (!required && getType() != null && getType().isPrimitive())
				"FormComponent can't be not required when the type is primitive class: " + this);
		if (required != isRequired())
		return this;
	}

Sets the type that will be used when updating the model for this component. If no type is specified String type is assumed.

Parameters:
type
Returns:
this for chaining
	public final FormComponent<T> setType(Class<?> type)
		 = type == null ? null : type.getName();
		if (type != null && type.isPrimitive())
		return this;
	}

Updates this components model from the request, it expects that the object is already converted through the convertInput() call that is called by the validate() method when a form is being processed. By default it just does this:
 setModelObject(getConvertedInput());
 
DO NOT CALL THIS METHOD DIRECTLY UNLESS YOU ARE SURE WHAT YOU ARE DOING. USUALLY UPDATING YOUR MODEL IS HANDLED BY THE FORM, NOT DIRECTLY BY YOU.
	public void updateModel()
	}


Called to indicate that the user input is valid.
	public final void valid()
	}

Performs full validation of the form component, which consists of calling validateRequired(), convertInput(), and validateValidators(). This method should only be used if the form component needs to be fully validated outside the form process.
	public void validate()
		if (isValid())
			if (isValid())
				if (isRequired() && getConvertedInput() == null && isInputNullable())
				else
	}

Parameters:
validator The validator to add to the validators Object (which may be an array of IValidators or a single instance, for efficiency)
	@SuppressWarnings("unchecked")
	private void validators_add(final IValidator<T> validator)
		if ( == null)
			 = validator;
		else
			// Get current list size
			final int size = validators_size();
			// Create array that holds size + 1 elements
			final IValidator<T>[] validators = new IValidator[size + 1];
			// Loop through existing validators copying them
			for (int i = 0; i < sizei++)
				validators[i] = validators_get(i);
			// Add new validator to the end
			validators[size] = validator;
			// Save new validator list
			this. = validators;
	}

Gets validator from validators Object (which may be an array of IValidators or a single instance, for efficiency) at the given index

Parameters:
index The index of the validator to get
Returns:
The validator
	@SuppressWarnings("unchecked")
	private IValidator<T> validators_get(int index)
		if ( == null)
		if ( instanceof IValidator[])
			return ((IValidator[]))[index];
	}

Returns:
The number of validators in the validators Object (which may be an array of IValidators or a single instance, for efficiency)
	private int validators_size()
		if ( == null)
			return 0;
		if ( instanceof IValidator<?>[])
			return ((IValidator[])).length;
		return 1;
	}

Converts and validates the conversion of the raw input string into the object specified by getType() and records any errors. Converted value is available through getConvertedInput().

Usually the user should do custom conversions by specifying an org.apache.wicket.util.convert.IConverter by registering it with the application by overriding org.apache.wicket.Application.getConverterLocator(), or at the component level by overriding org.apache.wicket.Component.getConverter().

	@SuppressWarnings("unchecked")
	protected void convertInput()
		if ( == null)
			try
				if (e.getResourceKey() != null)
				if (e.getTargetType() != null)
					error.addMessageKey("ConversionError." + Classes.simpleName(e.getTargetType()));
				error.addMessageKey("ConversionError");
		else
			final IConverter converter = getConverter(getType());
			try
				if (e.getResourceKey() != null)
				String simpleName = Classes.simpleName(getType());
				error.addMessageKey("IConverter." + simpleName);
				error.addMessageKey("IConverter");
				error.setVariable("type"simpleName);
	}

Parameters:
e
error
		final Locale locale = e.getLocale();
		if (locale != null)
			error.setVariable("locale"locale);
		error.setVariable("exception"e);
		Format format = e.getFormat();
		if (format instanceof SimpleDateFormat)
			error.setVariable("format", ((SimpleDateFormat)format).toLocalizedPattern());
		Map<StringObjectvariables = e.getVariables();
		if (variables != null)
			error.getVariables().putAll(variables);
	}

Subclasses should overwrite this if the conversion is not done through the type field and the org.apache.wicket.util.convert.IConverter. WARNING: this method may be removed in future versions. If conversion fails then a ConversionException should be thrown

Parameters:
value The value can be the getInput() or through a cookie
Returns:
The converted value. default returns just the given value
Throws:
org.apache.wicket.util.convert.ConversionException If input can't be converted
	@SuppressWarnings("unchecked")
	protected T convertValue(String[] valuethrows ConversionException
		return (T)(value != null && value.length > 0 && value[0] != null ? trim(value[0]) : null);
	}

Returns:
Value to return when model value is needed
	protected String getModelValue()
	}

Gets the request parameter for this component as an int.

Returns:
The value in the request for this component
	protected final int inputAsInt()
		final String string = getInput();
		try
			return Integer.parseInt(string);
				exceptionMessage("Internal error.  Request string '" + string +
					"' not a valid integer"));
	}

Gets the request parameter for this component as an int, using the given default in case no corresponding request parameter was found.

Parameters:
defaultValue Default value to return if request does not have an integer for this component
Returns:
The value in the request for this component
	protected final int inputAsInt(final int defaultValue)
		final String string = getInput();
		if (string != null)
			try
				return Integer.parseInt(string);
				throw new IllegalArgumentException(exceptionMessage("Request string '" + string +
					"' is not a valid integer"));
		else
			return defaultValue;
	}

Gets the request parameters for this component as ints.

Returns:
The values in the request for this component
	protected final int[] inputAsIntArray()
		final String[] strings = getInputAsArray();
		if (strings != null)
			final int[] ints = new int[strings.length];
			for (int i = 0; i < strings.lengthi++)
				ints[i] = Integer.parseInt(strings[i]);
			return ints;
		return null;
	}

	protected void internalOnModelChanged()
		// If the model for this form component changed, we should make it
		// valid again because there can't be any invalid input for it anymore.
	}

Processes the component tag.

	protected void onComponentTag(final ComponentTag tag)
		tag.put("name"getInputName());
		super.onComponentTag(tag);
	}

Sets the temporary converted input value to null.

	protected void onDetach()
		super.onDetach();
	}

Called by onComponentTag(org.apache.wicket.markup.ComponentTag) when the component is disabled. By default, this method will add a disabled="disabled" attribute to the tag. Components may override this method to tweak the tag as they think is fit.

Parameters:
tag the tag that is being rendered
	protected void onDisabled(final ComponentTag tag)
		tag.put("disabled""disabled");
	}

Handle invalidation
	protected void onInvalid()
	}

Handle validation
	protected void onValid()
	}

Determines whether or not this component should trim its input prior to processing it. The default value is true

Returns:
True if the input should be trimmed.
	protected boolean shouldTrimInput()
		return true;
	}

Trims the input according to shouldTrimInput()

Parameters:
string
Returns:
trimmed input if shouldTrimInput() returns true, unchanged input otherwise
	protected final String trim(String string)
		String trimmed = string;
		if (trimmed != null && shouldTrimInput())
			trimmed = trimmed.trim();
		return trimmed;
	}

Returns:
True if this type of FormComponent can be persisted.
	protected boolean supportsPersistence()
		return false;
	}

Checks if the raw input value is not null if this component is required.
	protected final void validateRequired()
	}

Reports required error against this component
	private void reportRequiredError()
	}

Validates this component using the component's validators.
	protected final void validateValidators()
		final int size = validators_size();
		final IValidatable<T> validatable = newValidatable();
		int i = 0;
		IValidator<T> validator = null;
		boolean isNull = getConvertedInput() == null;
		try
			for (i = 0; i < sizei++)
				validator = validators_get(i);
				if (isNull == false || validator instanceof INullAcceptingValidator<?>)
					validator.validate(validatable);
				if (!isValid())
					break;
		catch (Exception e)
			throw new WicketRuntimeException("Exception '" + e + "' occurred during validation " +
				validator.getClass().getName() + " on component " + getPath(), e);
	}

Creates an IValidatable that can be used to validate this form component. This validatable encorporates error key lookups that correspend to this form component. This method is useful when validation needs to happen outside the regular validation workflow but error messages should still be properly reported against the form component.

Returns:
IValidatable<T> for this form component
	public final IValidatable<T> newValidatable()
		return new ValidatableAdapter();
	}

Gets model

Returns:
model
	@SuppressWarnings("unchecked")
	public final IModel<T> getModel()
		return (IModel<T>)getDefaultModel();
	}

Sets model

Parameters:
model
	public final void setModel(IModel<T> model)
	}

Gets model object

Returns:
model object
	@SuppressWarnings("unchecked")
	public final T getModelObject()
	}

Sets model object

Parameters:
object
	public final void setModelObject(T object)
New to GrepCode? Check out our FAQ X