Updated for model based validation
This commit is contained in:
parent
04b8238a44
commit
7a6d8eb8b3
@ -41,28 +41,28 @@ class Admin_Postage_Bands_Controller extends Controller
|
||||
access::verify_csrf();
|
||||
|
||||
$form = postage_band::get_add_form_admin();
|
||||
try {
|
||||
$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);
|
||||
$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);
|
||||
try {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
if ($valid) {
|
||||
$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->save();
|
||||
$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->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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,29 +41,28 @@ class Admin_Product_Lines_Controller extends Controller
|
||||
access::verify_csrf();
|
||||
|
||||
$form = product::get_add_form_admin();
|
||||
try {
|
||||
$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);
|
||||
$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);
|
||||
try {
|
||||
$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) {
|
||||
$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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?php defined("SYSPATH") or die("No direct script access.");
|
||||
d<?php defined("SYSPATH") or die("No direct script access.");
|
||||
/**
|
||||
* Gallery - a web based photo album viewer and editor
|
||||
* Copyright (C) 2000-2009 Bharat Mediratta
|
||||
|
@ -18,9 +18,32 @@
|
||||
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
class Postage_Band_Model extends ORM {
|
||||
var $form_rules = array(
|
||||
"name" => "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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user