2012-05-10 31 views
0

下拉我有這樣的搜索功能:避免驗證表單在笨

public static function search_form() 
{ 
    $form = new Form('search_form'); 

    $form->field('keyword', 'text', array 
    (
     'min_length' => 4, 
     'max_length' => 15, 
     'alphanumeric' => lang('alphanumeric') 
    )); 
    $form->field('category', 'select', array 
    (
     'cat1' => 'Category 1', 
     'cat2' => 'Category 2', 
     'cat3' => 'Category 3' 

    ), $value); 

    if($data = $form->validate()) 
    {   
     header('Location: '.WEB.sprintf('search/'.$data['keyword'].'/'.$data['category'])); 
    } 

    return $form; 
} 

和可選的驗證,即正常工作與投入,但不與下拉菜單:

// Validate 
public function validate() 
{ 
    $this->script(); 

    if(!$this->submitted) 
    { 
     return false; 
    } 

    $this->valid = true; 

    foreach($this->fields as $field) 
    { 
     $value = $this->request[$field[0]]; 

     if(isset($field[2]['optional']) && $field[2]['optional']) 
     { 
      if($value == '') continue; 
     } 

     foreach($field[2] as $validator=>$data) 
     { 
      if($validator == 'optional') continue; 

      $custom = !method_exists($this, $validator); 

      if((!$custom && !$this->$validator($value, $data)) || ($custom && !$this->custom($data, $value))) 
      { 
       $this->valid = false; 

       $this->errors[$field[0]] = $this->error_message($field[0], $validator, $data); 

       break; 
      } 
     } 
    } 

    return $this->valid?$this->request:false; 
} 

當我添加可選參數在文本輸入中可用,但不能用於選擇輸入:

$form->field('category', 'select', array 
    (
     'optional'  => true, 
    ................. 

它被轉換爲HT中的值ML 這裏是選擇情況:

 # Input 
     switch($field[1]) 
     { 
      case 'text': 
      case 'password': 
       if($this->submitted) 
       { 
        echo '<input id="'.$this->id.'_'.$field[0].'" class="text" type="'.$field[1].'" name="'.$field[0].'" value="'.htmlentities(utf8_decode($this->request[$field[0]]), ENT_QUOTES).'"/>'; 
       } else 
       { 
        echo '<input id="'.$this->id.'_'.$field[0].'" class="text" type="'.$field[1].'" name="'.$field[0].'"'.(isset($field[3])?' value="'.$field[3].'"':'').'/>'; 
       } 
       break; 
      case 'textarea': 
       echo '<textarea type="text" id="'.$this->id.'_'.$field[0].'" name="'.$field[0].'">'; 
       echo '</textarea>'; 
       break; 
      case 'select': 
       echo '<select id="'.$this->id.'_'.$field[0].'" name="'.$field[0].'">'; 

       foreach($field[2] as $key=>$value) 
       { 
        echo '<option value="'.$key.'"'; 

        if($this->submitted && $this->request[$field[0]] == $key) 
        { 
         echo ' selected="selected"'; 
        } elseif(isset($field[3]) && $field[3] == $key) 
        { 
         echo ' selected="selected"'; 
        } 

        echo '>'.$value.'</option>'; 
       } 

       echo '</select>'; 
       break; 
     } 

的問題是,我需要爲第一輸入(關鍵字)而不是下拉驗證,它似乎是我要驗證這兩個領域,我不要不想要。有沒有辦法繞過第二次驗證?

編輯:其實,我不知道爲什麼下拉菜單沒有通過驗證,我只是在嘗試提交表單時得到「error_category」。

問題解決了:

的驗證功能實際上是試圖驗證第二場($場[2]),並尋找[「可選」]值,但無法找到它,我的第二個領域實際上是categories數組。 爲了解決這個問題,我在第二個位置添加了一個新的數組,其中包含「optional = true」,並將我的實際數組移到了第三個位置。 驗證是那麼好,但是選項值沒有露面,所以我只是不得不改變:

foreach($field[2] as $key=>$value) 
      { 
       echo '<option value="'.$key.'"'; ... 

和替換$場[2] $場[3] 然後,只需移動值字段(先前$字段[3])到$字段[4]

+0

有點令人困惑。你如何使驗證可選? – stef

+0

通過添加' '可選'=>成立,',例如: '$形式 - >字段( '密碼', '密碼',陣列 \t \t( \t \t \t '可選的' \t \t => \t真, \t \t \t '確認' \t \t => \t真, \t \t \t 'MIN_LENGTH' \t => \t \t));' – PhilMarc

+0

有一個範圍問題,我可能找到了解決方案。我會盡快告訴你的 – PhilMarc

回答

0

問題解決了:

的驗證功能實際上試圖驗證第二場($領域[2])和尋找['可選']值,但無法找到它,因爲我的第二個字段實際上是categories數組。爲了解決這個問題,我在第二個位置添加了一個新陣列,其中包含「optional = true」,並將我的實際陣列移至第三位。

驗證是那麼好,但是選項值沒有露面,所以我只是不得不改變:

foreach($field[2] as $key=>$value) 
     { 
      echo '<option value="'.$key.'"'; ... 

並更換$場[2]通過$場[3]然後,將值字段(以前的$ field [3])移動到$ field [4]