2013年10月19日土曜日

CodeIgniterのFormヘルパーのバグ(form_prep())

最近はFuelを使っていたのだがFuelの動作要件を満たさないサーバに当たってしまったので久々にCodeIgniter (2.1.4)を利用。

なんだけども、CodeIgniterのFormヘルパーにバグがあって困った。

Formヘルパーの各種関数を使うとvalueに設定する値は文字参照に変換してくれるのだが、同じname属性のものを2回使うと2回目は文字参照に変換してくれない。

これ↓を参考にform_helper.phpを拡張するMY_form_helper.phpを作成してみた。
https://bitbucket.org/matsuu/codeigniter-reactor/commits/20ca07f73bc3/

先人に感謝
MY_form_helper.php
function form_prep($str = '', $field_name = '')
{
static $prepped_fields = array();
// if the field name is an array we do this recursively
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = form_prep($val);
}
return $str;
}
if ($str === '')
{
return '';
}
// we've already prepped a field with this name
// @todo need to figure out a way to namespace this so
// that we know the *exact* field and not just one with
// the same name
if (isset($prepped_fields[$field_name]) && $prepped_fields[$field_name] == $str)
{
return $str;
}
$str = htmlspecialchars($str);
// In case htmlspecialchars misses these.
$str = str_replace(array("'", '"'), array("'", """), $str);
if ($field_name != '')
{
$prepped_fields[$field_name] = $prepped_fields[$field_name] = $str;;
}
return $str;
}
/* End of file MY_form_helper.php */
/* Location: ./application/helper/MY_form_helper.php */

0 件のコメント: