setting submit name outside the form
I have a zend_form for updating and inserting data. I want it to have diffrent sumbit labels but it doesn't seem to work. My form:
class Staff_Form extends Zend_Form
{
public function init()
{
$this->setAction('')
->setMethod('post');
开发者_JS百科$firstname = new Zend_Form_Element_Text('firstname');
$firstname->setLabel('firstname')->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setName('insert');
$this->addElements(array(
$firstname,
$submit
));
}
}
Now I would expect this to work:
//in controller
$form = new My_Form();
$form->getElement('submit')->setName('update');
But it gives a fatal error: Fatal error: Call to a member function setName() on a non-object in..... So I tried:
$first = $form->getElement('firstname');
var_dump($first);
echo 'html break';
$submit = $form->getElement('submit');
var_dump($submit);
die();
It appears $submit is NULL
What Am I doing wrong?
Should really be an typing error somewhere in your code, i just tested the following which works:
class Application_Form_Test extends Zend_Form
{
public function init()
{
$sub = new Zend_Form_Element_Submit('submit');
$sub->setLabel('Submit Me');
$this->addElement($sub);
}
}
//controller - It works
$form = new Application_Form_Test();
$sub = $form->getElement('submit');
$sub->setName('wahahahar');
You could pass in the submit value when instatiating the form.
// form
class App_Form_Something extends Zend_From {
protected $submitName
public function init() {
$this->addElement('submit',$this->submitName, array(
'label' => $this->submitName
));
public function setSubmitName($submitName) {
$this->submitName = $submitName;
}
}
Then in your controller
$form = new App_Form_Something(array('submitName' => 'Update or something'));
I use this techique quite often to pass in Id's of certain database objects so they can be auto populated etc.
Good luck
精彩评论