在表单之外设置提交名称

发布于 2024-12-07 20:27:56 字数 1001 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疧_╮線 2024-12-14 20:27:56

确实应该是代码中某处的输入错误,我刚刚测试了以下有效的方法:

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');

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');
梦旅人picnic 2024-12-14 20:27:56

您可以在创建表单时传入提交值。

// 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;
}
}

然后在你的控制器中,

$form = new App_Form_Something(array('submitName' => 'Update or something'));

我经常使用这种技术来传递某些数据库对象的 ID,以便它们可以自动填充等。

祝你好运

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文