可变按钮标签

时间:2014-03-19 11:05:15

标签: php zend-framework

我收到了一个用户注册表单,并将其用作编辑用户表单。 现在我想让这个形式变得更加可变。这意味着在我的情况下:我想为不同的动作设置一个特定的按钮标签 如果通过RegisterAction调用表单,则标签应为" Register"如果它是通过EditAction调用的,它应该是"更新用户"。我尝试了一些东西,但现在我的想法用完了。

这是我的代码:

CustomerController.php

...
 public function registerAction(){
        $form = new Application_Form_Register();
        $request = $this->_request->getParams();

    if(isset($request['registerbtn']) && ($form->isValid($request) )){
        $customerModel = new Application_Model_Customer();
        $customerArr = $customerModel->setCustomer($request,true);
        $this->redirect('/customer/detail/id/'.$customerArr);
    }
    else{
        $this->view->form = $form;
        $this->view->button = "Register"; //TEST
    }
}

public function editAction(){
        $request = $this->_request->getParams();
        if(isset($request['id']) && !empty($request['id'])){
            $form = new Application_Form_Register();

            $form->addElement('hidden', 'id', array(
                'required'   => true,
                'value'      => $request['id'],
                'validators' => array(
                    $digits = new Zend_Validate_Digits()
                )
            ));

            if(isset($request['registerbtn']) && ($form->isValid($request) )){
                $customerModel = new Application_Model_Customer();
                $id = $customerModel->setCustomer($request,false);
                $this->redirect('/customer/detail/id/'.$id);
            }else{
                $modelResult = new Application_Model_Customer();
                $customer = $modelResult->getCustomer($request['id']);
                $cArr = $customer->toArray();
                $form->populate($cArr);

                $this->view->form = $form;
                $this->view->button = "Update user"; //TEST
            }
        }else{
            $this->redirect('/');
        }
}
...

观看次数

// register.phtml - begin
<h2>Registration</h2>
<?php
    $this->headTitle('Registration');

    $button = $this->button; //TEST
    $this->form->button = $button; //TEST

    echo $this->form;
    echo $this->error;?>
// register.phtml - end

// edit.phtml - begin
<?php
    echo $this->headline;
    $this->headTitle('Update user');

    $button = $this->button; //TEST
    $this->form->button = $button; //TEST

    echo $this->form;
?>
// edit.phtml - end

表格

    //
    ...
    $this->addElement('submit', 'registerbtn', array(
        'ignore'   => true,
        'label'    => $button, //TEST
        'decorators' => $this->buttonDecorators,
    ));
    ...

我担心这是完全错误的,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

尝试类似

的内容
if ($cas1)
    $form->getElement('submit')->setLabel('cas1');
else
    $form->getElement('submit')->setLabel('cas2');