Yii: how to enable ajax if the client inserts space into the text box

in yii i find validateOnChange and validateOnType in clientoptions in yii 1.1.X

but I need ajax if the client enters a space in the textField. Like a tag in stackoverflow

this is my _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 -->
+4
source share
2 answers

Does this jQuery code help you ?:

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

you can do something like this

Suppose you want to enable ajax checking if the theme contains a space. For this you can do something like this

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

Source: https://habr.com/ru/post/1543853/


All Articles