Yii Framework Forum: Checkbox default value is not being sent to the server - Yii Framework Forum

Jump to content

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

Checkbox default value is not being sent to the server

#1 User is offline   Commun 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 185
  • Joined: 17-September 15

Posted 02 November 2017 - 06:43 PM

Here is my code :

     $model->free=true;
                echo $form->field($model, 'free',['inputOptions'=>['class'=>'form-control input-lg ']])->checkbox();

What I want is if the user doesn't uncheck, the default value(1 or true) must be sent to the server but to my suprise it is sending $model->free is remaining null weither the user uncheck or leave it as it is; to send the checked value the user has to uncheck and check which stress.

How can I acheive what I want?
0

#2 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 145
  • Joined: 10-October 11
  • Location:Russia

Posted 03 November 2017 - 12:44 AM

Show us your validator for 'free' field, please.
I think if you create your validation like
['free', 'boolean']

it should work correctly
0

#3 User is offline   jimgwhit2 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 27-September 17
  • Location:U.S.A.

Posted 03 November 2017 - 03:39 PM

Something like:
$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');

I use tinyint and store a 0 or 1.
That way in the create array:
$postdata = array(
                    'catpic' => $catpic,
                    'catname' => $catname,
                    'sex' => $sex,
                    'comments' => $comments,
                    'adopted' => $adopted,
                    'lastedit' => $lastedit
                );

Just change $_POST to a yii2 request.
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