聚合物纸输入有效性标志

时间:2017-05-12 16:20:35

标签: javascript validation polymer-1.0 paper-elements

我正在尝试使用聚合物元素,目前正在尝试使用纸张形式元素。

我还没有设法找到是否有可能在Javascript变量中获得纸质表格有效的事实。有什么想法吗?

我正在使用自动验证。

 /**
   * Parse a reponse based on the type
   * @param {Response} response
   * @returns {Promise} <resolve: *, reject: Error>
   */
  const parseResponse = (response) => {
    const contentType = (response.headers.get('content-type') || '').split(';')[0];
    if (contentType === 'application/json') {
      return response.json();
    } else if (contentType === 'multipart/form-data') {
      return response.formData();
    } else if (contentType === 'text/html') {
      return response.text();
    } else if (contentType === 'application/octet-stream') {
      return response.blob();
    }
  };


  /**
   * Check for API-level errors
   * @param {Response} response
   * @returns {Promise} <resolve: Response, reject: Error>
   */
  const checkStatus = (response) =>
    new Promise((resolve, reject) => {
      if (response.ok) {
        return resolve(response);
      }
      parseResponse(response)
        .then(reject)
        .catch(reject);
    });

  /**
   * Create a new Request object
   * @param {String} method
   * @param {String} route
   * @param {*} [data]
   * @param {Object} [options]
   * @returns {Request}
   */
  const buildRequest = (method, route, data = null, definedOptions = {}) => {
    const options = Object.assign({}, defaultOptions, validateOptions(definedOptions));
    const body = () => data ? { body: options.json ? JSON.stringify(data) : data } : {};
    const baseOptions = {
      method: method.toUpperCase(),
      mode: options.mode,
      headers: new Headers(headers(options.headers)),
    };
    const requestOptions = Object.assign({}, baseOptions, body());
    return new Request(getURL(route), requestOptions);
  };

  /**
   * Execute a request using fetch
   * @param {String} method
   * @param {String} route
   * @param {*} [body]
   * @param {Object} [options]
   */
  const executeRequest = (method, route, body, options) =>
    new Promise((resolve, reject) => {
      fetch(buildRequest(method, route, body, options))
        .then(checkStatus)
        .then(parseResponse)
        .then(resolve)
        .catch(reject);

非常感谢!

1 个答案:

答案 0 :(得分:2)

There is a property on the paper-input element called invalid which you can check to see if the value of the input is valid or not.

For example:

<paper-input id="foo"
  name="foo"
  label="Foo"
  required
  auto-validate
  pattern="[A-Za-z0-9]+"
  invalid="{{invalid}}"
  error-message="Alphanumerical characters only">
</paper-input>

And then in the JavaScript you can call

var invalid = this.$.foo.invalid;

See the docs here for more info.