summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-11-16 07:51:42 +0000
committerBharat Mediratta <bharat@menalto.com>2008-11-16 07:51:42 +0000
commit4610fc8e7f32f1a582b5f6c9c25714151b9a2967 (patch)
treebcfecbae267d03b231eb29b7a4f829fa96af1190
parentce7fbf979d249e5e1effb7040d3e51a2c35015e3 (diff)
Create Forge::add_rules_from() which pulls validation rules from the model and
associates them with the form. This replaces the various _add_validation_rules() functions in the user and comment modules. Move user edit form into user helper for consistency with the comment module. Implement missing _form method in the user controller.
-rw-r--r--core/libraries/MY_Forge.php14
-rw-r--r--modules/comment/helpers/comment.php6
-rw-r--r--modules/comment/models/comment.php2
-rw-r--r--modules/user/controllers/user.php52
-rw-r--r--modules/user/helpers/user.php19
-rw-r--r--modules/user/models/user.php2
6 files changed, 42 insertions, 53 deletions
diff --git a/core/libraries/MY_Forge.php b/core/libraries/MY_Forge.php
index a954eb9e..0aedac86 100644
--- a/core/libraries/MY_Forge.php
+++ b/core/libraries/MY_Forge.php
@@ -3,4 +3,18 @@ class Forge extends Forge_Core {
public function render($template="form.html", $custom=false) {
return parent::render($template, $custom);
}
+
+ /**
+ * Associate validation rules defined in the model with this form.
+ */
+ public function add_rules_from($model) {
+ foreach ($this->inputs as $name => $input) {
+ if (isset($input->inputs)) {
+ $input->add_rules_from($model);
+ }
+ if (isset($model->rules[$name])) {
+ $input->rules($model->rules[$name]);
+ }
+ }
+ }
} \ No newline at end of file
diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php
index 1c876f10..8352fae4 100644
--- a/modules/comment/helpers/comment.php
+++ b/modules/comment/helpers/comment.php
@@ -61,8 +61,7 @@ class Comment_Core {
$group->input("email") ->label(_("Email")) ->id("gEmail");
$group->textarea("text")->label(_("Text")) ->id("gText");
$group->submit(_("Add"));
-
- comment::_add_validation_rules("comment", $form);
+ $form->add_rules_from(ORM::factory("comment"));
return $form;
}
@@ -73,8 +72,7 @@ class Comment_Core {
$group->input("email") ->label(_("Email")) ->id("gEmail") ->value($comment->email);
$group->textarea("text")->label(_("Text")) ->id("gText") ->value($comment->text);
$group->submit(_("Edit"));
-
- comment::_add_validation_rules("comment", $form);
+ $form->add_rules_from($comment);
return $form;
}
diff --git a/modules/comment/models/comment.php b/modules/comment/models/comment.php
index 2ed2b83c..b1eb37b7 100644
--- a/modules/comment/models/comment.php
+++ b/modules/comment/models/comment.php
@@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Comment_Model extends ORM {
- var $validation_rules = array(
+ var $rules = array(
"author" => "required",
"email" => "required|valid_email",
"text" => "required");
diff --git a/modules/user/controllers/user.php b/modules/user/controllers/user.php
index d3370dbe..c1794af3 100644
--- a/modules/user/controllers/user.php
+++ b/modules/user/controllers/user.php
@@ -21,57 +21,19 @@ class User_Controller extends REST_Controller {
protected $resource_type = "user";
/**
- * Return the form for creating / modifying users.
+ * Present a form for editing a user
+ * @see Rest_Controller::form($resource)
*/
- private function _get_form($user) {
- $form = new Forge(url::current(true), "", "post", array("id" => "gUser"));
- $group = $form->group(_("User Info"));
- $group->input("name")
- ->label(_("Name"))
- ->id("gName")
- ->class(null)
- ->value($user->name);
- $group->input("display_name")
- ->label(_("Display Name"))
- ->id("gDisplayName")
- ->class(null)
- ->value($user->display_name);
- $group->password("password")
- ->label(_("Password"))
- ->id("gPassword")
- ->class(null);
- $group->input("email")
- ->label(_("Email"))
- ->id("gEmail")
- ->class(null)
- ->value($user->email);
- $group->submit(_("Modify"));
-
- $this->_add_validation_rules(ORM::factory("user")->validation_rules, $form);
-
- return $form;
- }
-
- /**
- * @todo Refactor this into a more generic location
- */
- private function _add_validation_rules($rules, $form) {
- foreach ($form->inputs as $name => $input) {
- if (isset($input->inputs)) {
- $this->_add_validation_rules($rules, $input);
- }
- if (isset($rules[$name])) {
- $input->rules($rules[$name]);
- }
- }
+ public function _form($user) {
+ $form = user::get_edit_form($user);
+ print $form->render();
}
/**
* @see Rest_Controller::_get($resource)
*/
public function _get($user) {
- $form = $this->_get_form($user);
- print $form->render("form.html");
+ throw new Exception("@todo User_Controller::_get NOT IMPLEMENTED");
}
/**
@@ -85,7 +47,7 @@ class User_Controller extends REST_Controller {
* @see Rest_Controller::_post($resource)
*/
public function _post($user) {
- $form = $this->_get_form($user);
+ $form = user::get_edit_form($user);
if ($form->validate()) {
foreach ($form->as_array() as $key => $value) {
$user->$key = $value;
diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php
index aaf16299..8ad2709f 100644
--- a/modules/user/helpers/user.php
+++ b/modules/user/helpers/user.php
@@ -19,14 +19,29 @@
*/
/**
- * This helper provides a common around the user management functions.
- *
+ * This helper provides a common around the user management functions.
+ *
* @author Tim Almdal <public@timalmdal.com>
*
*/
class user {
/**
+ * Return the form for creating / modifying users.
+ */
+ public static function get_edit_form($user) {
+ $form = new Forge(url::site("user/{$user->id}"), "", "post", array("id" => "gUserForm"));
+ $group = $form->group(_("User Info"));
+ $group->input("name") ->label(_("Name")) ->id("gName") ->value($user->name);
+ $group->input("display_name") ->label(_("Display Name")) ->id("gDisplayName") ->value($user->display_name);
+ $group->password("password") ->label(_("Password")) ->id("gPassword");
+ $group->input("email") ->label(_("Email")) ->id("gEmail") ->value($user->email);
+ $group->submit(_("Modify"));
+ $form->add_rules_from($user);
+ return $form;
+ }
+
+ /**
* Is the password provided correct?
*
* @param user User Model
diff --git a/modules/user/models/user.php b/modules/user/models/user.php
index 1c56b34e..5c88d8dc 100644
--- a/modules/user/models/user.php
+++ b/modules/user/models/user.php
@@ -20,7 +20,7 @@
class User_Model extends ORM {
protected $has_and_belongs_to_many = array("groups");
- var $validation_rules = array(
+ var $rules = array(
"name" => "required|length[4,32]",
"email" => "valid_email",
"password" => "required|length[5,40]");