用zend_form连接字段

时间:2013-07-27 13:51:39

标签: zend-framework

我正在使用百夫长的zend框架,我的表单有问题。我有字段num_ordrecode,两者都是主键,我的表中有一些名为conca的列,它是两个字段num_ordre和{{的串联1}}。

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

这是我的代码

code

2 个答案:

答案 0 :(得分: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;
}

希望这有帮助

答案 1 :(得分: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
    }
}

未经测试的代码