5

検証する必要のあるプロファイルデータの配列があります。

$user_group_profiles = $this->input->post('user_group_profiles', TRUE);
foreach ($user_group_profiles as $key => $user_group_profile)
{
    $this->form_validation->set_rules("user_group_profiles[$key][profile_name]", 'Profile Name', 'trim|required');
    $this->form_validation->set_rules("user_group_profiles[$key][birthdate]", 'Birthdate', 'trim|required');

    // TODO: heigth/weight not required, but the validation somehow makes it required
    $this->form_validation->set_rules("user_group_profiles[$key][height]", 'Height', 'trim|greater_than[0]');
    $this->form_validation->set_rules("user_group_profiles[$key][weight]", 'Weight', 'trim|greater_than[0]');
}

高さと重量はオプションですが、これらのフィールドに値が設定されていない場合、CI検証は文句を言います。Avar_dump($user_group_profiles);はこれを示しています:

array
  'ugp_b33333338' => 
    array
      'profile_name' => string '' (length=0)
      'birthdate' => string '' (length=0)
      'height' => string '' (length=0)
      'weight' => string '' (length=0)

何が間違っているのでしょうか?

編集1:

CIのForm_validationライブラリにアクセスして$_field_data、パブリックメンバーにしました。後でvar_exportすると、次のようになります。

  'user_group_profiles[ugp_833333338][height]' => 
    array
      'field' => string 'user_group_profiles[ugp_833333338][height]' (length=42)
      'label' => string 'Height' (length=6)
      'rules' => string 'greater_than[1]' (length=15)
      'is_array' => boolean true
      'keys' => 
        array
          0 => string 'user_group_profiles' (length=19)
          1 => string 'ugp_833333338' (length=13)
          2 => string 'height' (length=6)
      'postdata' => string '' (length=0)
      'error' => string 'The Height field must contain a number greater than 1.' (length=54)
4

2 に答える 2

7

わかりました-私はこれに1時間費やしました-そして私は問題と解決策を解決しました

問題は、テストしている変数が配列の一部である場合、CIはフィールドが設定されていると解釈するため、値が「含まれている」ことです(空の場合でも)。その「値」は0より大きい数値ではないため、失敗します。

したがって、検証に合格するために、「空」の場合は$ _POST($ user_group_profilesではない)変数の設定を解除する必要があります。注-検証は$_POSTで実行されます-そのため、$user_group_profilesではなく$_POSTの設定を解除します

したがって、回避策は次のとおりです。

$user_group_profiles = $this->input->post('user_group_profiles', TRUE);
foreach ($user_group_profiles as $key => $user_group_profile)
{
     // Set the rules
     $this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required");
     $this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required");
     $this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]");
     $this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]");

     // Now check if the field is actually empty
     if (empty($user_group_profile['height']))
     {
         // If empty, remove from array so CI validation doesnt get tricked and fail
         unset($_POST[$key]['height']);
     }
     if (empty($user_group_profile['weight']))
     {
         unset($_POST[$key]['weight']);
     }
}

私はこれをテストしました-そしてそれはあなたの問題を修正します。

$ _POSTデータに触れたくない場合は、次のようにプログラムすることもできます。

foreach ($user_group_profiles as $key => $user_group_profile)
    {
         // Set the rules
         $this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required");
         $this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required");


         // Now check if the field is actually empty
         if ( ! empty($user_group_profile['height']))
         {
             $this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]");
         }
         if ( ! empty($user_group_profile['weight']))
         {
             $this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]");
         }
    }
于 2012-06-01T12:38:28.480 に答える
1

これをパーツに分解してみましょう。ここでお手伝いできることを願っています。

2番目の「TRUE」引数を使用してXSSフィルタリングに対して次のことを行っていると想定しています:

$user_group_profiles = $this->input->post('user_group_profiles', TRUE);

実際には、フォーム検証ルールを使用して XSS フィルタリングを行うことができます。または、ルールの後に投稿をフィルタリングしたい場合。私の好みについては、こちらをご覧ください:

$this->form_validation->set_rules('profile_name', 'Profile Name', 'xss_clean|trim|required');

そのため、フォーム検証ライブラリの CI 規則に従うことができます。フォーム検証ライブラリを使用する前に投稿を取得する必要はありません。これは、フィールド名によって POST データを自動検出するためです。例えば:

$this->form_validation->set_rules('profile_name', 'Profile Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('birthdate', 'Birthdate', 'trim|required|xss_clean');
$this->form_validation->set_rules('height', 'Height', 'trim|greater_than[0]|numeric');
$this->form_validation->set_rules('weight', 'Weight', 'trim|greater_than[0]|numeric');

if ($this->form_validation->run() == FALSE) {
    $this->load->view('my_view_where_this_post_came_from');
} else {
    $profile_name = $this->input->post('profile_name');

    //or if you prefer the XSS check here, this:
    //$profile_name = $this->input->post('profile_name', TRUE);

    $birthdate= $this->input->post('birthdate');
    $height= $this->input->post('height');
    $weight= $this->input->post('weight');

    //$user_group_profiles = $this->input->post();

}

これが役立つことを願っています!

編集:私もこれに気づきました。投稿配列全体を取得しようとしている場合、コードは次のとおりです。

$user_group_profiles = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter 
$user_group_profiles = $this->input->post(); // returns all POST items without XSS filter

いいえ:

 $user_group_profiles = $this->input->post('user_group_profiles');

$_POST 名がわからない場合や混乱している場合に役立ちます。これを行うと、そのデータが存在するかどうかを確認できます! 最初の行として次のようにします。

var_dump($_POST);
exit();
于 2012-05-18T03:35:35.870 に答える