日本CodeIgniterユーザ会

日本国内におけるCodeIgniterユーザのためのユーザ会

技術情報


FORMのPOSTデータを配列で受け取る際の処理方法

2008年5月16日 - 15:20 | 投稿者 admin

http://codeigniter.com/wiki/Assosiative_Arrays_via_POST/

こちらの本家WIKIに、FORMのPOSTデータを配列で受け取る際の処理が記載されています。

この中で一部NOTICEエラーを出すか所がありましたので、修正版を記述しておきます。

MY_Validation.phpクラスのメソッド

<code>

// ——————————————————————–
/**
* Set Fields
*
* This function takes an array of field names as input
* and generates class variables with the same name, which will
* either be blank or contain the $_POST value corresponding to it
*
* @access public
* @param string
* @param string
* @return void
*/
/**
* 2008-05-16 KUNIHARU Tsujioka update
* no isset($_POST[$key]) NOTICE ERROR skip
*/
function set_fields($data = ”, $field = ”)
{
if ($data == ”)
{
if (count($this->_fields) == 0)
{
return FALSE;
}
}
else
{
if ( ! is_array($data))
{
$data = array($data => $field);
}

if (count($data) > 0)
{
$this->_fields = $data;
}
}

foreach($this->_fields as $key => $val)
{
if ( ! isset($_POST[$key]))
{
$_POST[$key] = $key;
}
$this->_value[$key] = $_POST[$key];
//2008-05-16 KUNIHARU Tsujioka update

//$this->_value[$key] = ( ! isset($_POST[$key]) OR is_array($_POST[$key])) ? ” : $this->prep_for_form($_POST[$key]);
//$this->_value[$key] = $_POST[$key];

/*
$error = $key.’_error’;
if ( ! isset($this->$error))
{
$this->$error = ”;
}
*/
// USE INSTEAD: $this->error(‘username’);

}
}

</code>

の修正と、

<code>

// ——————————————————————–

/**
* Return error message of given field name
*
* @access public
* @param string
* @return string
*/
/**
* 2008-05-16 KUNIHARU Tsujioka update
* no isset$this->_error_array[$field]) NOTICE ERROR skip
*/
function error($field){

//return $this->_error_array[$field];

if (isset($this->_error_array[$field]))
{
return $this->_error_array[$field];
}
else
{
return NULL;
}
}
</code>

の2か所の修正が必要です。

setされていないデータを参照したときにNOTICEが出ますので、これをisset()で判定し、ない場合は処理を行わないようスキップさせます。

カテゴリー 技術情報 | コメント(0) »