[SOLVED] How do I include a partial from another folder, with no modules?

How do I include a partial from another folder, with no modules?

I had the default Yii setup with the default folders, and then I…

[list=1]

[*]Created a Users model for the users database

[*]inside protected/views/users there is a form called _form.php

[*]I have made a new page inside protected/views/site/pages called apply.php

[*]I want to include the _form.php from users, inside another folder (views/site/pages).

[/list]

How do I do it?


<?php echo $this->renderPartial('SiteController/_form'); ?>

does not work

Here’s how you can do it:

$this->renderPartial(’//users/_form’); //In case _form.php is inside views/users/

Using the // double slashes you can access the root of the views

1 Like

Thanks! It works!

Now I’ve got a new problem.

When I include the form, I get an error that says

The code causing this is protected/views/site/pages/apply.php


<h1>Users</h1>


<?php $this->widget('zii.widgets.CListView', array(

    'dataProvider'=>$dataProvider,

    'itemView'=>'_view',

)); ?>

However, the dataProvider variable is defined in SiteController, (I think). So why isn’t it recognised?

protected/controllers/SiteController.php




	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Users');

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}

As it says $dataProvider is not defined because you have it defined in the actionIndex and you are rendering ‘index’ instead of apply.

This should fix it: (I am not sure if you are rendering it partially or what but this is how it should be done)




	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Users');

		$this->render('pages/apply',array(

			'dataProvider'=>$dataProvider,

		));

	}

That would work if I was rendering the page directly, but I’m not.

Using the default Yii project, I’m actually using the site/pages views, where I can make new pages by making .php files in that folder, and not have to make a new action for them.

The code for the Controller to view the site/page?view=apply page, goes like this.


<?php


class SiteController extends Controller

{

	/**

	 * Declares class-based actions.

	 */

	public function actions()

	{

		return array(

			// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

			// page action renders "static" pages stored under 'protected/views/site/pages'

			// They can be accessed via: index.php?r=site/page&view=FileName

			'page'=>array(

				'class'=>'CViewAction',

			),

		);

	}


	/**

	 * This is the default 'index' action that is invoked

	 * when an action is not explicitly requested by users.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Users');

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}



So where am I meant to place the $dataProvider variable? In the CViewAction class inside the actual Yii folder? Inside the actions() function (which doesn’t work)? I’m stuck.

I think this feature was really only intended for the occasional static page.

Just create a new action and things will start to make sense.




public function actionApply()

{

// capture $_GET parameters (or $_POST if using a form)


 // define your model

 

// display a view etc


}




doodle

This would work, but when I use renderPartial() to pass the $dataProvider variable to the partial that is being included, I get a fatal error that says that the object cannot be converted to a string.


<?php echo $this->renderPartial('//users/index', "$dataProvider"); ?>

If you are rendering a partial view then you have to pass the object to the partial view, just like you did with render.


<?php echo $this->renderPartial('SiteController/_form',array('var1'=>$var1,'var2'=>$var2)); ?>

I use this often when formatting links etc.


foreach ($categories  as $category) {

    $this->renderPartial('_requirementItem',array(

        'category'=>$category,

        'services'=>$services,

        'woodlot'=>$model

    ));

}

Then in the partial view


<?php echo $category->name ?>

Note also that the partial view can also render partial views, so you can pass a collection to a partial view and that view can loop through the collection but it has to pass the data to each view if that view is referencing it.

I have also adopted the naming standard where partial views start with an underscore, it helps me to keep organized.

does that help?

doodle

Yes it does help.

But now when I submit the form, I receive this error.




	public function actionCreate()

	{

		$model=new Users;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Users']))

		{

			$model->attributes=$_POST['Users'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

I have no idea why this error is happening, as the model is correctly defined, and I copied the code from the Users controller, so it should work.

What does your model look like?

Does it extend CActiveRecord?


class CatalogItem extends CActiveRecord

doodle

I’ve managed to fix that, so that the form can POST correctly.

But it says that I keep leaving a field blank, even when it isn’t blank.

I can’t see anywhere in my code that would cause the dateofbirth form field not to be recognised.

PS. dateofbirth is a VARCHAR field in mysql, but it is a date input in the form. I don’t think that should be a problem.


	<div class="row">

		<?php echo $form->labelEx($model,'dateofbirth'); ?>

		<?php

		$this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'name'=>'dateofbirth',

    // additional javascript options for the date picker plugin

    'options'=>array(

        'showAnim'=>'fold',

    ),

    'htmlOptions'=>array(

        'style'=>'height:20px;'

    ),

    ));

    ?>




  public function actionApply()

  {

		$model=new Users;

		

		// $this->performAjaxValidation($model);

		if(isset($_POST['Users']))

		{

			$model->attributes=$_POST['Users'];

			if($model->save())

				$this->redirect('site/apply');

		}


    $dataProvider=new CActiveDataProvider('Users');


    $this->render('apply',array(

			'dataProvider'=>$dataProvider,

			'model'=>$model,

		));

  }


<?php


/**

 * This is the model class for table "users".

 *

 * The followings are the available columns in table 'users':

 * @property integer $id

 * @property string $username

 * @property string $password

 * @property string $email

 * @property integer $level

 * @property string $firstname

 * @property string $lastname

 * @property string $firstline

 * @property string $secondline

 * @property string $city

 * @property string $postcode

 * @property string $phonenumber

 * @property string $dateofbirth

 * @property integer $upline

 */

class Users extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Users the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'users';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('username, password, email, firstname, lastname, firstline, secondline, city, postcode, phonenumber, dateofbirth', 'required'),

			array('level, upline', 'numerical', 'integerOnly'=>true),

			array('username, password, email, firstname, lastname, firstline, secondline, city, postcode, phonenumber, dateofbirth', 'length', 'max'=>255),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, username, password, email, level, firstname, lastname, firstline, secondline, city, postcode, phonenumber, dateofbirth, upline', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'username' => 'Username',

			'password' => 'Password',

			'email' => 'Email',

			'level' => 'Level',

			'firstname' => 'First Name',

			'lastname' => 'Last Name',

			'firstline' => 'Road name and number',

			'secondline' => '2nd line of address',

			'city' => 'City',

			'postcode' => 'Postcode',

			'phonenumber' => 'Phone number',

			'dateofbirth' => 'Date of birth',

			'upline' => 'Upline',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('username',$this->username,true);

		$criteria->compare('password',$this->password,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('level',$this->level);

		$criteria->compare('firstname',$this->firstname,true);

		$criteria->compare('lastname',$this->lastname,true);

		$criteria->compare('firstline',$this->firstline,true);

		$criteria->compare('secondline',$this->secondline,true);

		$criteria->compare('city',$this->city,true);

		$criteria->compare('postcode',$this->postcode,true);

		$criteria->compare('phonenumber',$this->phonenumber,true);

		$criteria->compare('dateofbirth',$this->dateofbirth,true);

		$criteria->compare('upline',$this->upline);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}

Have a look at your post array


if(isset($_POST['Users']))

                {

                    print_r($_POST);

                    die;

                        $model->attributes=$_POST['Users'];

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }



That might give you a clue, also you could do away with the datePicker widget and just use a text field until you are sure that everything else works.

So much for debugging strategies, if you want to shoot straight for the answer I think it is because you don’t have a model defined in your widget.

Look here.

doodle

Weirdly the thing is posted. So why isn’t it recognised?

Nowhere on the CWidget API reference page, does it say how to do that, or that it can be done.

http://www.yiiframework.com/doc/api/1.1/CWidget

I’m confused. :blink:

Actual working code


<?php 

 $this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'model'=>$model,

     'attribute'=>'EventStartdate',

     // additional javascript options for the date picker plugin

     'options'=>array(

         'showAnim'=>'fold',

         'dateFormat'=>'yy-mm-dd',

         'defaultDate'

     ),

     'htmlOptions'=>array(

         'style'=>'height:20px;'

     ),

 ));

?>

This is defined as a date field


`EventStartdate` date NOT NULL DEFAULT '0000-00-00',

It’s quite possible you are hung up on the slashes in the data in the post.

Did you try just a text field to eliminate problems with the widget?

doodle

I’ve done what you said with the widget, datetime and DATE mysql field, and it still doesn’t work.

Forget the widget, make sure that you can make it work with just a text field first.

Your example array ($_POST)


Array ( 

[Users] => Array ( 

  [username] => 42refno 

  [password] => mercury 

  [email] => example@example.com 

  [firstname] => Sheridon 

  [lastname] => Thompson 

  [firstline] => 42 Cheque Street 

  [secondline] => Hockley 

  [city] => Birmingham 	

  [postcode] => B54 5Dh 	

  [phonenumber] => 0759491554 

  ) 	

  [dateofbirth] => 02/15/2012 

  [yt0] => Create )

Notice that is not part of the [users] array.

That could be your clue.

You have $_POST[‘Users’] which is an array

You have $_POST[‘dateofbirth’] which has the value ‘02/15/2012’

And $_POST[‘yt0’] which is the Yii generated id for the submit button.

doodle

[size="5"]This topic is solved![/size]

The problem was with the datepicker not being recognised despite using a correct name field, and for the controller function being bad.

I made a new Yii project from scratch, and then got it to work really quick.

Thanks a lot for your help doodle! :lol:

I will make new topics for other issues.