yii:如果客户端在textfield中插入空格,如何启用ajax

时间:2014-06-09 11:05:20

标签: php ajax yii

在yii中我在yii 1.1.X中的clientoptions中找到validateOnChange和validateOnType

但是如果客户端在textField中输入空格,我需要启用ajax,这与stackoverflow中的标记类似

这是我的_form.php

    <?php
/* @var $this TextController */
/* @var $model Text */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'text-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>TRUE,
     'focus'=>array($model,'subject'),
     'clientOptions'=>array(
                'validateOnChange'=>true,  // the default. validate when input changes
                'validateOnType'=>'true',    // validate with EVERY keystroke, hooray!
                'validationDelay'=>10,     // not related to this post--but cool!
                                      // default delay is 200 ms
        ),
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'subject'); ?>
        <?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>80)); ?>
        <?php echo $form->error($model,'subject'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'matn'); ?>
        <?php echo $form->textField($model,'matn',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'matn'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'group'); ?>
        <?php echo $form->textField($model,'group',array('size'=>20,'maxlength'=>20)); ?>
        <?php echo $form->error($model,'group'); ?>
    </div>
<?php $userX=yii::app()->session['idX']; ?>

    <div class="row">
        <?php // echo $form->labelEx($model,'user_id'); ?>
        <?php echo $form->textField($model,'user_id',array ('value'=>$userX,'type'=>"hidden")); ?>
        <?php // echo $form->error($model,'user_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'Privacy'); ?>
        <?php echo $form->dropDownList($model,'Privacy',  array('1'=>'Public','2'=>'Only me')); ?>
        <?php echo $form->error($model,'Privacy'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

2 个答案:

答案 0 :(得分:0)

这个JQuery代码可以帮助你吗?:

$("input[type='text']").keyup(function(e){ 
    if(e.keyCode == 32)
    {
        //AJAX action(s)
    }
})

答案 1 :(得分:0)

你可以做这样的事情

如果主题包含空格,则假设您要启用ajax验证。为此你可以做这样的事情

    <?php echo $form->error($model,'subject',array(
                    'beforeValidateAttribute'=> new CJavaScriptExpression("function(form,attribute){
                            var value = attribute.value;
                           return value.indexOf(' ') >= 0;
                        }")
                )); ?>