dooid3005 2016-02-09 19:19
浏览 26
已采纳

确认弹出窗口链接到自定义验证器 - Zend

Right now I have a form that adds an instance of Dog to an Owner. I want to make it so that if the Dog already has an owner, a confirmation dialogue pops up. I can get the confirmation to pop up everytime the button is pressed, but am not quite sure how to add the conditional to it.

My code is as follows:

<div class="row-fluid"> 
    <?php echo $this->form()->openTag($form); ?>
    <div class="form-group ">
        <?php echo ctrlGroup($this, AddDogForm::KEY_DOG_ID, !($this->uberAdmin)); ?>

        <?php $addDog = $this->form->get(AddDogForm::KEY_ADD_BTN); ?>
        <?php $addDog->setAttribute("class", "btn btn-info"); ?>
        <?php $addDog->setAttribute('onclick', 'if (confirm("Are you sure?")) { document.form.submit(); } return false;'); ?>

        <?php echo $this->formSubmit($addDog); ?>
    </div>
    <?php echo $this->form()->closeTag(); ?>

This will bring a popup up every time that the button is clicked, but I want it to check what's been typed before putting up the popup.

The following is my AddDogForm:

class AddDogForm extends Form{
const KEY_PROJECT_ID = "project_id";
const KEY_ADD_BTN = "project_add_btn";

public function __construct($name = null, $options = array()){
    parent::__construct($name);

    $this->setAttribute('method', 'post');
    $this->setAttribute('class', 'form-inline');

    $id = new Text(self::KEY_DOG_ID);
    $id->setAttribute("id", self::KEY_DOG_ID);
    $id->setLabel("dogid to add");
    $this->add($id);


    $add = new Button(self::KEY_ADD_BTN);
    $add->setAttributes(array("id", self::KEY_ADD_BTN));
    $add->setLabel("Add Dog");
    $add->setValue("Add Dog");
    $this->add($add);   
    }


}

UPDATE: I can add a custom validator by adding this to the AddDogForm:

public function hasNoOwner($dogId) {
    $drm = DogResourceModel::create();
    $dogs = $drm->find($dogId);  
    $dog = array_values($dogs)[0]; 
    if($dog->getOwnerId()){
        return false; 
    } else {
        return true; 
    }
}

public function getInputFilterSpecification() {
    $hasOwnerValidator = array(
            'required' => true,
            'validators' => array(
                    new Callback(
                            array(
                                    $this,
                                    'hasNoOwner'
                            )),
            ),
    );
    return array(
            self::KEY_PROJECT_ID => $hasOwnerValidator
    );
}   

This will prevent the user from being able to input. Can I instead alter this so that it asks for confirmation rather than preventing the POST from happening?

  • 写回答

2条回答 默认 最新

  • dsdv76767671 2016-02-10 08:10
    关注

    May be you can add custom validator on post data on one of the field to validate against and then you can just show a general message.

    There are two possible way you can achieve this.

    1. add validator/custom validator based on your requirement on runtime in controller

      public function fooAction() {
          $form = new Dog_Form();
          if ($this->getRequest()->isPost()) {
              // all post data is valid now we validate for dog/owner thing
              $form->getElement('star_tag')->addValidator('Db_NoRecordExists',false,
                 [
                     'table' => 'table name', 
                     'field' => 'field to check duplicate', 
                     'messages' => array('recordFound' => 'already_linked')
                  ]
              );
              // redirect after successful insert
          }
          $this->view->form = $form;
      }
      
    2. you can use isValidPartial method of zend form and do your validation over there. check http://framework.zend.com/manual/1.12/en/zend.form.quickstart.html#zend.form.quickstart.validate to see how it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料