Yii Framework Forum: Checkbox Variable In View - Yii Framework Forum

Jump to content

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

Checkbox Variable In View Rate Topic: -----

#1 User is offline   Rasho 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 05-June 13

Posted 10 June 2013 - 01:43 AM

I have a problem with passing the checkbox variable to view and work with it. So basically what I'm trying to do is to use a checkbox field that is not from database, that means I can not fetch it from model. How to pass it to view? I need a checkbox that would tell me if I need to save the date to database or not.

That is how I call the variable in view, but it looks like I need to create it somewhere, but I'm lost, where.
<?php echo $form->checkBox($model, 'SaveDate');?>

0

#2 User is offline   mirunho 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 19-December 12
  • Location:Gdansk, Poland

Posted 10 June 2013 - 02:07 AM

You have to define virtual attribute in your model. I think You can also check out :
http://www.yiiframew...get-set-methods
0

#3 User is offline   Rasho 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 05-June 13

Posted 10 June 2013 - 03:11 AM

OK, I managed to do that. This is how it looks now:
Controller:
class PosegController extends Controller
{
	public $ShraniDatum;


actionCreate:
	public function actionCreate()
	{
		$model=new Poseg;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Poseg']))
		{
			$model->attributes=$_POST['Poseg'];
			if($model->save())
#				print_r($_POST);
#				exit();
				$ObvNRedni = new DateTime($_POST['NRedni']);
				$ObvNRedni->modify("-1 month");
				if(($_POST['ShraniDatum'])=='1')
				{
					OsnovnoSredstvo::model()->updateByPk($model->Inventarna,array('NRedni'=>$_POST['NRedni']));
					OsnovnoSredstvo::model()->updateByPk($model->Inventarna,array('ObvNRedni'=>$ObvNRedni->format("Y-m-d")));
				}
				$this->redirect(array('view','id'=>$model->idPoseg));
			
		}
		
		$this->render('create',array(
			'model'=>$model,
		));
	}


View:
	<div class="row">
		<?php echo $form->labelEx($model,'ShraniDatum'); ?>
		<?php echo $form->checkBox($model,'ShraniDatum');?>
	</div>


I can fetch ShraniDatum value if posting print_r($_POST); but when I it in the app, I get error: Undefined index: ShraniDatum. What am I missing?
0

#4 User is offline   Rasho 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 05-June 13

Posted 10 June 2013 - 05:31 AM

Found out the sollution:
changed
if(($_POST['ShraniDatum'])=='1')
to
if($model->ShraniDatum==1)

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