예제 #1
0
  /**
   * @param productForm
   * @return 返回一个包含错误信息的字符串列表 若返回为 空 则表示输入合法
   */
  public List<String> validate(ProductForm productForm) {
    List<String> errors = new ArrayList<>();
    String name = productForm.getName();
    if (name == null || name.trim().isEmpty()) {
      errors.add("Product must have a name");
    }
    String price = productForm.getPrice();
    if (price == null || price.trim().isEmpty()) {
      errors.add("Product must have a price");
    } else {
      try {
        Float.parseFloat(price);
      } catch (NumberFormatException e) {
        errors.add("Invalid price value");
      }
    }

    return errors;
  }