package org.apache.wicket.markup.html.form;
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
- Author(s):
- Jonathan Locke
- Eelco Hillenius
- Johan Compagner
- Igor Vaynberg (ivaynberg)
Visitor for traversing form components
Typesafe interface to code that is called when visiting a form component.
Called when visiting a form component
- Parameters:
formComponent
The form component- Returns:
- component
resource = prefix + getId() + "." + key;
message = getString(localizer, resource, formComponent);
message = getString(localizer, resource, formComponent);
return localizer.getString(key, component, "");
Creates a new params map that additionally contains the default input, name, label
parameters
- Parameters:
params
original params map- Returns:
- new params map
- Returns:
- value of label param for this form component
Change object to capture the required flag change
- Author(s):
- Igor Vaynberg (ivaynberg)
Whether this form component should save and restore state between sessions. This is false by
default.
Whether or not this component's value is required (non-empty)
Make empty strings null values boolean. Used by AbstractTextComponent subclass.
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 traversalvisitor
The visitor to call
if (container.size() > 0)
boolean visitChildren = true;
Raw Input entered by the user or NO_RAW_INPUT if nothing is filled in.
Type that the raw input string will be converted to
The list of validators for this form component as either an IValidator instance or an array
of IValidator instances.
Adds a validator to this form component.
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
buffer.append("Could not locate error message for component: "); buffer.append(" and error: "); buffer.append(". Tried keys: "); 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
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
- Returns:
- The parent form for this form component
Gets the request parameter for this component as a string.
- Returns:
- The value in the request for this component
if (input == null || input.length == 0)
Gets the request parameters for this component as strings.
- Returns:
- The values in the request for this component
if (values != null && values.length == 1 && values[0] == null)
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
if (inputName.equals("submit")) 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
- Returns:
- the type to use when updating the model for this form component
Gets an unmodifiable list of validators for this FormComponent.
- Returns:
- List of validators
for (int i = 0; i < size; i++)
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 whether this component has raw input. Raw input is unconverted input straight from
the client.
- Returns:
- boolean whether this component has raw input.
Used by Form to tell the FormComponent that a new user input is available
if (input != null && input.length > 0 && input[0] != null)
Indicate that validation of this form component failed.
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.
- Returns:
- True if this component encodes data in a multipart form submit
- Returns:
- True if this component supports persistence AND it has been asked to persist itself
with setPersistent().
- Returns:
- whether or not this component's value is required
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'
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.
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
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
Sets the value for a form component.
- Parameters:
value
The value
Sets whether this component is to be persisted.
- Parameters:
persistent
True if this component is to be persisted.- Returns:
- this for chaining
" does not support cookies");
Sets the required flag
- Parameters:
required- Returns:
- this for chaining
"FormComponent can't be not required when the type is primitive class: " + 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
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.
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.
- Parameters:
validator
The validator to add to the validators Object (which may be an array of
IValidators or a single instance, for efficiency)
for (int i = 0; i < size; i++)
validators[size] = validator;
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
- Returns:
- The number of validators in the validators Object (which may be an array of
IValidators or a single instance, for efficiency)
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()
Subclasses should overwrite this if the conversion is not done through the type field and the
IConverter.
WARNING: this method may be removed in future versions.
If conversion fails then a ConversionException should be thrown
return value != null && value.length > 0 && value[0] != null ? trim(value[0]) : null;
- Returns:
- Value to return when model value is needed
Gets the request parameter for this component as an int.
- Returns:
- The value in the request for this component
"' 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)
"' is not a valid integer"));
Gets the request parameters for this component as ints.
- Returns:
- The values in the request for this component
final int[] ints = new int[strings.length];
for (int i = 0; i < strings.length; i++)
Processes the component tag.
Sets the temporary converted input value to null.
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
tag.put("disabled", "disabled"); 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.
trimmed = trimmed.trim();
- Returns:
- True if this type of FormComponent can be persisted.
Checks if the raw input value is not null if this component is required.
Reports required error against this component
Validates this component using the component's validators.
for (i = 0; i < size; i++)