hxppb 2012-09-24 17:59:35 5217次浏览 12条回复 1 0 0

用CActiveForm的时候,如下代码所示。

<div class="row">
    <?php echo $form->labelEx($model,'firstName'); ?>
    <?php echo $form->textField($model,'firstName'); ?>
    <?php echo $form->error($model,'firstName'); ?>
</div>

正常使用的时候,如果验证不通过,$form->error($model,'firstName')会输出一个错误,验证通过后,错误信息会隐藏。 现在需要在用户输入前在输入框后显示一个提示。验证不通过在输入框后报错。验证通过在输入框后显示一个绿色的小勾。 要实现如上所说该怎么做呢?CActiveForm能坐到这种效果么,请帮忙提供解决办法。

  • 回复于 2012-09-24 21:42 举报

    印象中不会。看下别人写的了

  • 回复于 2012-09-25 00:37 举报

    可以的:

    <?php $form = $this->beginWidget('CActiveForm', array(
          'id'=>'user-form',
          'enableAjaxValidation'=>true,
          'enableClientValidation'=>true,
          'focus'=>array($model,'firstName'),
    )); ?>
    

    详情见:http://www.yiiframework.com/doc/api/1.1/CActiveForm

  • 回复于 2012-09-25 01:14 举报

    这个不是一般的验证嘛~~这个我懂,问题是他要实现没有验证通过显示图片~然后验证通过了显示成功的图片。。感觉要通过js的方式来验证吧,通过ajax先判断,然后输出相对应的图片,这样么

  • 回复于 2012-09-25 09:58 举报

    请小龙给出具体是用什么方法或者属性。因为就像前面我提到过的正常的验证这个是没问题。能够实现。
    关键是我在验证前有提示语,验证通过后要显示一个绿色的小勾。

  • 回复于 2012-09-25 10:03 举报

    没验证通过显示图片可以通过css来控制,但是验证前显示提示语,验证错误后把提示语用错误代替,然后验证通过后显示绿色小勾这个现在不知怎么处理。

  • 回复于 2012-09-25 10:19 举报

    http://www.yiiframework.com/doc/api/1.1/CActiveForm 文档里是这么说的:

    What makes CActiveForm extremely useful is its support for data validation. CActiveForm supports data validation at three levels:
    
    server-side validation: the validation is performed at server side after the whole page containing the form is submitted. If there is any validation error, CActiveForm will render the error in the page back to user.
    AJAX-based validation: when the user enters data into an input field, an AJAX request is triggered which requires server-side validation. The validation result is sent back in AJAX response and the input field changes its appearance accordingly.
    client-side validation (available since version 1.1.7): when the user enters data into an input field, validation is performed on the client side using JavaScript. No server contact will be made, which reduces the workload on the server.
    
  • 回复于 2012-09-25 10:24 举报

    可以通过Ajax或客户端验证。

    <?
        $form = $this->beginWidget('CActiveForm', array(
        'id'=>$name,
        'enableAjaxValidation'=>true,
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'validateOnChange'=>false,
            'validateOnType'=>false
        ),
        'focus'=>array($model,'subject')
    ));
    ?>
    
  • 回复于 2012-09-25 10:51 举报

    问题一:比如现在验证报错了,会显示一个提示框,这个没有问题。但是我输入正确后,报错那个层就会display:none, 但是我具体的需求是不隐藏,而且显示一个绿色的小勾。
    问题二:我在用户输入任何信息前会有提示语,怎么在报错的时候把这个提示语隐藏呢?因为在用户输入前报错的那个层也是隐藏的,有错误才会显现。所以感觉应该不能用同一个层。

  • 回复于 2012-09-25 16:29 举报
    <div class="row">
        <?php echo $form->labelEx($model,'firstName'); ?>
        <?php echo $form->textField($model,'firstName'); ?>
        <?php echo $form->error($model,'firstName',array(
        'afterValidateAttribute'=>'js:function(form, attribute, data, hasError) { //属性验证后执行
        if (hasError)
            jQuery("#LoginForm_username_em_").val(data[attribute.id]);   //选择器id用FF可以查看
        else 
            jQuery("#LoginForm_username_em_").html("1111111").show();  //这里你就可以实现验证通过显示对勾图片
    }')); ?>
    </div>
    
  • 回复于 2012-09-25 16:53 举报

    这个给力,可以解决问题

  • 回复于 2012-09-25 16:56 举报

    同时附上一篇老外的文章
    http://www.yiiframework.com/forum/index.php/topic/35701-displaying-success-messages-in-cactiveform/page__p__171582__hl__cactiveform#entry171582
    跳过前面所有内容,直接看如下部分

    'clientOptions'=>array('afterValidateAttribute'=>new CJavaScriptExpression("function(form, attribute, data, hasError){
        var id=attribute.id;
        var eid=attribute.errorID;
        
        if(data[id].toString().substring(0,7)=='Success')
        {       $('#'+eid+'.flash-error').css('display','none');
                $('#'+id).parent().children('label.required').css('color','green');
                $('#'+id).css('background','#E6EFC2');
        }
        
        else
        {
                $('#'+eid+'.flash-success').css('display','none');
                $('#'+id).parent().children('label.required').css('color','red');
                $('#'+id).css('background','').addClass('error');
        }
        }")),
                    
    ));
    
  • 回复于 2012-09-25 16:59 举报

    附上文档说明

    afterValidateAttribute: function, the function that will be invoked after performing ajax-based validation triggered by a single attribute input change. The expected function signature should be afterValidateAttribute(form, attribute, data, hasError) {...}, where 'form' is the jquery representation of the form object; 'attribute' refers to the js options for the triggering attribute (see error); 'data' is the JSON response from the server-side validation; 'hasError' is a boolean value indicating whether there is any validation error. 
    
    Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpressoin to prevent it from being encoded as a string. This option has been available since version 1.1.3.
    
您需要登录后才可以回复。登录 | 立即注册