[Solved]Masked Text Field from controller (AJAX)


Here’s my problem : in my view i’ve got an ajax button wich add a line in a table. I want Masked Text Field to be in this line but the script do not seem to follow. Anyway, here is my code:

This is in my _form.php, it calls the ajax button on wich the user click to add a line in the table

CHtml::ajaxButton('Add','index.php?r=event/ManageUser',array('type'=>'post','cache'=>true,'update'=>"#eventRight"), array('id'=>'addUser'))

Now, a part of my ajax function called by the button. This function is located in my controller:

echo '<tr>';

echo '<td>';

$this->widget('CMaskedTextField', array('attribute'=>'sartHour','mask'=>'99:99:99','name'=>'evenementUser'));

echo '</td>';

//other stuff wich works

echo '</tr>';

The result of this is a normal text field without the mask. Seems to me the script don’t follow the field.

First of all you should look at your JavaScript console to figure out if there was some errors.

Maybe you find some errors like jQuery is not defined or Object has no method ‘mask’ etc…

In that case be sure that jQuery library is properly included before jquery.maskedinput.js plugin.

One note about your coding style:

Try to move out any presentation logic from controller. Put it in a view file. If you must do that in your controller, don’t echo it right there. Instead generate your content, and supply render() with it. (in case of ajax JSON for example)

You may notice that $this->widget() has third parameter captureOutput which if you set it to true returns generated widget content.


Javascript console doesn’t show any errors, the line is added as expected and I can add several lines. The maskedTextField still appears as a simple textField.

I already have working maskedTextField in the view, but they are create directly within the view. The problem is to call the maskedTextField from an ajax function.

I tried setting the captureOutput to true, but that only allows me to echo the maskedTextField instead of just calling it, it still render a simple textField.

(By the way, I don’t speak 100% English so please don’t be surprise of any English mistake ;))

Ok solution is in fourth parameter processOutput of CController.renderPartial(). Set it to true and you’ll get jquery.maskedinput plugin on your ajax requests. If you have installed Firebug or you’re using Chrome (Developer Tools) or whatever you’re using, look at network panel… you’ll see in response three files: action you have called, jQuery and jquery.maskedinput.

Btw here is a code example to demonstrate what I have meant:

// your action

// ...

	if (Yii::app()->request->isAjaxRequest)


            $content = $this->renderPartial('content', null, true, true);

            echo $content;



// ...

// view file content.php


    <td><?php $this->widget('CMaskedTextField', array('mask'=>'99:99:99','name'=>'someName')); ?></td>


If you have some more complex request rather than widget, you first prepare it(for example in model), get it in controller action, and supply it to renderPartial to actually render the result.

I hope you’ll make it!

Problem solved thanks to you!