2008年11月27日星期四

[fw-mvc] Zend_Form_SubForm and belongsTo ?? How to ?

Hello

I'm making an installer wizard.

I've build a SubForm with atabase information (host,username, ...)

I would like that all fields except "host" appear in a nested
"params" array. The main idea is to reproduce "Zend_Config" structure
and Zend_Config_Writer

Currently I obtain this structure :

array(3) {
["step_1"] => array(6) {
["adapter"] => string(9) "PDO_MYSQL"
["host"] => string(9) "127.0.0.1"
["dbname"] => string(2) "ZF"
["username"] => string(2) "ZF"
["password"] => string(4) "ZEND"
["dbprefix"] => string(3) "ZF_"
}
...
}

And I would like to obtain

array(3) {
["step_1"] => array(2) {
["adapter"] => string(9) "PDO_MYSQL"
["params"] => array(5) {
["host"] => string(9) "127.0.0.1"
["dbname"] => string(2) "ZF"
["username"] => string(2) "ZF"
["password"] => string(4) "ZEND"
["dbprefix"] => string(3) "ZF_"
}
}
...
}

I initialize my SubForm as this :

$step1 = new Zend_Form_SubForm();

$array = array(
'PDO_MYSQL' => 'Mysql',
'PDO_PGSQL' => 'PgSql',
'PDO_SQLITE' => 'Sqlite',
'PDO_ORACLE' => 'Oracle'
);

$field2 = new Zend_Form_Element_Select('adapter');
$field2->setRequired(false)
->setLabel('adapter')
->addMultiOptions( $array )
->addValidator('InArray', false, array(
array_keys($array) ));

$field3 = new Zend_Form_Element_Text('host');
$field3->setRequired(true)
->setBelongsTo('params')
->setLabel('host');

$field4 = new Zend_Form_Element_Text('dbname');
$field4->setRequired(true)
->setBelongsTo('params')
->setLabel('dbname');

$field5 = new Zend_Form_Element_Text('username');
$field5->setRequired(true)
->setBelongsTo('params')
->setLabel('username');

$field6 = new Zend_Form_Element_Password('password');
$field6->setRequired(true)
->setBelongsTo('params')
->setLabel('password');

$field7 = new Zend_Form_Element_Text('dbprefix');
$field7->setRequired(true)
->setBelongsTo('params')
->setLabel('dbprefix');

$cancel = $this->addSubmitButton('cancel', 'cancel');
$submit = $this->addSubmitButton('save', 'To step 2 >>');

$step1->addElement($field2)
->addElement($field3)
->addElement($field4)
->addElement($field5)
->addElement($field6)
->addElement($field7)
->addElement($cancel)
->addElement($submit);


Many thanks for your help
Regards

没有评论: