在表单之外设置提交名称
我有一个 zend_form 用于更新和插入数据。 我希望它有不同的 sumbit 标签,但它似乎不起作用。 我的形式:
class Staff_Form extends Zend_Form
{
public function init()
{
$this->setAction('')
->setMethod('post');
$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
));
}
}
现在我希望这能起作用:
//in controller
$form = new My_Form();
$form->getElement('submit')->setName('update');
但它给出了一个致命错误: 致命错误:在非对象上调用成员函数 setName()...... 所以我尝试了:
$first = $form->getElement('firstname');
var_dump($first);
echo 'html break';
$submit = $form->getElement('submit');
var_dump($submit);
die();
$submit 似乎为 NULL
我做错了什么?
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');
$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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确实应该是代码中某处的输入错误,我刚刚测试了以下有效的方法:
Should really be an typing error somewhere in your code, i just tested the following which works:
您可以在创建表单时传入提交值。
然后在你的控制器中,
我经常使用这种技术来传递某些数据库对象的 ID,以便它们可以自动填充等。
祝你好运
You could pass in the submit value when instatiating the form.
Then in your controller
I use this techique quite often to pass in Id's of certain database objects so they can be auto populated etc.
Good luck