How to add a javascript function to mask input in Yii

Hi again everyone… :rolleyes:

Being new to Yii, I still haven’t tried

adding a javascript through clientChange methods

with jquery

to add a mask input to my textfields…

please…any example will be appreciated very much…

thanks… 8)

Use directly CMaskedTextField widget

Example

At view:




<?php

$this->widget('CMaskedTextField',array(

    'model'=>$model,

    'attribute'=>'my_date_field',

    'mask'=>'99/99/9999',    

));

?>

Hi there…thnx for the suggestion…i’ll try it when i get home. :)

How can I remove the masking placeholders before saving form?

For example I enter the date in the text fields as "26-10-2010" using jQuery masking plugin, but I want to save it to the database as "26102010".

Let’s say my database is already designed to store simple strings without any placeholders for dates, time, etc… and I don’t want to change the columns nor (resize them) neither to edit model.php and change the validation rules etc…

Is that achievable?

Thanks

Tom

Convert it at ‘beforeSave’ function (try just replace ‘-’ to blanks with str_replace function)

Then, undo conversion at ‘afterSave’ function (to keep your widget working)

Hi ricardograna

In my model I have function:




public function beforeSave()

{

	$this->address_code=substr($this->address_code,0,2).substr($this->address_code,2,3);

	return parent::beforeSave();

}



In this function I strip address_code from 12-331 to 12331. And when I try to save form it logs errors:

[size="6"]

beforeSave()[/size]

But when I use berofeValidate() function in model:




public function beforeValidate()

{

	$this->address_code=substr($this->address_code,0,2).substr($this->address_code,2,3);

	return parent::beforeValidate();

}



Now the address_code 12-331 is stripped to 12331 and I see no error, but the value form text input gets cleared:

[size="6"]beforeValidate()[/size]

Could You help me more with this?

Thanks in advance

Tom

@pc133

Please do not ask the same question on two different threads… you already opened a topic for this problem - http://www.yiiframework.com/forum/index.php?/topic/12658-saving-cmaskedtextfield-to-model-without-just-pure-strings-placeholders-i-think/page__p__61860__fromsearch__1#entry61860