Zend Framework Reg Ex不在更新表单上工作

时间:2011-07-25 17:51:54

标签: regex zend-framework

我有一个客户端输入表单,其中包含以下两个reg表达式,这些表达式在创建客户端时有效,但在更新客户端时无效。更新表单是一个扩展包装箱形式的类。

// Create text input for mobile
$mobile = new Zend_Form_Element_Text ('mobile');
$mobile->setLabel ('Mobile Number:')
        ->setDescription('Enter mobile in the format 353XXYYYYYYY')
        ->setOptions(array('size'=>'14'))
        ->setRequired(false)
        ->addValidator('Regex',false,array(
            'pattern'=>'/^\d{12}$/',
            'messages'=>array(
                Zend_Validate_Regex::INVALID => '\'%value%\' Invalid mobile number it does not match the required format 353XXYYYYYYY',
                Zend_Validate_Regex::NOT_MATCH =>'\'%value%\'does not match the required format 353XXXXXXXX')
                )
              )
        ->addFilter('HtmlEntities')
        ->addFilter('StringTrim');

    // Create text input for landline
$landline = new Zend_Form_Element_Text ('landLine');
$landline->setLabel ('Phone Number:')
        ->setDescription('Enter phone number in the format +353(0) X YYY YYYZ')
        ->setOptions(array('size'=>'20'))
        ->setRequired(false)
        ->addValidator('StringLength', false, array('min' => 8))
         ->addValidator('Regex', false, array(
              'pattern'   => '/^\+353\(0\)\s\d\s\d{3}\s\d{3,4}$/',
              'messages'  => array(
                Zend_Validate_Regex::INVALID    => 
                    '\'%value%\' In valid Phone number does not match required number format +353(0) X YYY YYYZ',
                Zend_Validate_Regex::NOT_MATCH  => 
                    '\'%value%\' does not match required number format of +353(0) X YYY YYYZ'
            )
        ))
        ->addFilter('HtmlEntities')
        ->addFilter('StringTrim');

当我在创建客户端时输入无效的移动或固定电话号码时,reg表达式会起作用并阻止保存记录。

但是,当我在更新客户端时输入无效的移动或固定电话号码时,reg表达式失败并发生404错误。

我认为该问题可能与我的控制器中的更新操作的获取部分有关,如下所示,但我无法弄清楚是什么导致了这一点,因为我在ini文件中配置的路由将记录检索为必需的。

public function updateAction(){
// generate input form
$form = new PetManager_Form_UpdateClient;
$this->view->form=$form;

/* if the requrest was made via post
   test if the input is valid 
   retrieve current record 
   update values and save to DB */

 if($form->isValid($this->getRequest()->getPost())){
  $input=$form->getValues();
  $client = Doctrine::getTable('PetManager_Model_Clients')
        ->find($input['clientid']);
   $client->fromArray($input);
   if($client->email=='')
           {$client->email=NULL;}
        if($client->mobile=='')
           {$client->mobile=NULL;}
        if($client->landLine=='')
           {$client->landLine=NULL;}
        if($client->address3=='')
           {$client->address3=NULL;}
   $client->save();

   $sessionClient = new Zend_Session_Namespace('sessionClient');
    $id = $client->clientid;
    $fname = $client->firstName;
    $lname = $client->lastName;
    $sessionClient->clientid=$id;
    $sessionClient->clientfName=$fname;
    $sessionClient->clientlName=$lname;
    $sessionClient->clientfName=$fname;

   $this->_helper->getHelper('FlashMessenger')
        ->addMessage('The record for '.$fname.' '.$lname. ' was successfully updated.');
   $this->_redirect('clients/client/success');
  }else{
     /* if GET request
        set filters and validators for GET input
        test if input is valid, retrieve requested
        record and pree-populate the form */
     $filters = array(
        'id'=>array('HtmlEntities','StripTags','StringTrim')
        );  
     $validators = array(
          'id'=>array('NotEmpty','Int')
          );
     $input = new Zend_Filter_Input($filters,$validators);
     $input->setData($this->getRequest()->getParams());
        if($input->isValid()){
            $qry = Doctrine_Query::create()
             ->from('PetManager_Model_Clients c')
                ->leftJoin('c.PetManager_Model_Counties co')
                ->where('c.clientid=?',$input->id);
            $result = $qry->fetchArray();
            if(count($result)==1){
                $this->view->form->populate($result[0]);
            }else{
                throw new Zend_Controller_Action_Exception('Page not found',404);
                }
        }else{
            throw new Zend_Controller_Action_Exception('Invalid Input');
        }
      }
}

所有人都非常感谢。

1 个答案:

答案 0 :(得分:0)

好的,我已经对此进行了排序,我愚蠢地在我的更新操作中留下了一个检查,以查看请求是否是通过帖子发出的,因为这是我的表单中定义的操作。

如果这有助于其他任何人,则更正后的代码如下所示。

//更新单个客户详细信息的操作   公共函数updateAction()   {     //生成输入表单     $ form = new PetManager_Form_UpdateClient;     $这 - >视图 - >形式= $形式;

/* if the requrest was made via post
   test if the input is valid 
   retrieve current record 
   update values and save to DB */
 if ($this->getRequest()->isPost()) {
 if($form->isValid($this->getRequest()->getPost())){
  $input=$form->getValues();
  $client = Doctrine::getTable('PetManager_Model_Clients')
        ->find($input['clientid']);
   $client->fromArray($input);
   if($client->email=='')
           {$client->email=NULL;}
        if($client->mobile=='')
           {$client->mobile=NULL;}
        if($client->landLine=='')
           {$client->landLine=NULL;}
        if($client->address3=='')
           {$client->address3=NULL;}
   $client->save();

   $sessionClient = new Zend_Session_Namespace('sessionClient');
    $id = $client->clientid;
    $fname = $client->firstName;
    $lname = $client->lastName;
    $sessionClient->clientid=$id;
    $sessionClient->clientfName=$fname;
    $sessionClient->clientlName=$lname;
    $sessionClient->clientfName=$fname;

   $this->_helper->getHelper('FlashMessenger')
        ->addMessage('The record for '.$fname.' '.$lname. ' was successfully updated.');
   $this->_redirect('clients/client/success');
   }
  }else{
     /* if GET request
        set filters and validators for GET input
        test if input is valid, retrieve requested
        record and pree-populate the form */
     $filters = array(
        'id'=>array('HtmlEntities','StripTags','StringTrim')
        );  
     $validators = array(
          'id'=>array('NotEmpty','Int')
          );
     $input = new Zend_Filter_Input($filters,$validators);
     $input->setData($this->getRequest()->getParams());
        if($input->isValid()){
            $qry = Doctrine_Query::create()
             ->from('PetManager_Model_Clients c')
                ->leftJoin('c.PetManager_Model_Counties co')
                ->where('c.clientID=?',$input->id);
            $result = $qry->fetchArray();
            if(count($result)==1){
                $this->view->form->populate($result[0]);
            }else{
                $t=count($result);
                throw new Zend_Controller_Action_Exception('Page not found',404);
                }
        }else{
            throw new Zend_Controller_Action_Exception('Invalid Input');
        }
      }

}