没有错误消息时回调函数笨表单验证运行

问题描述:

对不起我的英文不好,没有错误消息时回调函数笨表单验证运行

我创建的形式,发生在我的笨项目的一些值指定报告选项。我想显示在我的回调函数中创建的错误消息。我有3个回调函数为“checkStartDate()checkFinishDate()checkIssueExists()”如果验证部分处理这样的不回调设置“需要”的错误,没关系。但是,当我在回调函数中设置错误消息时,它们不显示。重要的事情; 如果“required”规则未通过,我的所有回叫错误都会显示为。但是,如果“必需”条件通过,则没有错误消息。

我有错误消息的问题,回调函数正常工作。当我给出错误的值时,它们返回FALSE。

这里是我的代码:

观点:

<div id="newIssue"> 
<p> 
    Fill the form below, add your issue to searching pool.</br> 
</p>  

<?php 
    if(isset($_GET['validationErrors'])) 
     echo $_GET['validationErrors']; 
?> 

<?=form_open("main/add-to-pool");?> 
    <table class="form-table"> 
     <tr> 
      <td>Issue </td> 
      <td><?=form_input('issue', $this->input->post('issue'));?></td> 
     </tr> 

     <tr> 
      <td>Report timing </td> 
      <td> 
       <?php 
        $options = array(
         'daily' => 'Every day', 
         'weekly' => 'Every week', 
         'monthly' => 'Every month', 
        ); 

        echo form_dropdown('timing', $options, 'weekly'); 
       ?> 
      </td> 
     </tr> 

     <tr> 
      <td>Start Date </td> 
      <td> 
       <?=form_input(array('name' => 'startDate', 'type' => 'date'), $this->input->post('startDate'));?> 
      </td> 
     </tr> 

     <tr> 
      <td>Finish Date </td> 
      <td> 
       <?=form_input(array('name' => 'finishDate', 'type' => 'date'), $this->input->post('finishDate'));?> 
      </td> 
     </tr> 

     <tr> 
      <td>Location based </td> 
      <td>&nbsp&nbsp 
       <?php 
       echo form_radio(array(
         'name'  => "location", 
         'class'  => "radio", 
         'checked' => TRUE 
        )); 
       ?> 
       Yes 

       <?php 
       echo form_radio(array(
         'name'  => "location", 
         'class'  => "radio", 
         'checked' => FALSE 
        )); 
       ?> 
       No 
      </td> 
     </tr> 

     <tr> 
      <td></td> 
      <td> 
       <div style="float:right"> 
        <?php 
        echo form_submit(array(
          'class' => 'btn btn-info', 
          'id' => 'addToPool', 
          'name' => 'addToPool', 
          'value' => 'Add to Pool' 
         )); 
        ?> 
       </div> 
      </td> 
     </tr> 
    </table> 
<?=form_close();?> 

控制器:

public function addToPool() 
{ 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('issue', 'Issue', 'required|trim|xss_clean|callback_checkIssueExists'); 
    $this->form_validation->set_rules('timing', 'Report Timing', 'required|trim|xss_clean'); 
    $this->form_validation->set_rules('startDate', 'Start Date', 'required|trim|xss_clean|callback_checkStartDate'); 
    $this->form_validation->set_rules('finishDate', 'Finish Date', 'required|trim|xss_clean|callback_checkFinishDate'); 

    if ($this->form_validation->run()) { 
     $issueContent = preg_replace("/\s+/"," ", $this->input->post('issue')); 
     $startDate = date("Y-m-d H:i:s", strtotime($this->input->post('startDate'))); 
     $finishDate = date("Y-m-d H:i:s", strtotime($this->input->post('finishDate'))); 

     $issue = new Issue(); 

     $issue->setContent($this->clearTurkishCharacters($issueContent)); 
     $issue->setTrContent($issueContent); 
     $issue->setCreatedDate(date("Y-m-d H:i:s")); 
     $issue->setUpdatedDate(date("Y-m-d H:i:s")); 
     $issue->setStartDate($startDate); 
     $issue->setFinishDate($startDate); 

     $user = new User(); 
     $user->setUsername($this->session->userdata('username')); 
     $user->dbToUser(); 

     $issue->setUser($user); 

     if ($issue->issueToDb()) { 
      $_GET['newIssueFancyBox'] = "close"; 

      $this->home(); 
     } else 
      echo "An error occured while adding user to database!"; 

    } else { 
     $_GET['validationErrors'] = validation_errors('<div class="alert alert-error">','</div>'); 
     $_GET['newIssueFancyBox'] = "open"; 

     $this->home(); 
    } 
} 

public function checkStartDate() 
{ 
    $startDate = $this->input->post('startDate'); 

    if (strtotime($startDate) < strtotime('-1 day')) { 
     $this->form_validation->set_message('checkStartDate', 'The %s field cannot take a value before today.'); 
     return FALSE; 
    } else { 
     return TRUE; 
    } 
} 

