【问题标题】:Show error message in textfield in Yii在 Yii 的文本字段中显示错误消息
【发布时间】:2014-04-30 20:13:49
【问题描述】:

我在 Yii 活动表单中使用 'enableClientValidation'=>true 来启用客户端验证。它工作正常,但我想在字段中而不是在字段下显示错误消息。 我根据Yii. How to add css "error" class to input on form submit? 工作,代码在文本字段上添加了error_input 类,但文本字段下仍然出现错误。 应感谢任何帮助。

我的代码是

<?php 
     $form = $this->beginWidget('CActiveForm', array(
      'id'=>'contact-form',
      'enableClientValidation'=>true,
      'clientOptions'=>array(
          'validateOnSubmit'=>true,
          'afterValidate' => 'js:function(form, data, hasError) { 
              if(hasError) {
                  for(var i in data) $("#"+i).addClass("error_input");
                  return false;
              }
              else {
                  form.children().removeClass("error_input");
                  return true;
              }
          }',
          'afterValidateAttribute' => 'js:function(form, attribute, data, hasError) {
              if(hasError) 
                 $("#"+attribute.id).addClass("error_input");
              else 
                 $("#"+attribute.id).removeClass("error_input"); 
          }'
            ),

    )); ?>


    <div class="margin-bottom3 label-width1"> <?php echo $form->labelEx($Customer,'restaurant_name',array('class'=>'label-texts')); ?></div>
    <div class="name-common-div"> <?php echo $form->textField($Customer,'restaurant_name',array('class'=>'text-background customer_fields')) ?>
    <?php 
          echo $this->msg; 
          echo $form->error($Customer,'restaurant_name',array('class'=>'check-add-member-error')); 
    ?>
 </div>

我该怎么做?`

【问题讨论】:

  • 这是更多的 css 问题。我个人为文本字段+错误制作了包装器。浮动离开它们并给出宽度。然后只需为包装器添加错误(如果您想用红色突出显示文本字段+错误)或将错误添加到 errormsg 类。

标签: php jquery css validation yii


【解决方案1】:

你可能想看看 yii 的引导扩展 (http://www.yiiframework.com/extension/bootstrap/‎)。它允许我们使用占位符来做你想做的事,所以它应该比创建你自己的 css 更容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 2013-02-26
    相关资源
    最近更新 更多