Esempio n. 1
0
  /**
   * Validates the given Object
   *
   * @param object the Object to validate
   * @param resolver the ClassDescriptorResolver to load ClassDescriptors for use during validation.
   */
  public void validate(Object object, ClassDescriptorResolver resolver) throws ValidationException {

    if (object == null) {
      throw new ValidationException("Cannot validate a null Object.");
    }

    if (resolver == null) {
      resolver = new ClassDescriptorResolverImpl();
    }

    XMLClassDescriptor classDesc = resolver.resolve(object.getClass());

    // -- we cannot validate an object if ClassDescriptor is null
    if (classDesc == null) return;

    TypeValidator validator = classDesc.getValidator();

    XMLFieldDescriptor fieldDesc = null;

    try {
      if (validator != null) {
        if (validator instanceof ClassValidator)
          ((ClassValidator) validator).validate(object, resolver);
        else validator.validate(object);
      }
      // -- default validation
      else {
        // -- just validate each field
        FieldDescriptor[] fields = classDesc.getFields();
        if (fields != null) {
          for (int i = 0; i < fields.length; i++) {
            fieldDesc = (XMLFieldDescriptor) fields[i];
            if (fieldDesc == null) continue;
            FieldValidator fieldValidator = fieldDesc.getValidator();
            if (fieldValidator != null) fieldValidator.validate(object, resolver);
          }
        }
      }
    } catch (ValidationException vx) {
      // -- add location information
      XPathLocation loc = (XPathLocation) vx.getLocation();
      if (loc == null) {
        loc = new XPathLocation();
        vx.setLocation(loc);
        if (fieldDesc != null) {
          if (fieldDesc.getNodeType() == NodeType.Attribute)
            loc.addAttribute(fieldDesc.getXMLName());
          else loc.addChild(fieldDesc.getXMLName());
        }
      }
      loc.addParent(classDesc.getXMLName());
      throw vx;
    }
  } // -- validate
Esempio n. 2
0
 @Override
 public void validate(String value) throws IllegalArgumentException {
   if (isRequired()) {
     if (value == null || value.trim().isEmpty()) {
       throw new IllegalArgumentException(String.format("%s is required", getLabel()));
     }
     if (validator != null) {
       validator.validate(value);
     }
   }
 }
Esempio n. 3
0
  /**
   * Render the upload tags.
   *
   * @param writer Writer to output markup
   */
  protected void beginRender(MarkupWriter writer) {
    formSupport.setEncodingType(MULTIPART_ENCTYPE);

    writer.element(
        "input", "type", "file", "name", getControlName(), "id", getClientId(), "class", cssClass);

    validate.render(writer);

    resources.renderInformalParameters(writer);

    decorateInsideField();

    // TAPESTRY-2453
    if (request.isXHR()) {
      javaScriptSupport.require("t5/core/injected-upload").with(getClientId());
    }
  }
