D
D
Dmitry Cherednichenko2014-07-08 21:30:34
Kohana
Dmitry Cherednichenko, 2014-07-08 21:30:34

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');
    }

Tried like this:
if(isset($clientName)) {
     ORM::factory('order')
           ->values($_POST)
         ->save();
} 
else {
 
 $error = TRUE;	
}

And put in the view:
<?php if(isset($error)) { ?>
<?php echo 'error'; ?>
<?php } ?>

That doesn't work..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2014-07-09
@rdifb0

Guide to help you kohanaframework.org/3.1/guide/orm/validation

I
Ilya Lesnykh, 2014-07-09
@Aliance

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 question

Ask a Question

731 491 924 answers to any question