Validate Chtml::textfield Without Model

Hi all, i want to validate form text field that is generated by CHtml::textfield that does not have any model.

<?php echo CHtml::textField(‘page_size’, ‘25’, array(‘size’=>3,‘maxlength’=>3,‘value’=>25)); ?>

It should accept only integer fields.How can i do this?

Hi

please see it…

http://help.discretelogix.com/php/yii/how-to-create-custom-validation-rule.htm

use this


array('page_size', 'numerical', 'integerOnly'=>true),

Hi Ankit,

i m not having any model,so how can i use custom validation rule?

Hi

can u post a full input element like

<input type=’’ value=’’/>

<div id="page_size">

	&lt;?php echo CHtml::label('Page Size' ,''); ?&gt;


	&lt;?php echo CHtml::textField('page_size', '25',array('size'=&gt;3,'maxlength'=&gt;3,'value'=&gt;25,)); ?&gt;


&lt;/div&gt;

Either you can use jquery validation plugin.

use client side validators

  1. If you do need to validate the user input, for example before you store the value to database, then using a model is the right choice. It will make things quite easy for you.

Remember the client side validation is not enough. We have to validate it in the server side.

Consider using CFormModel if ‘page_size’ is not an attribute of some CActiveRecord.

  1. If you are working with the page size of a CGridView or CListView, then you can simplify the validation by using intval(), without using a model.



$pageSize = isset($_GET['page_size']) ? intval($_GET['page_size']) : $defaultPageSize;

if ($pageSize <= 0) {

    $pageSize = $defaultPageSize;

}



By forcing the conversion to an integer, you’ll be free from the danger of a malicious input.