From 7a6d8eb8b3fdf27ff6e8c0e211cc9dd42c2c9ac4 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 25 Jan 2010 22:26:08 -0800 Subject: [PATCH] Updated for model based validation --- .../controllers/admin_postage_bands.php | 63 +++++++++---------- .../controllers/admin_product_lines.php | 61 ++++++++---------- modules/basket/helpers/basket_installer.php | 31 +++++++-- modules/basket/helpers/postage_band.php | 24 ------- modules/basket/helpers/product.php | 25 -------- modules/basket/models/item_product.php | 2 +- modules/basket/models/postage_band.php | 29 ++++++++- modules/basket/models/product.php | 31 +++++++-- 8 files changed, 134 insertions(+), 132 deletions(-) diff --git a/modules/basket/controllers/admin_postage_bands.php b/modules/basket/controllers/admin_postage_bands.php index 22bf4b3d..741153df 100644 --- a/modules/basket/controllers/admin_postage_bands.php +++ b/modules/basket/controllers/admin_postage_bands.php @@ -41,28 +41,28 @@ class Admin_Postage_Bands_Controller extends Controller access::verify_csrf(); $form = postage_band::get_add_form_admin(); - $valid = $form->validate(); - $name = $form->add_postage->inputs["name"]->value; - $postage = ORM::factory("postage_band")->where("name", "=", $name)->find(); - if ($postage->loaded()) { - $form->add_postage->inputs["name"]->add_error("in_use", 1); + try { + $valid = $form->validate(); + $postage_band = ORM::factory("postage_band"); + $postage_band->name = $form->add_postage->inputs["name"]->value; + $postage_band->flat_rate = $form->add_postage->flat_rate->value; + $postage_band->per_item = $form->add_postage->per_item->value; + $postage_band->validate(); + } catch (ORM_Validation_Exception $e) { + // Translate ORM validation errors into form error messages + foreach ($e->validation->errors() as $key => $error) { + $form->add_postage->inputs[$key]->add_error($error, 1); + } $valid = false; } if ($valid) { - $postage = postage_band::create( - $name, - $form->add_postage->flat_rate->value, - $form->add_postage->per_item->value - ); - - $postage->save(); + $postage_band->save(); message::success(t("Created postage band %postage_name", array( - "postage_name" => html::clean($postage->name)))); + "postage_name" => html::clean($postage_band->name)))); print json_encode(array("result" => "success")); } else { - print json_encode(array("result" => "error", - "form" => $form->__toString())); + print json_encode(array("result" => "error", "form" => (string)$form)); } } @@ -91,8 +91,7 @@ class Admin_Postage_Bands_Controller extends Controller $name = $postage->name; $postage->delete(); } else { - print json_encode(array("result" => "error", - "form" => $form->__toString())); + print json_encode(array("result" => "error", "form" => (string)$form)); } $message = t("Deleted user %postage_band", array("postage_band" => html::clean($name))); @@ -110,33 +109,27 @@ class Admin_Postage_Bands_Controller extends Controller } $form = postage_band::get_edit_form_admin($postage); - $valid = $form->validate(); - if ($valid) { - $new_name = $form->edit_postage->inputs["name"]->value; - if ($new_name != $postage->name && - ORM::factory("postage_band") - ->where("name", "=", $new_name) - ->where("id", "<>", $postage->id) - ->find() - ->loaded()) { - $form->edit_postage->inputs["name"]->add_error("in_use", 1); - $valid = false; - } else { - $postage->name = $new_name; + try { + $valid = $form->validate(); + $postage->name = $form->edit_postage->inputs["name"]->value; + $postage->flat_rate = $form->edit_postage->flat_rate->value; + $postage->per_item = $form->edit_postage->per_item->value; + $postage->validate(); + } catch (ORM_Validation_Exception $e) { + // Translate ORM validation errors into form error messages + foreach ($e->validation->errors() as $key => $error) { + $form->edit_postage->inputs[$key]->add_error($error, 1); } + $valid = false; } if ($valid) { - $postage->flat_rate = $form->edit_postage->flat_rate->value; - $postage->per_item = $form->edit_postage->per_item->value; $postage->save(); - message::success(t("Changed postage band %postage_name", array("postage_name" => html::clean($postage->name)))); print json_encode(array("result" => "success")); } else { - print json_encode(array("result" => "error", - "form" => $form->__toString())); + print json_encode(array("result" => "error", "form" => (string)$form)); } } diff --git a/modules/basket/controllers/admin_product_lines.php b/modules/basket/controllers/admin_product_lines.php index 2ddb1af2..2f19e1bd 100644 --- a/modules/basket/controllers/admin_product_lines.php +++ b/modules/basket/controllers/admin_product_lines.php @@ -41,29 +41,28 @@ class Admin_Product_Lines_Controller extends Controller access::verify_csrf(); $form = product::get_add_form_admin(); - $valid = $form->validate(); - $name = $form->add_product->inputs["name"]->value; - $product = ORM::factory("product")->where("name", "=", $name)->find(); - if ($product->loaded()) { - $form->add_product->inputs["name"]->add_error("in_use", 1); + try { + $valid = $form->validate(); + $product = ORM::factory("product"); + $product->name = $form->add_product->inputs["name"]->value; + $product->description = $form->add_product->description->value; + $product->postage_band_id = $form->add_product->postage_band->value; + $product->validate(); + } catch (ORM_Validation_Exception $e) { + // Translate ORM validation errors into form error messages + foreach ($e->validation->errors() as $key => $error) { + $form->add_product->inputs[$key]->add_error($error, 1); + } $valid = false; } if ($valid) { - $product = product::create( - $name, - $form->add_product->cost->value, - $form->add_product->description->value, - $form->add_product->postage_band->value - ); - $product->save(); message::success(t("Created product %product_name", array( "product_name" => html::clean($product->name)))); print json_encode(array("result" => "success")); } else { - print json_encode(array("result" => "error", - "form" => $form->__toString())); + print json_encode(array("result" => "error", "form" => (string)$form)); } } @@ -111,34 +110,28 @@ class Admin_Product_Lines_Controller extends Controller } $form = product::get_edit_form_admin($product); - $valid = $form->validate(); - if ($valid) { - $new_name = $form->edit_product->inputs["name"]->value; - if ($new_name != $product->name && - ORM::factory("product") - ->where("name", "=", $new_name) - ->where("id", "<>", $product->id) - ->find() - ->loaded()) { - $form->edit_product->inputs["name"]->add_error("in_use", 1); - $valid = false; - } else { - $product->name = $new_name; - } - } - - if ($valid) { + try { + $valid = $form->validate(); + $product->name = $form->edit_product->inputs["name"]->value; $product->cost = $form->edit_product->cost->value; $product->description = $form->edit_product->description->value; $product->postage_band_id = $form->edit_product->postage_band->value; - $product->save(); + $product->validate(); + } catch (ORM_Validation_Exception $e) { + // Translate ORM validation errors into form error messages + foreach ($e->validation->errors() as $key => $error) { + $form->edit_product->inputs[$key]->add_error($error, 1); + } + $valid = false; + } + if ($valid) { + $product->save(); message::success(t("Changed product %product_name", array("product_name" => html::clean($product->name)))); print json_encode(array("result" => "success")); } else { - print json_encode(array("result" => "error", - "form" => $form->__toString())); + print json_encode(array("result" => "error", "form" => (string)$form)); } } diff --git a/modules/basket/helpers/basket_installer.php b/modules/basket/helpers/basket_installer.php index 48c58d4b..1aa6d8bd 100644 --- a/modules/basket/helpers/basket_installer.php +++ b/modules/basket/helpers/basket_installer.php @@ -56,15 +56,32 @@ class basket_installer PRIMARY KEY (`id`)) DEFAULT CHARSET=utf8;"); - postage_band::create("No Postage",0,0); + $postage_band = ORM::factory("postage_band"); + $postage_band->name = "No Postage"; + $postage_band->save(); - product::create("4x6",5,"4\"x6\" print",1); - product::create("8x10",25,"8\"x10\" print",1); - product::create("8x12",30,"8\"x12\" print",1); + $product = ORM::factory("product"); + $product->name = "4x6"; + $product->cost = 5; + $product->description = "4\"x6\" print"; + $product->postage_band_id = 1; + $product->save(); + $product = ORM::factory("product"); + $product->name = "8x10"; + $product->cost = 25; + $product->description = "8\"x10\" print"; + $product->postage_band_id = 1; + $product->save(); + + $product = ORM::factory("product"); + $product->name = "8x12"; + $product->cost = 30; + $product->description = "8\"x12\" print"; + $product->postage_band_id = 1; + $product->save(); module::set_version("basket", 2); - } static function upgrade($version) { @@ -84,7 +101,9 @@ class basket_installer `per_item` DECIMAL(10,2) default 0, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); - postage_band::create("No Postage",0,0); + $postage_band = ORM::factory("postage_band"); + $postage_band->name = "No Postage"; + $postage_band->save(); module::set_version("basket", $version = 2); } diff --git a/modules/basket/helpers/postage_band.php b/modules/basket/helpers/postage_band.php index 4a59d4a4..0f70c4c8 100644 --- a/modules/basket/helpers/postage_band.php +++ b/modules/basket/helpers/postage_band.php @@ -28,7 +28,6 @@ class postage_band_Core { $group->input("per_item")->label(t("Per Item"))->id("gPetItem"); $group->submit("")->value(t("Add Postage Band")); $postage = ORM::factory("postage_band"); - $form->add_rules_from($postage); return $form; } @@ -44,7 +43,6 @@ class postage_band_Core { value($postage->per_item); $group->submit("")->value(t("Modify Postage Band")); - $form->add_rules_from($postage); return $form; } @@ -58,28 +56,6 @@ class postage_band_Core { return $form; } - /** - * Create a new postage band - * - * @param string $name - * @param string $full_name - * @param string $password - * @return User_Model - */ - static function create($name, $flatrate, $peritemcost) { - $postage = ORM::factory("postage_band")->where("name", "=", $name)->find(); - if ($postage->loaded()) { - throw new Exception("@todo postage already EXISTS $name"); - } - - $postage->name = $name; - $postage->flat_rate = $flatrate; - $postage->per_item = $peritemcost; - - $postage->save(); - return $postage; - } - /** * returns the array of postage bands * @return an array of postage bands diff --git a/modules/basket/helpers/product.php b/modules/basket/helpers/product.php index e02cd70c..32fe75c0 100644 --- a/modules/basket/helpers/product.php +++ b/modules/basket/helpers/product.php @@ -30,8 +30,6 @@ class product_Core { ->label(t("Postage Band")) ->options(postage_band::getPostageArray()); $group->submit("")->value(t("Add Product")); - $product = ORM::factory("product"); - $form->add_rules_from($product); return $form; } @@ -51,7 +49,6 @@ class product_Core { ->selected($product->postage_band_id); $group->submit("")->value(t("Modify Product")); - $form->add_rules_from($product); return $form; } @@ -65,28 +62,6 @@ class product_Core { return $form; } - /** - * Create a new product - * - * @param string $name - * @param string $full_name - * @param string $password - * @return User_Model - */ - static function create($name, $cost, $description, $postage_band) { - $product = ORM::factory("product")->where("name", "=", $name)->find(); - if ($product->loaded()) { - throw new Exception("@todo USER_ALREADY_EXISTS $name"); - } - - $product->name = $name; - $product->cost = $cost; - $product->description = $description; - $product->postage_band_id = $postage_band; - $product->save(); - return $product; - } - static function getProductArray($id){ $producta = array(); // check for product override diff --git a/modules/basket/models/item_product.php b/modules/basket/models/item_product.php index e152ab19..359a18db 100644 --- a/modules/basket/models/item_product.php +++ b/modules/basket/models/item_product.php @@ -1,4 +1,4 @@ - "length[1,32]"); + protected $has_many = array("products"); - protected $has_many=array('products'); + /** + * Specify our rules here so that we have access to the instance of this model. + */ + public function validate($array=null) { + if (!$array) { + $this->rules = array( + "name" => array("rules" => array("required", "length[1,32]"), + "callbacks" => array(array($this, "valid_name"))), + "flat_rate" => array("rules" => array("required", "decimal")), + "per_item" => array("rules" => array("required"))); + } + + parent::validate($array); + } + + /** + * Validate the item name. It can't conflict with other names, can't contain slashes or + * trailing periods. + */ + public function valid_name(Validation $v, $field) { + $postage_band = ORM::factory("postage_band")->where("name", "=", $this->name)->find(); + if ($postage_band->loaded() && $postage_band->id != $this->id) { + $v->add_error("name", "in_use"); + } + } } diff --git a/modules/basket/models/product.php b/modules/basket/models/product.php index 5c1ff1dd..d68867c2 100644 --- a/modules/basket/models/product.php +++ b/modules/basket/models/product.php @@ -18,9 +18,32 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Product_Model extends ORM { - var $form_rules = array( - "name" => "length[1,32]", - "description" => "length[0,255]"); - protected $belongs_to=array('postage_band'); + protected $belongs_to = array("postage_band"); + /** + * Specify our rules here so that we have access to the instance of this model. + */ + public function validate($array=null) { + if (!$array) { + $this->rules = array( + "name" => array("rules" => array("required", "length[1,32]"), + "callbacks" => array(array($this, "valid_name"))), + "description" => array("rules" => array("required", "length[0,255]")), + "cost" => array("rules" => array("required", "decimal"))); + } + + parent::validate($array); + } + + /** + * Validate the item name. It can't conflict with other names, can't contain slashes or + * trailing periods. + */ + public function valid_name(Validation $v, $field) { + Kohana_Log::add("error",print_r("valid_name!",1)); + $product = ORM::factory("product")->where("name", "=", $this->name)->find(); + if ($product->loaded() && $product->id != $this->id) { + $v->add_error("name", "in_use"); + } + } }