src/App/Controllers/products.php | ●●●●● patch | view | raw | blame | history | |
src/Framework/Model.php | ●●●●● patch | view | raw | blame | history |
src/App/Controllers/products.php
@@ -69,4 +69,32 @@ ]); }; } public function update(string $id) { $product = $this->model->find($id); if( $product == false ) { throw new PageNotFoundException("Product not found"); } $data = [ "name" => $_POST["name"], "description" => empty($_POST["description"]) ? null : $_POST["description"] ]; if($this->model->update($id, $data)) { header("Location: /products/{$id}/show"); exit; } else { echo $this->viewer->render("shared/header.php", [ "title" => "Edit Product" ]); echo $this->viewer->render("Products/edit.php", [ "errors" => $this->model->getErrors(), "product"=> $product ]); }; } } src/Framework/Model.php
@@ -13,6 +13,16 @@ public function __construct(private Database $database) { } public function update(string $id, array $data): bool { $this->validate($data); if(!empty($this->errors)) { return false; } return true; } protected function addError(string $field, string $message): void { $this->errors[$field] = $message;