📄 form.java
字号:
* @see org.apache.wicket.Component#internalOnModelChanged() */ protected void internalOnModelChanged() { // Visit all the form components and validate each visitFormComponentsPostOrder(new FormComponent.AbstractVisitor() { public void onFormComponent(final FormComponent formComponent) { // If form component is using form model if (formComponent.sameInnermostModel(Form.this)) { formComponent.modelChanged(); } } }); } /** * Mark each form component on this form invalid. */ protected final void markFormComponentsInvalid() { // call invalidate methods of all nested form components visitFormComponentsPostOrder(new FormComponent.AbstractVisitor() { public void onFormComponent(final FormComponent formComponent) { if (formComponent.isVisibleInHierarchy()) { formComponent.invalid(); } } }); } /** * Mark each form component on this form and on nested forms valid. */ protected final void markFormComponentsValid() { internalMarkFormComponentsValid(); markNestedFormComponentsValid(); } /** * Mark each form component on nested form valid. */ private void markNestedFormComponentsValid() { visitChildren(Form.class, new IVisitor() { public Object component(Component component) { Form form = (Form)component; if (form.isEnableAllowed() && form.isEnabled() && form.isVisibleInHierarchy()) { form.internalMarkFormComponentsValid(); return CONTINUE_TRAVERSAL; } return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER; } }); } /** * Mark each form component on this form valid. */ private void internalMarkFormComponentsValid() { // call valid methods of all nested form components visitFormComponentsPostOrder(new FormComponent.AbstractVisitor() { public void onFormComponent(final FormComponent formComponent) { if (formComponent.getForm() == Form.this && formComponent.isVisibleInHierarchy()) { formComponent.valid(); } } }); } /** * @see org.apache.wicket.Component#onComponentTag(ComponentTag) */ protected void onComponentTag(final ComponentTag tag) { super.onComponentTag(tag); checkComponentTag(tag, "form"); if (isRootForm()) { String method = getMethod().toLowerCase(); tag.put("method", method); String url = urlFor(IFormSubmitListener.INTERFACE).toString(); if (method.equals("get")) { int i = url.indexOf('?'); String action = (i > -1) ? url.substring(0, i) : ""; tag.put("action", action); // alternatively, we could just put an empty string here, so // that mounted paths stay in good order. I decided against this // as I'm not sure whether that could have side effects with // other encoders } else { tag.put("action", Strings.replaceAll(url, "&", "&")); } if (multiPart) { tag.put("enctype", "multipart/form-data"); } else { // sanity check String enctype = (String)tag.getAttributes().get("enctype"); if ("multipart/form-data".equalsIgnoreCase(enctype)) { // though not set explicitly in Java, this is a multipart // form setMultiPart(true); } } } else { tag.setName("div"); tag.remove("method"); tag.remove("action"); tag.remove("enctype"); } } /** * Append an additional hidden input tag to support anchor tags that can submit a form. * * @param markupStream * The markup stream * @param openTag * The open tag for the body */ protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { if (isRootForm()) { // get the hidden field id String nameAndId = getHiddenFieldId(); // render the hidden field AppendingStringBuffer buffer = new AppendingStringBuffer( "<div style=\"display:none\"><input type=\"hidden\" name=\"").append(nameAndId) .append("\" id=\"") .append(nameAndId) .append("\" />"); String method = getMethod().toLowerCase(); // if it's a get, did put the parameters in the action attribute, // and have to write the url parameters as hidden fields if (method.equals("get")) { String url = urlFor(IFormSubmitListener.INTERFACE).toString(); int i = url.indexOf('?'); String[] params = ((i > -1) ? url.substring(i + 1) : url).split("&"); for (int j = 0; j < params.length; j++) { String[] pair = params[j].split("="); buffer.append("<input type=\"hidden\" name=\"").append(pair[0]).append( "\" value=\"").append(pair.length > 1 ? pair[1] : "").append("\" />"); } } buffer.append("</div>"); getResponse().write(buffer); // if a default submitting component was set, handle the rendering of that if (defaultSubmittingComponent instanceof Component) { final Component submittingComponent = (Component)defaultSubmittingComponent; if (submittingComponent.isVisibleInHierarchy() && submittingComponent.isEnabled()) { appendDefaultButtonField(markupStream, openTag); } } } // do the rest of the processing super.onComponentTagBody(markupStream, openTag); } /** * @see org.apache.wicket.Component#onDetach() */ protected void onDetach() { super.internalOnDetach(); setFlag(FLAG_SUBMITTED, false); super.onDetach(); } /** * Method to override if you want to do something special when an error occurs (other than * simply displaying validation errors). */ protected void onError() { } /** * @see org.apache.wicket.Component#onRender(MarkupStream) */ protected void onRender(final MarkupStream markupStream) { // Force multi-part on if any child form component is multi-part visitFormComponents(new FormComponent.AbstractVisitor() { public void onFormComponent(FormComponent formComponent) { if (formComponent.isVisible() && formComponent.isMultiPart()) { setMultiPart(true); } } }); super.onRender(markupStream); } /** * Implemented by subclasses to deal with form submits. */ protected void onSubmit() { } /** * Update the model of all components on this form and nested forms using the fields that were * sent with the current request. This method only updates models when the Form.validate() is * called first that takes care of the conversion for the FormComponents. * * Normally this method will not be called when a validation error occurs in one of the form * components. * * @see org.apache.wicket.markup.html.form.FormComponent#updateModel() */ protected final void updateFormComponentModels() { internalUpdateFormComponentModels(); updateNestedFormComponentModels(); } /** * Update the model of all components on nested forms. * * @see #updateFormComponentModels() */ private final void updateNestedFormComponentModels() { visitChildren(Form.class, new IVisitor() { public Object component(Component component) { Form form = (Form)component; if (form.isEnabled() && form.isEnableAllowed() && form.isVisibleInHierarchy()) { form.internalUpdateFormComponentModels(); return CONTINUE_TRAVERSAL; } return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER; } }); } /** * Update the model of all components on this form. * * @see #updateFormComponentModels() */ private void internalUpdateFormComponentModels() { visitFormComponentsPostOrder(new ValidationVisitor() { public void validate(FormComponent formComponent) { Form form = formComponent.getForm(); if (form == Form.this) { // Potentially update the model formComponent.updateModel(); } } }); } /** * Validates the form by checking required fields, converting raw input and running validators * for every form component, and last running global form validators. This method is typically * called before updating any models. * <p> * NOTE: in most cases, custom validations on the form can be achieved using an IFormValidator * that can be added using addValidator(). * </p> */ protected void validate() { if (isEnabled() && isEnableAllowed() && isVisibleInHierarchy()) { // since this method can be called directly by users, this additional check is needed validateComponents(); validateFormValidators(); validateNestedForms(); } } /** * Triggers type conversion on form components */ protected final void validateComponents() { visitFormComponentsPostOrder(new ValidationVisitor() { public void validate(final FormComponent formComponent) { final Form form = formComponent.getForm(); if (form == Form.this && form.isEnabled() && form.isEnableAllowed() && form.isVisibleInHierarchy()) { formComponent.validate(); } } }); } /** * Checks if the specified form component visible and is attached to a page * * @param fc * form component * * @return true if the form component and all its parents are visible and there component is in * page's hierarchy */ private boolean isFormComponentVisibleInPage(FormComponent fc) { if (fc == null) { throw new IllegalArgumentException("Argument `fc` cannot be null"); } Component c = fc; Component last = fc; while (c != null) { if (c.isRenderAllowed() && c.isVisible()) { last = c; c = c.getParent(); } else { return false; } } return last == findPage(); } /** * Validates form with the given form validator * * @param validator */ protected final void validateFormValidator(final IFormValidator validator) { if (validator == null) { throw new IllegalArgumentException("Argument [[validator]] cannot be null"); } final FormComponent[] dependents = validator.getDependentFormComponents(); boolean validate = true; if (dependents != null) { for (int j = 0; j < dependents.length; j++) { final FormComponent dependent = dependents[j]; // check if the dependent component is valid if (!dependent.isValid()) { validate = false; break; } // check if the dependent component is visible and is attached to // the page else if (!isFormComponentVisibleInPage(dependent)) { if (log.isWarnEnabled()) { log.warn("IFormValidator in form `" + getPageRelativePath() + "` depends on a component that has been removed from the page or is no longer visible. " + "Offending component id `" + dependent.getId() + "`."); } validate = false; break; } } } if (validate) { validator.validate(this); } } /** * Triggers any added {@link IFormValidator}s. */ protected final void validateFormValidators() { final int count = formValidators_size(); for (int i = 0; i < count; i++) { validateFormValidator(formValidators_get(i)); } } /** * Validates {@link FormComponent}s as well as {@link IFormValidator}s in nested {@link Form}s. * * @see #validate() */ private void validateNestedForms() { visitChildren(Form.class, new IVisitor() { public Object component(Component component) { final Form form = (Form)component; if (form.isEnabled() && form.isEnableAllowed() && form.isVisibleInHierarchy()) { form.validateComponents(); form.validateFormValidators(); return CONTINUE_TRAVERSAL; } return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER; } }); } /** * Change object to keep track of form validator removals * * @author Igor Vaynberg (ivaynberg at apache dot org) */ private class FormValidatorRemovedChange extends Change { private static final long serialVersionUID = 1L; private final IFormValidator removed; /** * Construct. * * @param removed */ public FormValidatorRemovedChange(final IFormValidator removed) { super(); this.removed = removed; } public void undo() { add(removed); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -