CForm model

I need to build forms for my website. They are built dynamically so I can’t have CFormModel subclasses (I don’t know what will be the fields).

I could write the form in pure HTML but I’d like to use the CForm class, so I could have validation operations…

It seems there’s a problem with CForm (or maybe I didn’t find the right way to use it…): the constructor expects a “model” parameter and if it is null the class will use the model of a “parent” parameter. This parent parameter may be a CForm object or a CBaseController object.

I tried with a CBaseController object (because my form is a top-level form), but it doesn’t have a model property… How am I supposed to do ?

Please, what kind of model am I supposed to use ?

I’d like to use it with a null model and a CController subclass parent but I can’t get it work, I get this error message:

Is it a mistake from myself or a bug ?

Ok, maybe my request wasn’t clear, so here is a minimal test case.

File test/protected/controllers/FormFailController.php :


<?php

class FormFailController extends CController

{

	public function actionFail() {

		$this->render('fail', array('form' => $this->generateForm()));

	}


	private function generateForm() {

		$elements = array(	'element1' => array('type' => 'text', 'value' => 'Element 1'),

					'element2' => array('type' => 'text', 'value' => 'Element 2'));

		$buttons = array('valid' => array('type' => 'submit', 'label' => 'Valid'));

		$config = array('elements' => $elements, 'buttons' => $buttons);

		return new CForm($config, null, $this);

	}

}

?>

File test/protected/views/formFail/fail.php :


<?php

	echo $form->render();

?>

URL : http://localhost/~julien/test/index.php?r=formFail/fail

Result :

However I followed the Yii documentation for the CForm class : I don’t have a CModel associated with this form (how could I have one ?), and I give a CBaseController object as its parent.

This post is more appropriate to be put in general discussion rather than bug report.

CForm is designed to work with a model object. Even if your form is dynamic (with dynamic fields and rules), you should still provide a model object. Otherwise, the whole thing simply won’t work.

Because CFormModel::rules() is a function, you should be able to return a set of dynamic rules with some code (instead of just returning a static array). Similarly, you should design your own way to define dynamic fields.

Ok, so I was wrong.

I don’t know how to “build” a dymamic CModel object with appropriate fields in PHP. Could you give me an example ?

BTW why is it necessary to provide a CModel object to the CForm object ? The description of the elements in the configuration ($config param of the constructor) is not enough ?

The model parameter can be null only when the form is a sub-form of another. Internally, the active methods of CHtml will be used to render those input fields, which requires a model object.

You should know better what a dynamic model is. ;) My guess is that you may want users to somehow specify which fields the form has. You should have some strategy to store these information. Based on these information, you should be able to return rules().

Thanks for the reply, I see how to build the attributes of my CFormModel object dynamically, but I don’t see how to add a rules function (with PHP 5.1) :


<?php


class FormFailController extends CController

{

	public function actionFail() {

		$this->render('fail', array('form' => $this->generateForm()));

	}


	private function generateForm() {

		$elements = array(	'element1' => array('type' => 'text', 'value' => 'Element 1'),

					'element2' => array('type' => 'text', 'value' => 'Element 2'));

		$buttons = array('valid' => array('type' => 'submit', 'label' => 'Valid'));

		$config = array('elements' => $elements, 'buttons' => $buttons);

		$model = new CFormModel();

		$model->attributes = array('element1' => 'Hello', 'element2' => 'Are you doing ?');

		return new CForm($config, $model, $this);

	}

}

?>

Currently the form is displayed only if I call setVisible(true) on each of its elements.

Hi,

I need your help please. I am using Cform for to integrate wizagrd(Multisteps form) form functionality. I had done everything and working perfectly. Now i want to change the design of these form. I am getting issue in it and design part is not working for me. please anybody help me with step wise to resolve this issue.

Waiting for your quick response.

Thanks & Regards,

Jarnail