Yii Framework Forum: Validate Chtml::textfield Without Model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Validate Chtml::textfield Without Model Rate Topic: -----

#1 User is offline   Y!! 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 09-February 13

Posted 29 June 2013 - 02:05 AM

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?
0

#2 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 840
  • Joined: 19-February 13

Posted 29 June 2013 - 02:14 AM

Hi

please see it..

http://help.discrete...dation-rule.htm

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

Thanks,
Ankit Modi
0

#3 User is offline   Y!! 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 09-February 13

Posted 29 June 2013 - 02:25 AM

View PostAnkit Modi, on 29 June 2013 - 02:14 AM, said:

Hi

please see it..

http://help.discrete...dation-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?
0

#4 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 840
  • Joined: 19-February 13

Posted 29 June 2013 - 02:39 AM

Hi

can u post a full input element like

<input type='' value=''/>
Thanks,
Ankit Modi
0

#5 User is offline   Y!! 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 09-February 13

Posted 29 June 2013 - 03:45 AM

View PostAnkit Modi, on 29 June 2013 - 02:39 AM, said:

Hi

can u post a full input element like

<input type='' value=''/>



<div id="page_size">
<?php echo CHtml::label('Page Size' ,''); ?>
<?php echo CHtml::textField('page_size', '25',array('size'=>3,'maxlength'=>3,'value'=>25,)); ?>
</div>
0

#6 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 840
  • Joined: 19-February 13

Posted 29 June 2013 - 04:14 AM

Either you can use jquery validation plugin.
Thanks,
Ankit Modi
0

#7 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 831
  • Joined: 20-April 11
  • Location:India

Posted 29 June 2013 - 04:40 AM

use client side validators
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 29 June 2013 - 05:38 AM

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.

2) 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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users