Пример #1
0
  /*
   * (non-Javadoc)
   *
   * @see org.apache.wicket.behavior.Behavior#onComponentTag(org.apache.wicket.Component,
   * org.apache.wicket.markup.ComponentTag)
   */
  @Override
  public void onComponentTag(Component component, ComponentTag tag) {
    super.onComponentTag(component, tag);

    if (!Form.class.isAssignableFrom(component.getClass())) {
      throw new WicketRuntimeException("This behavior is only applicable on a Form component");
    }

    Form<?> form = (Form<?>) component;

    // Retrieve and set form name
    String formName = verifyFormName(form, tag);

    tag.put("onsubmit", "return yav.performCheck('" + formName + "', rules);");

    // Open the Yav script (inlined JavaScript)
    AppendingStringBuffer buffer = new AppendingStringBuffer("<script>\n");
    buffer.append("var rules=new Array();\n");

    // Visit all form components and check for validators (and write the
    // appropriate Yav rules in the current inlined JavaScript)
    form.visitFormComponents(new YavFormComponentVisitor(buffer, form));

    // Build the call to the yav.init with the proper form name
    buffer.append("function yavInit() {\n");
    buffer.append("    yav.init('" + formName + "', rules);\n");
    buffer.append("}\n");

    // Close the Yav script
    buffer.append("</script>\n");

    // Write the generated script into the response
    Response response = RequestCycle.get().getResponse();
    response.write(buffer.toString());
  }