0

我正在使用 Zend 框架和 centurion,但我的表单有问题。我有字段num_ordrecode,它们都是主键,并且我的表中有名为 的列conca,它是两个字段的串联,num_ordre并且code

我的问题是,在我的方法帖子中,我想测试我的数据库中是否已经存在num_ordre和的连接;code但问题是如何在发布之前获取字段的值。

这是我的代码

public function postAction(){

    $this->_helper->viewRenderer->setNoRender(TRUE);
    $user = new Param_Model_DbTable_Verification();

    $form= $this->_getForm();
    $form->getElement('Num_ordre')->addValidator(new Zend_Validate_Db_NoRecordExists('verifications','Num_ordre'));
    $form->getElement('Num_ordre')->setRequired(true);
    $posts = $this->_request->getPost();
    if ($this->getRequest()->isPost()) {

        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $row=$user->createRow();
            $row->code=$this->_getParam('code');
            $row->Num_ordre=$this->_getParam('Num_ordre');
            $row->Libelle_champ=$this->_getParam('Libelle_champ');
            $row->comparaison=$this->_getParam('comparaison');
            $row->formule=$this->_getParam('formule');
            $row->obligatoire=$this->_getParam('obligatoire');
            $row->Req_traduction=$this->_getParam('Req_traduction');
            $row->tolerance_erreur=$this->_getParam('tolerance_erreur');
            $row->Mess_erreur=$this->_getParam('Mess_erreur');
            $row->conca=$this->_getParam('Num_ordre').$this->_getParam('code');
            $row->save();
            if( isset ($posts['_addanother'])){
                $_form = $this->_getForm();
                $_form->removeElement('id');
                $this->_helper->redirector('new','admin-verification');
            }
            else
                $this->_helper->redirector(array('controller'=>'Admin-verification'));

            }else{
                parent::postAction();
            }
            }}
4

2 回答 2

0

您可以手动调用isValid验证器:

$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
    $formValues = $form->getValues();
    $uniqueValidator = new Zend_Validate_Db_NoRecordExists('verifications','conca');
    if ($uniqueValidator->isValid($formValues['Num_ordre'] . $formValues['Num_ordre'])) {
        // valid
    } else {
        // not unique
    }
}

未经测试的代码

于 2013-07-27T14:46:09.813 回答
0

你这样检查怎么样?

public function postAction(){

    $this->_helper->viewRenderer->setNoRender(TRUE);
    $user = new Param_Model_DbTable_Verification();

    $form= $this->_getForm();
    $form->getElement('Num_ordre')->addValidator(new Zend_Validate_Db_NoRecordExists('verifications','Num_ordre'));
    $form->getElement('Num_ordre')->setRequired(true);
    $posts = $this->_request->getPost();
    if ($this->getRequest()->isPost()) {

        $formData = $this->getRequest()->getPost();
        $mdl = new Model_Something();   //Call your model so you can test it
        //Add a condition here
        if ($form->isValid($formData) && $mdl->uniqueConcatenated($this->_getParam('num_ordre'), $this->_getParam('code')) {
            $row=$user->createRow();
            /**truncated, keep your existing code here**/
        }
    }
}

然后在你的模型 Model_Something

public function uniqueConcatenated($numOrder, $code) {
  $concatenated = $numOrder.$code;
  //Check for the existence of a row with the concatenated field values
  $select = $this->select();
  $select->where('concatenatedField = '.$concatenated);
  $row = $this->fetchRow($select);
  return $row;
}

希望这可以帮助

于 2013-07-27T14:26:34.383 回答