public function checkFinishDate() 
{ 
    $startDate = $this->input->post('startDate'); 
    $finishDate = $this->input->post('finishDate'); 

    if (strtotime($finishDate) < strtotime($startDate) || strtotime($finishDate) < strtotime('-1 day')) { 
     $this->form_validation->set_message('checkFinishDate', 'The %s field cannot take a value before start date.'); 
     return FALSE; 
    } else { 
     return TRUE; 
    } 
} 

public function checkIssueExists() 
{ 
    $this->load->model('modelIssue'); 

    $issueContent = preg_replace("/\s+/"," ", $this->input->post('issue')); 

    $issue = new Issue(); 

    $issue->setContent($issueContent); 

    $user = new User(); 
    $user->setUsername($this->session->userdata('username')); 
    $user->dbToUser(); 

    $issue->setUser($user); 

    if($this->modelIssue->checkIssueExists($issue)) { 
     $this->form_validation->set_message('checkIssueExists', 'You have already the same issue in pool.'); 
     return FALSE; 
    } 
    else 
     return TRUE; 
} 
+0

显示回调函数的代码。 –

+0

它已经在代码中。 – bulutcagatay

+0

在视图中显示错误,使用(全部)或 – Rooneyl

我已经自己解决了这个问题,但我已经知道根据@ Philip的回答改变我的代码时会发生什么。在我的旧代码中,我有回调函数,然后我将它们更改为@ Philip的回答。但我仍然没有收到我想要显示的错误消息。我刚刚意识到,当我在同一个函数中创建一个User或Issue类对象时,我无法设置错误消息。

在我的旧代码(library/MY_Form_validation.php);

public function checkIssueExists() 
{ 

    $this->load->model('modelIssue'); 

    $issueContent = preg_replace("/\s+/"," ", $this->input->post('issue')); 

    $issue = new Issue(); 

    $issue->setContent($issueContent); 

    $user = new User(); 
    $user->setUsername($this->session->userdata('username')); 
    $user->dbToUser(); 

    $issue->setUser($user); 

    if (!$this->modelIssue->checkIssueExists($issue->getContent(), $issue->getUser()->getUsername())) { 
     return TRUE; 
    } else { 
     $this->form_validation->set_message('checkIssueExists', 'You have already the same issue in pool.'); 
     return FALSE; 
    } 
} 

你可以看到$issue = new Issue();$user = new User();线引起了我的问题。但我不明白为什么会发生。我有3个contorller类,

  • 主(处理请求和基本功能,如签到(),注册(),家(),仪表板()等)
  • 用户(用户基本功能,如getUsername() ,setId(),dbToUser(),userToDb()等)
  • 问题(基本问题,功能类似于getIssue(),SETUSER()的getContent()等)

我需要这些功能,所以我想,当我需要他们,我可以使用这些类。但在回调函数或MY_Form_validation.php文件中,我不能像上面那样使用它们。

我改变了我的代码(和其他页面依赖于我的代码),如下所示,它现在可以工作; library/MY_Form_validation.php;

public function checkIssueExists() 
{ 
    $issueContent = preg_replace("/\s+/"," ", $this->input->post('issue')); 
    $username = $this->session->userdata('username'); 

    if (!$this->modelIssue->checkIssueExists($issueContent, $username)) { 
     return TRUE; 
    } else { 
     $this->form_validation->set_message('checkIssueExists', 'You have already the same issue in pool.'); 
     return FALSE; 
    } 
} 

但我不知道为什么我不能在同一个函数中使用我的类时设置错误消息。

感谢您的帮助。

+0

验证类应该用于验证数据没有别的(即:在您的案例对象创建)。是的,你可以同时创建/查询对象,但基本上你可能要做的就是查询对象(基于你的ORM)。我看到你使用fancybox,而不是发回一个视图,你可能想发送一个包含所有验证错误的JSON对象,并将它们绑定到它们的输入字段。 – Philip

+0

这非常有帮助。其实我不知道如何在Codeigniter结构中使用fancybox。我要试试这个。 – bulutcagatay

而应该扩展使用回调

的CI_Form_validation

- 像一些别的尽量使用错误直列

echo form_error('field_name') 

OR

foreach(validation_errors() as $error): 
... 
endforeach; 

我也要建立在./config中一个form_validation.php配置文件

$config = array(
    'class/method' => array(
     array('field'=>'', 'label'=>'', 'rules'=>'required|checkIssueExists'), 
    ), 
); 

你现在干净的控制器方法

 // If the form validation cant find any key for this class/method 
     // in config/form_validation.php 
     // optionally you can pass in a key 
     // ie: if($this->form_validation->run('key')) 
     if(!$this->form_validation->run()) 
     { 
      return $this->home(); 
     } 

     //validation must have passed 
+0

这个答案不会使OP重新编写所有代码吗? – interlude

+0

我根据你的答案更新了我的代码,但它不起作用。但我发现了新的东西。 – bulutcagatay