Yii Framework Forum: Whats the best boolean field validator? - Yii Framework Forum

Jump to content

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

Whats the best boolean field validator? Rate Topic: -----

#1 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 09 June 2009 - 10:57 AM

It's very simple.

The field in the table is a tinyint(1). It stores 0 or 1 (not null in my case).

Looking here: http://www.yiiframew.../api/CValidator

I would like to know which approach is better?

I thought using the match: CRegularExp​ressionValidator with the regular exp​ression: '/^(0|1)$/'

I'd like some opinion.
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,889
  • Joined: 04-October 08
  • Location:DC, USA

Posted 09 June 2009 - 10:14 PM

You can use: array('attribute', 'in', 'range'=>array(0,1))
0

#3 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 10 June 2009 - 08:10 AM

Well point.

It's easer than "match" method.

Thanks.
0

#4 User is offline   jimmy@007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-December 12

Posted 30 July 2013 - 09:08 AM

My problem is a little bit different :

I have a boolean that must be set, so using boolean validator is fine, but using required validator return error even if I do $obj->attribute=false; (the default value for this attribute is null)

CRequiredValidator doesn't consider false a value because in isEmpty() that is inherited from CValidator, the following expression is_scalar($value) && trim($value)==='' is true when $value=false

So the question still remain, how to choose validators for a required boolean ?

Thanks !
0

#5 User is offline   Coksnuss 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-May 09

Posted 31 July 2013 - 06:29 AM

View Postjimmy@007, on 30 July 2013 - 09:08 AM, said:

My problem is a little bit different :

I have a boolean that must be set, so using boolean validator is fine, but using required validator return error even if I do $obj->attribute=false; (the default value for this attribute is null)

CRequiredValidator doesn't consider false a value because in isEmpty() that is inherited from CValidator, the following expression is_scalar($value) && trim($value)==='' is true when $value=false

So the question still remain, how to choose validators for a required boolean ?

Thanks !


Hi jimmy,

please open a new topic for new requests. This topic is 4 years old.
The validators are supposed to validate user form input values. There is no way that the user can submit the native boolean values "true" or "false" to the server. A boolean field takes either the string value "0" (false) or "1" (true). They do also pass the required validator.

Therefore: Instead of $model->attribute = false use $model->attribute = 0 (or $model->attribute = '0')
0

#6 User is offline   jimmy@007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-December 12

Posted 03 August 2013 - 01:13 PM

View PostCoksnuss, on 31 July 2013 - 06:29 AM, said:

Hi jimmy,

please open a new topic for new requests. This topic is 4 years old.
The validators are supposed to validate user form input values. There is no way that the user can submit the native boolean values "true" or "false" to the server. A boolean field takes either the string value "0" (false) or "1" (true). They do also pass the required validator.

Therefore: Instead of $model->attribute = false use $model->attribute = 0 (or $model->attribute = '0')


Thanks for the answer. I will open a new topic next time :rolleyes:
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