I need to generate a form (witch gathering from some views and models). User fill in this form and data save to DB. I have a few forms in my module, they have some equal fields and some unique. Some data puts once, they a fixed, for example user name, email, phone. And some data fill in dynamically, for example, goods. User can add as many goods, as he wish.
At first time creating form, to make filing easier, some fields fill with default data. For example user name, phone, email, also some dynamically fields, etc.
So, I can’t determine where define default data for forms? Where fill in this data to my object which I pass to form view? It’s would really helpful for me, a detailed explanation of implement. Thanks in advance.
class DocController {
public function actionCreate( $type ){
$factory = new DocFactory();
$doc = $factory->createDoc( $type );
if ( $doc === null ) {
throw new CHttpException(404,'Page doesn\'t exists');
}
if ( isset( $_POST['somedata'] ) ) {
// save form data
} else {
// something else, for example fill in object with defaut data
}
$this->render( $doc->getView('create'), array(
// form values takes from $doc
'doc' => $doc
));
}
}
class DocFactory {
protected $contract_profitable;
public function createDoc( $type ){
switch( $type ){
case 'contract_profitable':
return $this->getContractProfitable();
break;
}
return null;
}
public function getContractProfitable(){
if ( $this->contract_profitable === null ) {
$this->contract_profitable = new DocContractProfitable();
}
return $this->contract_profitable;
}
}
interface IDoc {
public function getView( $view );
}
class DocContractProfitable implements IDoc {
public function getView( $view ){
return $view;
}
}