Separation Of Concerns: New Class For Generating Html-Forms

I notice there is no class in Yii2 for creating html-forms. Forms are generated by all that exists in Yii. They are HTML-class (see the getInputId() method for example), CForm in Yii1 and may be so on. So there is no only class in yii which is intended for generating html-forms. CModel-class in Yii can’t be the necessary class because of it is for validating data only. New independent class for form generating needed. Models, CForms, helpers and other clases can be deligated in this class. Developers could extend this class for their own needs. All logic for form generating must be inside this class.


$FormManager=new FormManager('formName');

$FormManager->fillFormByModel($model); //Model which validates data is used to get needed data to generate form

if (!$FormManager->isError())

{

    $FormManager->MainAction();

}

else

{

    $this->render('myview', array('FormManager'=>$FormManager));

}




//myview.php

echo $FormManager->beginForm();

echo $FormManager->errorSummary();

echo $FormManager->generateFullForm();

echo $FormManager->endForm();



or




//myview.php

echo $FormManager->beginForm();

echo $FormManager->errorSummary();




echo $FormManager->activeLabel('username');

echo $FormManager->activeTextField('username');


echo '<br />';


echo $FormManager->activeLabel('password');

echo $FormManager->activePasswordField('password');


echo '<br />';


echo $FormManager->submitButton('Enter');

echo $FormManager->endForm();



What do you think about this? Is it possible to create new class and give it the independent functionality?

My rough example for BaseFormManager:




class BaseFormManager

{

	private $errors=aaray();

	private $data=arrays();

	private $formName;


	public function __construct($formName)

	{

		$this->formName=$formName;

	}


	public function fillForm($data=array())

	{

		//$data - Any data from $_POST or models or anything else. Data is object containing names and values


		if ($data->getErrors())

		{

			$this->errors=$data->getErrors();

		}

		$this->data=$data;

	}


	public function fillFormByModel($model)

	{

		//do something with model and get $data

		$this->fillForm($data); 

	}


	public function isFilled()

	{

		return !empty($this->data);

	}


	public function isError()

	{

		return !empty($this->errors);

	}


	public function generateFullForm()

	{

		/* Any scheme to generate html-form and fields via BaseFormManager::generateField()  */

	}


/**

 * @param string $type Type of field: text-input, password-input, texarea, checkbox and so on

 * @param string $name Name of the field

 * @param Object $value Data to generate field

 * @params array $params Additional params

 */

	public function generateField($type, $name, $value, $params)

	{


	}


/* This method is main action for form. Data can be saved in database or data can be used to login the user. it is better to define in derived classes */

	public function MainAction()

	{

		if (!$this->isErrors())

		{

			//Do something

		}

	}

}