Please Help: Yii Textfield Default Text And Value

I have a textField like this in view _form:




	<div class="row">

		<?php echo $form->labelEx($model,'capacity'); ?>

		<?php echo $form->textField($model,'capacity'); ?>

		<?php echo $form->error($model,'capacity'); ?>

	</div>



In database, the default value for this column is -1 which means unlimited capacity. Hence, whenever a model is created, the default text in that textfield is -1 which is not meaningful to end-user. The billion dollars question is how can I have the default text for this field says "unlimited" and -1 is the value. Of course, if user type in any other number then that number should be stored in database. Thank you!


<div class="row">

                <?php echo $form->labelEx($model,'capacity'); ?>

                <?php echo $form->textField($model,'capacity',array('value'=>'unlimited')); ?>

                <?php echo $form->error($model,'capacity'); ?>

        </div>

Sorry, I don’t think you can do that. By doing so, what would be saved in the column capacity in the database would be “unlimited” and it will fail both requirements and Ajax validation since it must be an integer. Thank you anyway!

Then you declare a var and give the value for that.

Thanks Nawin, I adopted your idea and combine it with the model event like this:




        protected function beforeValidate() {

            if(!is_numeric($this->capacity)) { 

                

                $this->capacity=-1;

            }

            return parent::beforeValidate();

        }