-->

How to use dijit/Textarea validation (Dojo 1.9)?

2019-03-04 21:04发布

问题:

I have textarea which is required field. I've found post suggesting that Dojo doesn't have validation for Textarea, but in Dojo 1.9, there's an argument 'required'.

I've done the following:

new Textarea({required:true, value:""}, query('[name=description]')[0])

but the effect isn't what I've expected. The texarea has red border always, even if the field wasn't focused (as opposite to, for example, ValidationTextBox). But when I call:

form.validate()

the validation is passed even if the texarea is empty.

Is it possible to get Textare behave the same as in ValidationTextBox, or as for now, the validation for that component is not yet ready and I'd have to write custom version (as in linked post) or wait for next Dojo?

回答1:

I've done it using mixin of SimpleTextArea and ValidationTextArea:

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: "<textarea ${!nameAttrSetting} data-dojo-attach-point='focusNode,containerNode,textbox' autocomplete='off'></textarea>"
  })
})

See also my answer in Dojo validation of a textarea



回答2:

The power of Dojo lies in extending it with ease. If you really need the required functionality, then implement it. If you design it well, there should be no problem if it actually gets implemented in a new release of Dojo.

If you really want to know if such a feature exists or is in development I suggest looking at http://bugs.dojotoolkit.org. Besides, you can always contribute to the code, that's what open source is meant for.



回答3:

I would like to add to the answer of Donaudampfschifffreizeitfahrt

instead of "this.baseClass += ' dijitValidationTextArea';"

I would do

this.baseClass = this.baseClass.replace('dijitTextBox', 'dijitValidationTextArea');

because

• we do not need the TextBox class if we have got a textarea mixin

• ! the "rows" parameter is mixed in but not fired/styled if the TextBox class is present ...