Dojo validation of a textarea

后端 未结 5 2049
夕颜
夕颜 2020-12-11 04:39

I\'m attempting to use dojo for the first time, so this may be be obvious.

I have a very simple form with one textarea in it that needs to be filled in.

5条回答
  •  粉色の甜心
    2020-12-11 05:04

    Instead of copying the whole existing classes, it's enough to use mixin:

    define(["dojo/_base/declare", "dojo/_base/lang", "dijit/form/SimpleTextarea", "dijit/form/ValidationTextBox"],
    function(declare, lang, SimpleTextarea, ValidationTextBox) {
    
      return declare('dijit.form.ValidationTextArea', [SimpleTextarea, ValidationTextBox], {
        constructor: function(params){
          this.constraints = {};
          this.baseClass += ' dijitValidationTextArea';
        },    
        templateString: "",
        validator: function(value, constraints) {
          return (new RegExp("^(?:" + this._computeRegexp(constraints) + ")"+(this.required?"":"?")+"$",["m"])).test(value) &&
            (!this.required || !this._isEmpty(value)) &&
            (this._isEmpty(value) || this.parse(value, constraints) !== undefined); // Boolean
        }
        })
      })
    

    Unfortunatelly, I wasn't able to get exactly the same behaviour with red exclamation sign as with validating input - because of Textarea resizing factility, so I've done the CSS trick:

    .dijitValidationTextAreaError, .dijitValidationTextAreaError.dijitTextBoxHover {
     background-image: url("error.png");
     background-position: right;
     background-repeat: no-repeat;
    }
    

    The error.png needs to be copied from claro theme to your css location. It is displayed inside the text area, not outside it, but it's the only, quite minor difference.

提交回复
热议问题