Answer the question
In order to leave comments, you need to log in
How to display an error message if a field is empty in Kohana?
Here is the controller code:
public function action_product() {
$id = (int) Request::initial()->param('id');
$product = ORM::factory('product', $id);
$content = View::factory('prodView')
->bind('product', $product);
$this->template->title = $product->title;
$this->template->content = $content;
if(isset($_POST['Submit'])) {
$clientName = Arr::get($_POST, 'Name', '');
$clientPhone = Arr::get($_POST, 'Phone', '');
$clientAdress = Arr::get($_POST, 'Adress', '');
$orderNumber = Arr::get($_POST, 'Number', '');
$_POST = array(
'name' => $product->name,
'number' => $orderNumber,
'url' => $product->url,
'client_name' => $clientName,
'client_phone' => $clientPhone,
'client_adress' => $clientAdress
);
ORM::factory('order')
->values($_POST)
->save();
Controller::redirect('main/ordered');
}
if(isset($clientName)) {
ORM::factory('order')
->values($_POST)
->save();
}
else {
$error = TRUE;
}
<?php if(isset($error)) { ?>
<?php echo 'error'; ?>
<?php } ?>
Answer the question
In order to leave comments, you need to log in
1. You first access the keys with a capital letter, and then redefine the same keys, but in lower case. Is it on purpose or by accident? (keys are case-sensitive)
2. Why do you need to redefine the $_POST array, it's better to assume that it is readonly.
3. In the view, you are accessing the variable simply as a global one, shouldn't it be accessed as part of the current object ($this->VAR_NAME)?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question