Yii Framework Forum: Boolean Validator And True Value - Yii Framework Forum

Jump to content

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

Boolean Validator And True Value Rate Topic: -----

#1 User is offline   jira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-May 13

Posted 23 July 2013 - 02:28 PM

Hello,

how can I make the boolean validator set the attribute to true only when the value equals the trueValue?
It sets it to true even when it is empty.

Thank you
0

#2 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,380
  • Joined: 21-February 11
  • Location:Greece

Posted 23 July 2013 - 03:44 PM

Hi,

try this
array('yourattribute','boolean','allowEmpty'=>false),

or
array('yourattribute','compare','compareValue'=>true),

Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   jira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-May 13

Posted 23 July 2013 - 04:11 PM

Hi

View PostKonApaz, on 23 July 2013 - 03:44 PM, said:

try this
array('yourattribute','boolean','allowEmpty'=>false),


This doesn't work. I get a error when the posted value is empty.

Quote

array('yourattribute','compare','compareValue'=>true),


This doesn't work either. It always sets the attribute to true.

Is there really no way to use a simple checkbox with Yii?

Regards,
Jiri
0

#4 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,380
  • Joined: 21-February 11
  • Location:Greece

Posted 23 July 2013 - 04:41 PM

View Postjira, on 23 July 2013 - 04:11 PM, said:

Hi

This doesn't work. I get a error when the posted value is empty.

This doesn't work either. It always sets the attribute to true.

Is there really no way to use a simple checkbox with Yii?

Regards,
Jiri



I tested both of two validators and work fine!
Are you sure threr is no any other suspected code? post your model/action/view code...
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   jira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-May 13

Posted 23 July 2013 - 04:55 PM

View PostKonApaz, on 23 July 2013 - 04:41 PM, said:

I tested both of two validators and work fine!
Are you sure threr is no any other suspected code? post your model/action/view code...


View is a simple form with a standard checkbox. It is hand written no Yii involved.
<input type="checkbox" name="NewItemForm[riapproved"] value="on"/>


Model and controller are attached.

Thank you,
Jiri

Attached File(s)


0

#6 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,380
  • Joined: 21-February 11
  • Location:Greece

Posted 23 July 2013 - 05:21 PM

View Postjira, on 23 July 2013 - 04:55 PM, said:

View is a simple form with a standard checkbox. It is hand written no Yii involved.
<input type="checkbox" name="NewItemForm[riapproved"] value="on"/>


Model and controller are attached.

Thank you,
Jiri


array('rememberMe','boolean', 'trueValue' => 'on','allowEmpty' => false), //false not true!

Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#7 User is offline   jira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-May 13

Posted 24 July 2013 - 04:11 AM

View PostKonApaz, on 23 July 2013 - 05:21 PM, said:

array('rememberMe','boolean', 'trueValue' => 'on','allowEmpty' => false), //false not true!



Hi,

yes I tried that, but then I get a validation error that the value must be either 'on' or 'off'.
I actually tried nearly ever possible permutation of validators and configuration options. None has worked.

So now I ended up doing manual validation in controller. Ugly but works.

Jiri
0

#8 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,380
  • Joined: 21-February 11
  • Location:Greece

Posted 24 July 2013 - 07:28 AM

View Postjira, on 24 July 2013 - 04:11 AM, said:

Hi,

yes I tried that, but then I get a validation error that the value must be either 'on' or 'off'.
I actually tried nearly ever possible permutation of validators and configuration options. None has worked.

So now I ended up doing manual validation in controller. Ugly but works.

Jiri


Did you modify your code? I can't find the 'off' in your code?

Also check the
<input type="checkbox" name="NewItemForm[riapproved"] value="on"/>

must be
<input type="checkbox" name="NewItemForm[riapproved]" value="on"/>


Finaly, how assign the inputs of form? RequestItem not same of NewItemForm
give us the controller/action code
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#9 User is offline   Tsunami 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 16-February 12

Posted 24 July 2013 - 07:59 AM

Browsers don't send any value when the checkbox is unchecked, causing your validator to error. CHtml::checkBox() has the "uncheckValue" option, which creates a hidden input that's sent when the checkbox is unchecked.
1

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