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

你说的曾经没有我的故事 提交于 2019-12-02 19:03:08

问题


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 ...



来源:https://stackoverflow.com/questions/19317258/how-to-use-dijit-textarea-validation-dojo-1-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!