📄 formcomponent.java
字号:
* Checks if the form component's 'required' requirement is met. This method should typically * only be called when {@link #isRequired()} returns true. * * @return true if the 'required' requirement is met, false otherwise */ 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()) { // 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() { rawInput = NO_RAW_INPUT; } /** * Reports a validation error against this form component. * * The actual error is reported by creating a {@link ValidationErrorFeedback} object that holds * both the validation error and the generated error message - so a custom feedback panel can * have access to both. * * @param error * validation error */ public void error(IValidationError error) { if (error == null) { throw new IllegalArgumentException("Argument [[error]] cannot be null"); } String message = error.getErrorMessage(new MessageSource()); if (message == null) { // XXX maybe make message source remember tried resource keys so a // more detailed error message can be created - like show which keys // were tried message = "Could not locate error message for error: " + error.toString(); } error(new ValidationErrorFeedback(error, message)); } /** * Gets the converted input. The converted input is set earlier though the implementation of * {@link #convertInput()}. * * @return value of input possibly converted into an appropriate type */ public final Object getConvertedInput() { return convertedInput; } /** * Sets the converted input. This method is typically not called by clients, unless they * override {@link #convertInput()}, in which case they should call this method to update the * input for this component instance. * * @param convertedInput * the converted input */ public final void setConvertedInput(Object convertedInput) { this.convertedInput = convertedInput; } /** * @return The parent form for this form component */ public Form getForm() { class FindFormVisitor implements Component.IVisitor { Form form = null; public Object component(Component component) { form = (Form)component; return Component.IVisitor.STOP_TRAVERSAL; } } Form form = (Form)findParent(Form.class); if (form == null) { // check whether the form is a child of a surrounding border final Border border = (Border)findParent(Border.class); if (border != null) { FindFormVisitor formVisitor = new FindFormVisitor(); border.visitChildren(Form.class, formVisitor); form = formVisitor.form; } 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. * * @return The value in the request for this component */ public String getInput() { String[] input = getInputAsArray(); if (input == null || input.length == 0) { return null; } else { return input[0].trim(); } } /** * Gets the request parameters for this component as strings. * * @return The values in the request for this component */ public String[] getInputAsArray() { String[] values = getRequest().getParameters(getInputName()); 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 EMPTY_STRING_ARRAY; } } return values; } /** * Gets the string to be used for the <tt>name</tt> 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. * * @return 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; } inputName.prepend(Component.PATH_SEPARATOR); 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")) { inputName.prepend(Component.PATH_SEPARATOR); } 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. * * @return The raw form input that is stored for this formcomponent */ public final String getRawInput() { return NO_RAW_INPUT.equals(rawInput) ? null : rawInput; } /** * @return the type to use when updating the model for this form component */ public final Class getType() { return typeName == null ? null : Classes.resolveClass(typeName); } /** * @see Form#getValidatorKeyPrefix() * @return prefix used when constructing validator key messages */ public String getValidatorKeyPrefix() { Form form = (Form)findParent(Form.class); if (form != null) { return getForm().getValidatorKeyPrefix(); } return null; } /** * Gets an unmodifiable list of validators for this FormComponent. * * @return List of validators */ public final List getValidators() { final int size = validators_size(); if (size == 0) { return Collections.EMPTY_LIST; } else { final List list = new ArrayList(size); for (int i = 0; i < size; i++) { 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. * * @return The value */ public final String getValue() { if (NO_RAW_INPUT.equals(rawInput)) { return getModelValue(); } else { if (getEscapeModelStrings() && rawInput != null) { return Strings.escapeMarkup(rawInput).toString(); } return rawInput; } } /** * Returns whether this component has raw input. Raw input is unconverted input straight from * the client. * * @return boolean whether this component has raw input. */ public final boolean hasRawInput() { return !NO_RAW_INPUT.equals(rawInput); } /** * Used by Form to tell the FormComponent that a new user input is available */ public final void inputChanged() { if (isVisibleInHierarchy() && isEnabled()) { // 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" rawInput = StringList.valueOf(input).join(VALUE_SEPARATOR); } else if (isInputNullable()) { // no input rawInput = null; } else { rawInput = NO_RAW_INPUT; } } } /** * Indicate that validation of this form component failed. */ public final void invalid() { onInvalid(); } /** * 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 <code>disabled="disabled"</code> set. * * @return True if this component's input can be null. Returns true by default. */ public boolean isInputNullable() { return true; } /** * @return True if this component encodes data in a multipart form submit */ public boolean isMultiPart() { return false; } /** * @return True if this component supports persistence AND it has been asked to persist itself * with setPersistent(). */ public final boolean isPersistent() { return supportsPersistence() && getFlag(FLAG_PERSISTENT); } /** * @return whether or not this component's value is required */ public boolean isRequired() { return getFlag(FLAG_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. * * @return valid whether this component is 'valid' */ public final boolean isValid() { class IsValidVisitor implements IVisitor { boolean valid = true; public Object formComponent(IFormVisitorParticipant formComponent) { final FormComponent fc = (FormComponent)formComponent; if (fc.hasErrorMessage()) { valid = false; return Component.IVisitor.STOP_TRAVERSAL; } return Component.IVisitor.CONTINUE_TRAVERSAL; } } IsValidVisitor tmp = new IsValidVisitor(); visitFormComponentsPostOrder(this, tmp); return tmp.valid; } /** * @see IFormVisitorParticipant#processChildren() */ 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() { inputChanged(); validate(); if (hasErrorMessage()) { invalid(); } else { valid(); updateModel(); } } /** * The value will be made available to the validator property by means of ${label}. It does not * have any specific meaning to FormComponent itself. * * @param labelModel * @return this for chaining */ public FormComponent setLabel(IModel labelModel) { setLabelInternal(labelModel); return this; } /** * Sets the value for a form component this value will be split the string with * {@link FormComponent#VALUE_SEPARATOR} and calls setModelValue(String[]) with that. * * @param value * The value * * @deprecated call or override setModelValue(String[]) */ public void setModelValue(final String value) { setModelValue(value.split(VALUE_SEPARATOR)); } /** * Sets the value for a form component. * * @param value * The value */ public void setModelValue(final String[] value) { convertedInput = convertValue(value); updateModel(); } /** * Sets whether this component is to be persisted. * * @param persistent * True if this component is to be persisted. * @return this for chaining */ public final FormComponent setPersistent(final boolean persistent) { if (supportsPersistence()) { setFlag(FLAG_PERSISTENT, persistent); } else { throw new UnsupportedOperationException("FormComponent " + getClass() + " does not support cookies"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -