GenericForm

Yii required a form model to startup with. This could be an overhead if you just wanto ask for 2 input for example. GenericForm is here for this purpose.

Its structure is basic and straightforward, support 5 variables, 1 file upload and 1 image upload. No rules and validation included, you will need to do that manually in your code.

<?php

class GenericForm extends CFormModel
{
	public $uploadPath;
	public $uploadFile_value;
	public $imageFile_value;
	public $var1;
	public $var2;
	public $var3;
	public $var4;
	public $var5;

	public function init()
	{
		$this->uploadPath = Yii::getPathOfAlias('uploads') . DIRECTORY_SEPARATOR . 'generic';
	}

	public function attributeLabels()
	{
		return array(
			'uploadFile_value' => Yii::t('core', 'Upload File'),
			'imageFile_value' => Yii::t('core', 'Image'),
			'var1' => Yii::t('core', 'Var 1'),
			'var2' => Yii::t('core', 'Var 2'),
			'var3' => Yii::t('core', 'Var 3'),
			'var4' => Yii::t('core', 'Var 4'),
			'var5' => Yii::t('core', 'Var 5'),
		);
	}
}

You can extend the form to set labels. This piece of code can either in a new model or simply at the bottom of your Controller where it is use. e.g.:

Your controller would be something like:

In view:

Another use case, on receiving file upload in Controller:

Last updated

Was this helpful?