creazione gestore di errore in caso di campi vuoti e specifica di quale campo manca (parte 127 e 128)
| | |
| | | ]; |
| | | |
| | | var_dump($this->model->insert($data)); |
| | | |
| | | print_r($this->model->getErrors()); |
| | | } |
| | | } |
| | |
| | | use PDO; |
| | | use Framework\Model; |
| | | class Product extends Model { |
| | | protected ?string $table = "product"; |
| | | //protected ?string $table = "product"; |
| | | protected array $errors = []; |
| | | |
| | | protected function addError(string $field, string $message): void { |
| | | $this->errors[$field] = $message; |
| | | } |
| | | protected function validate(array $data): bool { |
| | | |
| | | if(empty($data["name"])) { |
| | | $this->addError("name","Name is required"); |
| | | } |
| | | return empty($this->errors); |
| | | } |
| | | |
| | | public function getErrors(): array { |
| | | return $this->errors; |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | public function insert(array $data) : bool { |
| | | if( ! $this->validate($data)) { |
| | | return false; |
| | | } |
| | | $columns = implode(", " , array_keys($data)); |
| | | $placeholders = implode(", ", array_fill(0, count($data), "?")); |
| | | |