FuelPHP Ver1.5.1の話題です。
FuelPHPのForm::inputの第二引数は自動的にhtmlentitiesが適用されます。
ですので、
<?php echo Form::input('name', Input::post('name')); ?>
とInput Classの値をそのまま渡しても大丈夫です。
ソースは以下のようになっています。
\core\form.php
ControllerからViewに渡す値はset_safe等で渡さない限り、自動的にhtmlentitiesが適用されます。
Controllerで
$data['name'] = Input::post('name');
View::forge('user/contents', $data);
Viewで
<?php echo Form::Input('name', $name); ?>
とすると、Viewにnameが渡されたときにhtmlentitiesが適用され、Form::Inputに渡されたときにまたhtmlentitiesが適用されてしまいます。
しかし、 FuelPHPのデフォルト設定は「double_encodeをしない」なので、一度HTMLエンティティに変換された文字が再度変換されることはないので、特殊なケースを除いて問題ないです。
特殊なケースとは「&」等を入力した場合です。
こちらについては、前の記事を参照ください。
※「&:」等が入力されることがないのであればいい気もするんですが、例えば「&は&に変換されます」という記事を書きたいとかいう場合は困る気がしました。
FuelPHPのForm::inputの第二引数は自動的にhtmlentitiesが適用されます。
ですので、
<?php echo Form::input('name', Input::post('name')); ?>
とInput Classの値をそのまま渡しても大丈夫です。
ソースは以下のようになっています。
\core\form.php
public static function input($field, $value = null, array $attributes = array())
{
return static::$instance->input($field, $value, $attributes);
}
\core\form\instance.php
\core\form\instance.php
public function input($field, $value = null, array $attributes = array())
{
if (is_array($field))
{
$attributes = $field;
! array_key_exists('value', $attributes) and $attributes['value'] = '';
}
else
{
$attributes['name'] = (string) $field;
$attributes['value'] = (string) $value;
}
$attributes['type'] = empty($attributes['type']) ? 'text' : $attributes['type'];
if ( ! in_array($attributes['type'], static::$_valid_inputs))
{
throw new \InvalidArgumentException(sprintf('"%s" is not a valid input type.', $attributes['type']));
}
if ($this->get_config('prep_value', true) && empty($attributes['dont_prep']))
{
$attributes['value'] = $this->prep_value($attributes['value']);
}
unset($attributes['dont_prep']);
if (empty($attributes['id']) && $this->get_config('auto_id', false) == true)
{
$attributes['id'] = $this->get_config('auto_id_prefix', 'form_').$attributes['name'];
}
$tag = ! empty($attributes['tag']) ? $attributes['tag'] : 'input';
unset($attributes['tag']);
return html_tag($tag, $this->attr_to_string($attributes));
} public function prep_value($value)
{
$value = \Security::htmlentities($value, ENT_QUOTES);
return $value;
}
ControllerからViewに渡す値はset_safe等で渡さない限り、自動的にhtmlentitiesが適用されます。
Controllerで
$data['name'] = Input::post('name');
View::forge('user/contents', $data);
Viewで
<?php echo Form::Input('name', $name); ?>
とすると、Viewにnameが渡されたときにhtmlentitiesが適用され、Form::Inputに渡されたときにまたhtmlentitiesが適用されてしまいます。
しかし、 FuelPHPのデフォルト設定は「double_encodeをしない」なので、一度HTMLエンティティに変換された文字が再度変換されることはないので、特殊なケースを除いて問題ないです。
特殊なケースとは「&」等を入力した場合です。
こちらについては、前の記事を参照ください。
※「&:」等が入力されることがないのであればいい気もするんですが、例えば「&は&に変換されます」という記事を書きたいとかいう場合は困る気がしました。