June 8, 2012

FuelPHPのScaffold(admin)と、バリデーションエラーメッセージのエスケープ問題の対応策。最善(?)策。

2012/06/16 追記。

当問題、以下の内容で解決しました。

oil generateとバリデーションエラーメッセージの件について報告。
http://madroom-project.blogspot.jp/2012/06/oil-generate.html

--

ちょっと前に以下のエントリを書きましたが、より良いと思われる方法を思いつきました。

FuelPHPのScaffold(admin)と、バリデーションエラーメッセージのエスケープ問題の対応策。(応急処置。)
http://madroom-project.blogspot.jp/2012/06/fuelphpscaffoldadmin.html

根本的に解決するには、例えば以下のような修正が必要と思われます。
diff --git a/fuel/core/classes/validation.php b/fuel/core/classes/validation.php
index a09c30e..80abcac 100644
--- a/fuel/core/classes/validation.php
+++ b/fuel/core/classes/validation.php
@@ -565,6 +565,37 @@ class Validation
     }
     /**
+     * Get errors
+     *
+     * Returns all errors in array
+     *
+     * @param   array  uses keys message_prefix
+     * @return  array
+     */
+     public function get_errors($options = array())
+     {
+          $default = array(
+               'message_prefix' => \Config::get('validation.message_prefix', '* '),
+          );
+          $options = array_merge($default, $options);
+
+          if (empty($this->errors))
+          {
+               return array();
+          }
+
+          $array = array();
+
+          foreach($this->errors as $e)
+          {
+               $array[] = $options['message_prefix'].$e->get_message();
+          }
+
+          return $array;
+     }
+
+     /**
     * Alias for $this->fieldset->add()
     *
     * @return  Fieldset_Field
diff --git a/fuel/packages/oil/views/admin/crud/actions/create.php b/fuel/packages/oil/views/admin/crud/actions/create.php
index a28517e..3172c4a 100644
--- a/fuel/packages/oil/views/admin/crud/actions/create.php
+++ b/fuel/packages/oil/views/admin/crud/actions/create.php
@@ -22,7 +22,7 @@
               }
               else
               {
-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }
          }

diff --git a/fuel/packages/oil/views/admin/crud/actions/edit.php b/fuel/packages/oil/views/admin/crud/actions/edit.php
index 2d6bad7..3f5c33e 100644
--- a/fuel/packages/oil/views/admin/crud/actions/edit.php
+++ b/fuel/packages/oil/views/admin/crud/actions/edit.php
@@ -22,7 +22,7 @@
               }
               else
               {
-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }
          }

diff --git a/fuel/packages/oil/views/admin/orm/actions/create.php b/fuel/packages/oil/views/admin/orm/actions/create.php
index c785673..b24faa0 100644
--- a/fuel/packages/oil/views/admin/orm/actions/create.php
+++ b/fuel/packages/oil/views/admin/orm/actions/create.php
@@ -24,7 +24,7 @@
               }
               else
               {
-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }
          }

diff --git a/fuel/packages/oil/views/admin/orm/actions/edit.php b/fuel/packages/oil/views/admin/orm/actions/edit.php
index 8b5362b..7f8635f 100644
--- a/fuel/packages/oil/views/admin/orm/actions/edit.php
+++ b/fuel/packages/oil/views/admin/orm/actions/edit.php
@@ -28,7 +28,7 @@
                    $<?php echo $singular_name; ?>-><?php echo $field['name']; ?> = $val->validated('<?php echo $field['name']; ?>');
<?php endforeach; ?>

-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }
              
               $this->template->set_global('<?php echo $singular_name; ?>', $<?php echo $singular_name; ?>, false);
diff --git a/fuel/packages/oil/views/scaffolding/crud/actions/create.php b/fuel/packages/oil/views/scaffolding/crud/actions/create.php
index a28517e..3172c4a 100644
--- a/fuel/packages/oil/views/scaffolding/crud/actions/create.php
+++ b/fuel/packages/oil/views/scaffolding/crud/actions/create.php
@@ -22,7 +22,7 @@
               }
               else
               {
-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }
          }

diff --git a/fuel/packages/oil/views/scaffolding/crud/actions/edit.php b/fuel/packages/oil/views/scaffolding/crud/actions/edit.php
index d73e196..7fd5c83 100644
--- a/fuel/packages/oil/views/scaffolding/crud/actions/edit.php
+++ b/fuel/packages/oil/views/scaffolding/crud/actions/edit.php
@@ -24,7 +24,7 @@
               }
               else
               {
-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }
          }

diff --git a/fuel/packages/oil/views/scaffolding/orm/actions/create.php b/fuel/packages/oil/views/scaffolding/orm/actions/create.php
index c785673..b24faa0 100644
--- a/fuel/packages/oil/views/scaffolding/orm/actions/create.php
+++ b/fuel/packages/oil/views/scaffolding/orm/actions/create.php
@@ -24,7 +24,7 @@
               }
               else
               {
-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }
          }

diff --git a/fuel/packages/oil/views/scaffolding/orm/actions/edit.php b/fuel/packages/oil/views/scaffolding/orm/actions/edit.php
index fb2ecb5..a52c292 100644
--- a/fuel/packages/oil/views/scaffolding/orm/actions/edit.php
+++ b/fuel/packages/oil/views/scaffolding/orm/actions/edit.php
@@ -31,7 +31,7 @@
                    $<?php echo $singular_name; ?>-><?php echo $field['name']; ?> = $val->validated('<?php echo $field['name']; ?>');
<?php endforeach; ?>

-                    Session::set_flash('error', $val->show_errors());
+                    Session::set_flash('error', $val->get_errors());
               }

               $this->template->set_global('<?php echo $singular_name; ?>', $<?php echo $singular_name; ?>, false);
diff --git a/fuel/packages/oil/views/scaffolding/template.php b/fuel/packages/oil/views/scaffolding/template.php
index 610f3be..f952932 100644
--- a/fuel/packages/oil/views/scaffolding/template.php
+++ b/fuel/packages/oil/views/scaffolding/template.php
@@ -17,14 +17,14 @@
<?php if (Session::get_flash('success')): ?>
                    <div class="alert-message success">
                         <p>
-                         <?php echo implode('</p><p>', (array) Session::get_flash('success')); ?>
+                         <?php echo implode('</p><p>', e((array) Session::get_flash('success'))); ?>
                         </p>
                    </div>
<?php endif; ?>
<?php if (Session::get_flash('error')): ?>
                    <div class="alert-message error">
                         <p>
-                         <?php echo implode('</p><p>', (array) Session::get_flash('error')); ?>
+                         <?php echo implode('</p><p>', e((array) Session::get_flash('error'))); ?>
                         </p>
                    </div>
<?php endif; ?>
* fuel/packages/oil/views/scaffolding/template.phpは、admin側と処理を同じにしておいた方が良いかな。程度です。必須では無いです。

ただし、上記はcoreの修正になるので、本家にpull requestしてマージしてもらわない限り、手出ししにくいのが本音です。
app側のみで対応するには、Validationクラスを同一名で継承、置換。上記のget_errors関数のみ書きます。
細かな方法は、以下を参考にして下さい。

FuelPHPのcoreクラスを拡張してみる。
http://madroom-project.blogspot.jp/2011/12/fuelphpcore.html

加えて、以下の修正が必要です。
scaffoldされたコントローラの
Session::set_flash('error', $val->show_errors());

Session::set_flash('error', $val->get_errors());
に変更。

No comments:

Post a Comment