如何验证joomla 2.5默认文本编辑器?

时间:2013-02-15 13:52:45

标签: jquery joomla validation joomla2.5

如何使用jQuery验证joomla默认文本编辑器的模糊效果?

这是我使用的代码获取文本编辑器。例如,在/models/forms/newsitem.xml中:

    <field name="description" type="editor" buttons="true" 
                   hide="pagebreak,readmore,article,image"
                   class="inputbox required"
                   filter="JComponentHelper::filterText"
                   label="JGLOBAL_DESCRIPTION" 
                   description="COM_NEWSITEMS_FIELD_DESCRIPTION_DESC" required="true" />

第二个文件(/views/.../edit.php) - 要添加到javascript的新对象是什么?

<script type="text/javascript">
    Joomla.submitbutton = function(task) {
        if (task == 'newsitem.cancel' || document.formvalidator.isValid(document.id('newsitem-form'))) {
            <?php echo $this->form->getField('description')->save(); ?>
            Joomla.submitform(task, document.getElementById('newsitem-form'));
        } else {
            alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED'));?>');
        }
    }
</script>

我想要的是我想在文本编辑器旁边显示一条警告消息,说“你不能留空”。我可以在表单提交中完成,但真正的挑战是如何在模糊中进行。

2 个答案:

答案 0 :(得分:0)

此代码document.formvalidator.isValid(document.id('newsitem-form'))验证您的表单,如果您可以尝试此表单中只有编辑器 -

jQuery('#editorid').blur(function() {
  if(!document.formvalidator.isValid(document.id('newsitem-form'))){
    //Add your error message code 

  }
});

<强>更新: -

jQuery(jQuery('iframe')[0].contentWindow).blur(function() {
 var editor_text = jQuery('iframe').contents().find('body').text();
 if(editor_text=='')alert('Error message');
});

注意: - 使用指向编辑器iframe的有效选择器,只有在有一个编辑器时才会有效

希望这会有所帮助。

答案 1 :(得分:0)

我知道了。更改了答案(login- Irfan ),但应该是 - 如果单击“保存”,则编辑器会显示错误(空文本)。这是一个例子:

<script type="text/javascript">    
    Joomla.submitbutton = function(task) {
        if (task == 'newsitem.cancel' || document.formvalidator.isValid(document.id('newsitem-form'))) {
          if(task != 'newsitem.cancel') {
            var boolSubmit = true;
            if(jQuery('#jform_description_parent').is(':visible')) {
                jQuery(jQuery('#jform_description_parent iframe')[0].contentWindow).each(function() {
                    var editor_text = jQuery('#jform_description_parent iframe').contents().find('body').text();
                    if(editor_text=='') {
                        alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED'));?>');
                        boolSubmit = false
                    }
                });
            } else {
                jQuery('textarea#jform_description').each(function() {
                    if(jQuery(this).val()=='') {
                        alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED'));?>');
                        boolSubmit = false;
                    }
                });
            }
            if(!boolSubmit)
               return boolSubmit;
          }
          <?php echo $this->form->getField('description')->save(); ?>            
          Joomla.submitform(task, document.getElementById('newsitem-form'));
        } else {
            alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED'));?>');
        }
    }
</script>

好的结论?我想可能没事。

相关问题