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();
|
access::verify_csrf();
|
||||||
|
|
||||||
$form = postage_band::get_add_form_admin();
|
$form = postage_band::get_add_form_admin();
|
||||||
$valid = $form->validate();
|
try {
|
||||||
$name = $form->add_postage->inputs["name"]->value;
|
$valid = $form->validate();
|
||||||
$postage = ORM::factory("postage_band")->where("name", "=", $name)->find();
|
$postage_band = ORM::factory("postage_band");
|
||||||
if ($postage->loaded()) {
|
$postage_band->name = $form->add_postage->inputs["name"]->value;
|
||||||
$form->add_postage->inputs["name"]->add_error("in_use", 1);
|
$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;
|
$valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($valid) {
|
if ($valid) {
|
||||||
$postage = postage_band::create(
|
$postage_band->save();
|
||||||
$name,
|
|
||||||
$form->add_postage->flat_rate->value,
|
|
||||||
$form->add_postage->per_item->value
|
|
||||||
);
|
|
||||||
|
|
||||||
$postage->save();
|
|
||||||
message::success(t("Created postage band %postage_name", array(
|
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"));
|
print json_encode(array("result" => "success"));
|
||||||
} else {
|
} else {
|
||||||
print json_encode(array("result" => "error",
|
print json_encode(array("result" => "error", "form" => (string)$form));
|
||||||
"form" => $form->__toString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,8 +91,7 @@ class Admin_Postage_Bands_Controller extends Controller
|
||||||
$name = $postage->name;
|
$name = $postage->name;
|
||||||
$postage->delete();
|
$postage->delete();
|
||||||
} else {
|
} else {
|
||||||
print json_encode(array("result" => "error",
|
print json_encode(array("result" => "error", "form" => (string)$form));
|
||||||
"form" => $form->__toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = t("Deleted user %postage_band", array("postage_band" => html::clean($name)));
|
$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);
|
$form = postage_band::get_edit_form_admin($postage);
|
||||||
$valid = $form->validate();
|
try {
|
||||||
if ($valid) {
|
$valid = $form->validate();
|
||||||
$new_name = $form->edit_postage->inputs["name"]->value;
|
$postage->name = $form->edit_postage->inputs["name"]->value;
|
||||||
if ($new_name != $postage->name &&
|
$postage->flat_rate = $form->edit_postage->flat_rate->value;
|
||||||
ORM::factory("postage_band")
|
$postage->per_item = $form->edit_postage->per_item->value;
|
||||||
->where("name", "=", $new_name)
|
$postage->validate();
|
||||||
->where("id", "<>", $postage->id)
|
} catch (ORM_Validation_Exception $e) {
|
||||||
->find()
|
// Translate ORM validation errors into form error messages
|
||||||
->loaded()) {
|
foreach ($e->validation->errors() as $key => $error) {
|
||||||
$form->edit_postage->inputs["name"]->add_error("in_use", 1);
|
$form->edit_postage->inputs[$key]->add_error($error, 1);
|
||||||
$valid = false;
|
|
||||||
} else {
|
|
||||||
$postage->name = $new_name;
|
|
||||||
}
|
}
|
||||||
|
$valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($valid) {
|
if ($valid) {
|
||||||
$postage->flat_rate = $form->edit_postage->flat_rate->value;
|
|
||||||
$postage->per_item = $form->edit_postage->per_item->value;
|
|
||||||
$postage->save();
|
$postage->save();
|
||||||
|
|
||||||
message::success(t("Changed postage band %postage_name",
|
message::success(t("Changed postage band %postage_name",
|
||||||
array("postage_name" => html::clean($postage->name))));
|
array("postage_name" => html::clean($postage->name))));
|
||||||
print json_encode(array("result" => "success"));
|
print json_encode(array("result" => "success"));
|
||||||
} else {
|
} else {
|
||||||
print json_encode(array("result" => "error",
|
print json_encode(array("result" => "error", "form" => (string)$form));
|
||||||
"form" => $form->__toString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,29 +41,28 @@ class Admin_Product_Lines_Controller extends Controller
|
||||||
access::verify_csrf();
|
access::verify_csrf();
|
||||||
|
|
||||||
$form = product::get_add_form_admin();
|
$form = product::get_add_form_admin();
|
||||||
$valid = $form->validate();
|
try {
|
||||||
$name = $form->add_product->inputs["name"]->value;
|
$valid = $form->validate();
|
||||||
$product = ORM::factory("product")->where("name", "=", $name)->find();
|
$product = ORM::factory("product");
|
||||||
if ($product->loaded()) {
|
$product->name = $form->add_product->inputs["name"]->value;
|
||||||
$form->add_product->inputs["name"]->add_error("in_use", 1);
|
$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;
|
$valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($valid) {
|
if ($valid) {
|
||||||
$product = product::create(
|
|
||||||
$name,
|
|
||||||
$form->add_product->cost->value,
|
|
||||||
$form->add_product->description->value,
|
|
||||||
$form->add_product->postage_band->value
|
|
||||||
);
|
|
||||||
|
|
||||||
$product->save();
|
$product->save();
|
||||||
message::success(t("Created product %product_name", array(
|
message::success(t("Created product %product_name", array(
|
||||||
"product_name" => html::clean($product->name))));
|
"product_name" => html::clean($product->name))));
|
||||||
print json_encode(array("result" => "success"));
|
print json_encode(array("result" => "success"));
|
||||||
} else {
|
} else {
|
||||||
print json_encode(array("result" => "error",
|
print json_encode(array("result" => "error", "form" => (string)$form));
|
||||||
"form" => $form->__toString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,34 +110,28 @@ class Admin_Product_Lines_Controller extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$form = product::get_edit_form_admin($product);
|
$form = product::get_edit_form_admin($product);
|
||||||
$valid = $form->validate();
|
try {
|
||||||
if ($valid) {
|
$valid = $form->validate();
|
||||||
$new_name = $form->edit_product->inputs["name"]->value;
|
$product->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->cost = $form->edit_product->cost->value;
|
$product->cost = $form->edit_product->cost->value;
|
||||||
$product->description = $form->edit_product->description->value;
|
$product->description = $form->edit_product->description->value;
|
||||||
$product->postage_band_id = $form->edit_product->postage_band->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",
|
message::success(t("Changed product %product_name",
|
||||||
array("product_name" => html::clean($product->name))));
|
array("product_name" => html::clean($product->name))));
|
||||||
print json_encode(array("result" => "success"));
|
print json_encode(array("result" => "success"));
|
||||||
} else {
|
} else {
|
||||||
print json_encode(array("result" => "error",
|
print json_encode(array("result" => "error", "form" => (string)$form));
|
||||||
"form" => $form->__toString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,15 +56,32 @@ class basket_installer
|
||||||
PRIMARY KEY (`id`))
|
PRIMARY KEY (`id`))
|
||||||
DEFAULT CHARSET=utf8;");
|
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 = ORM::factory("product");
|
||||||
product::create("8x10",25,"8\"x10\" print",1);
|
$product->name = "4x6";
|
||||||
product::create("8x12",30,"8\"x12\" print",1);
|
$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);
|
module::set_version("basket", 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function upgrade($version) {
|
static function upgrade($version) {
|
||||||
|
@ -84,7 +101,9 @@ class basket_installer
|
||||||
`per_item` DECIMAL(10,2) default 0,
|
`per_item` DECIMAL(10,2) default 0,
|
||||||
PRIMARY KEY (`id`))
|
PRIMARY KEY (`id`))
|
||||||
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
|
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);
|
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->input("per_item")->label(t("Per Item"))->id("gPetItem");
|
||||||
$group->submit("")->value(t("Add Postage Band"));
|
$group->submit("")->value(t("Add Postage Band"));
|
||||||
$postage = ORM::factory("postage_band");
|
$postage = ORM::factory("postage_band");
|
||||||
$form->add_rules_from($postage);
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +43,6 @@ class postage_band_Core {
|
||||||
value($postage->per_item);
|
value($postage->per_item);
|
||||||
|
|
||||||
$group->submit("")->value(t("Modify Postage Band"));
|
$group->submit("")->value(t("Modify Postage Band"));
|
||||||
$form->add_rules_from($postage);
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,28 +56,6 @@ class postage_band_Core {
|
||||||
return $form;
|
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
|
* returns the array of postage bands
|
||||||
* @return an array of postage bands
|
* @return an array of postage bands
|
||||||
|
|
|
@ -30,8 +30,6 @@ class product_Core {
|
||||||
->label(t("Postage Band"))
|
->label(t("Postage Band"))
|
||||||
->options(postage_band::getPostageArray());
|
->options(postage_band::getPostageArray());
|
||||||
$group->submit("")->value(t("Add Product"));
|
$group->submit("")->value(t("Add Product"));
|
||||||
$product = ORM::factory("product");
|
|
||||||
$form->add_rules_from($product);
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +49,6 @@ class product_Core {
|
||||||
->selected($product->postage_band_id);
|
->selected($product->postage_band_id);
|
||||||
|
|
||||||
$group->submit("")->value(t("Modify Product"));
|
$group->submit("")->value(t("Modify Product"));
|
||||||
$form->add_rules_from($product);
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,28 +62,6 @@ class product_Core {
|
||||||
return $form;
|
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){
|
static function getProductArray($id){
|
||||||
$producta = array();
|
$producta = array();
|
||||||
// check for product override
|
// 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
|
* Gallery - a web based photo album viewer and editor
|
||||||
* Copyright (C) 2000-2009 Bharat Mediratta
|
* Copyright (C) 2000-2009 Bharat Mediratta
|
||||||
|
|
|
@ -18,9 +18,32 @@
|
||||||
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
class Postage_Band_Model extends ORM {
|
class Postage_Band_Model extends ORM {
|
||||||
var $form_rules = array(
|
protected $has_many = array("products");
|
||||||
"name" => "length[1,32]");
|
|
||||||
|
|
||||||
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.
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
class Product_Model extends ORM {
|
class Product_Model extends ORM {
|
||||||
var $form_rules = array(
|
protected $belongs_to = array("postage_band");
|
||||||
"name" => "length[1,32]",
|
|
||||||
"description" => "length[0,255]");
|
|
||||||
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