Esempio n. 4
0
  void beginRender(MarkupWriter writer) {
    writer.element("select", "name", getControlName(), "id", getClientId());

    putPropertyNameIntoBeanValidationContext("value");

    validate.render(writer);

    removePropertyNameFromBeanValidationContext();

    resources.renderInformalParameters(writer);

    decorateInsideField();

    // Disabled is via a mixin

    if (this.zone != null) {
      Link link = resources.createEventLink(CHANGE_EVENT);

      JSONObject spec =
          new JSONObject("selectId", getClientId(), "zoneId", zone, "url", link.toURI());

      javascriptSupport.addInitializerCall("linkSelectToZone", spec);
    }
  }
  public void testGetValidatorsForInterface() {
    List validatorList =
        annotationActionValidatorManager.getValidators(AnnotationDataAware2.class, alias);

    // 1 in interface hierarchy, 2 from parent interface (1 default + 1 context)
    assertEquals(3, validatorList.size());

    final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(0);
    assertEquals("data", dataValidator1.getFieldName());
    assertTrue(dataValidator1 instanceof RequiredFieldValidator);

    final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(1);
    assertEquals("data", dataValidator2.getFieldName());
    assertTrue(dataValidator2 instanceof RequiredStringValidator);

    final FieldValidator blingValidator = (FieldValidator) validatorList.get(2);
    assertEquals("bling", blingValidator.getFieldName());
    assertTrue(blingValidator instanceof RequiredStringValidator);
  }
 private boolean isValidField(ExprNodeFieldDesc field) {
   return fieldValidator == null || fieldValidator.validate(field);
 }
  public void no_testGetValidatorsFromInterface() {
    List validatorList =
        annotationActionValidatorManager.getValidators(SimpleAnnotationAction3.class, alias);

    // 17 in the class hierarchy + 1 in the interface + 1 in interface alias
    assertEquals(19, validatorList.size());

    final FieldValidator v = (FieldValidator) validatorList.get(0);
    assertEquals("bar", v.getFieldName());
    assertTrue(v instanceof RequiredFieldValidator);

    final FieldValidator v1 = (FieldValidator) validatorList.get(1);
    assertEquals("bar", v1.getFieldName());
    assertTrue(v1 instanceof IntRangeFieldValidator);

    final FieldValidator vdouble = (FieldValidator) validatorList.get(2);
    assertEquals("percentage", vdouble.getFieldName());
    assertTrue(vdouble instanceof DoubleRangeFieldValidator);

    final FieldValidator v2 = (FieldValidator) validatorList.get(3);
    assertEquals("baz", v2.getFieldName());
    assertTrue(v2 instanceof IntRangeFieldValidator);

    final FieldValidator v3 = (FieldValidator) validatorList.get(4);
    assertEquals("date", v3.getFieldName());
    assertTrue(v3 instanceof DateRangeFieldValidator);

    // action-level validator comes first
    final Validator v4 = (Validator) validatorList.get(5);
    assertTrue(v4 instanceof ExpressionValidator);

    // action-level validator comes first
    final Validator v5 = (Validator) validatorList.get(6);
    assertTrue(v5 instanceof ExpressionValidator);

    // action-level validator comes first
    final Validator v6 = (Validator) validatorList.get(7);
    assertTrue(v6 instanceof ExpressionValidator);

    // action-level validator comes first
    final Validator v7 = (Validator) validatorList.get(8);
    assertTrue(v7 instanceof ExpressionValidator);

    // action-level validator comes first
    final Validator v8 = (Validator) validatorList.get(9);
    assertTrue(v8 instanceof ExpressionValidator);

    final FieldValidator v9 = (FieldValidator) validatorList.get(10);
    assertEquals("datefield", v9.getFieldName());
    assertTrue(v9 instanceof DateRangeFieldValidator);

    final FieldValidator v10 = (FieldValidator) validatorList.get(11);
    assertEquals("emailaddress", v10.getFieldName());
    assertTrue(v10 instanceof EmailValidator);

    final FieldValidator v11 = (FieldValidator) validatorList.get(12);
    assertEquals("intfield", v11.getFieldName());
    assertTrue(v11 instanceof IntRangeFieldValidator);

    final FieldValidator v12 = (FieldValidator) validatorList.get(13);
    assertEquals("customfield", v12.getFieldName());
    assertTrue(v12 instanceof RequiredFieldValidator);

    final FieldValidator v13 = (FieldValidator) validatorList.get(14);
    assertEquals("stringisrequired", v13.getFieldName());
    assertTrue(v13 instanceof RequiredStringValidator);

    final FieldValidator v14 = (FieldValidator) validatorList.get(15);
    assertEquals("needstringlength", v14.getFieldName());
    assertTrue(v14 instanceof StringLengthFieldValidator);

    final FieldValidator v15 = (FieldValidator) validatorList.get(16);
    assertEquals("hreflocation", v15.getFieldName());
    assertTrue(v15 instanceof URLValidator);

    final FieldValidator v16 = (FieldValidator) validatorList.get(17);
    assertEquals("data", v16.getFieldName());
    assertTrue(v16 instanceof RequiredFieldValidator);

    final FieldValidator v17 = (FieldValidator) validatorList.get(18);
    assertEquals("data", v17.getFieldName());
    assertTrue(v17 instanceof RequiredStringValidator);
  }
Esempio n. 8
0
  public void beginRender(MarkupWriter writer) {
    String value = tracker.getInput(this);

    if (value == null) {
      value = formatCurrentValue();
    }

    String clientId = getClientId();
    String triggerId = clientId + "-trigger";

    writer.element(
        "input",
        "type",
        hideTextField ? "hidden" : "text",
        "name",
        getControlName(),
        "id",
        clientId,
        "value",
        value);

    if (isDisabled()) {
      writer.attributes("disabled", "disabled");
    }

    validate.render(writer);

    resources.renderInformalParameters(writer);

    decorateInsideField();

    writer.end();

    // Now the trigger icon.

    writer.element(
        "img",
        "id",
        triggerId,
        "class",
        "t-calendar-trigger",
        "src",
        icon.toClientURL(),
        "alt",
        "[Show]");
    writer.end(); // img

    writeTimeZone(writer);

    JSONObject spec =
        new JSONObject(
                "clientId", clientId, "clientDateFormat", formatConverter.convertToClient(format))
            .put("time", time)
            .put("singleClick", singleClick);

    if (max != null) {
      spec.put("max", convertDateToClientTimeZone(max).getTime());
    }

    if (min != null) {
      spec.put("min", convertDateToClientTimeZone(min).getTime());
    }

    javascriptSupport.addInitializerCall("tapxDateField", spec);
  }
Esempio n. 9
0
 /** @since 5.4 */
 @Override
 public boolean isRequired() {
   return validate.isRequired();
 }