TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Problem with ORM and errors
  • What is the correct way to handle errors with Kohana 3.0.12 ?

    In the controller I have this :

            $content = View::factory('mgmt/activity/activity');
            $this->template->content = $content
                ->bind('importance',$importance);
    
            $activity = ORM::factory('activity');
    
            if($_POST){
                if (isset($_POST['add'])) {
                    $activity->name = $_POST['name'];
                    $activity->description = $_POST['description'];
                    if($_POST['active']){
                        $activity->active = 1;
                    } else {
                        $activity->active = 0;
                    }
                    $activity->importance = $_POST['importance'];
    
    
                    if ($activity->check()) {
                        $activity->save();
                    }
                    else {
                        $errors = $_POST->errors('mgmt/activity/add');
                    }
                }
            }
    

    I then want to show the error in a dialog :

    
    <?php if (isset($errors)): ?>
        <div id="myDialogErrors" title="<?php echo __('Errors'); ?>">
                <ul>
                <?php foreach ($errors as $error): ?>
                    <li><?php echo __($error); ?></li>
                <?php endforeach; ?>
                </ul>
        </div>
        <script type="text/javascript">
        $(function(){
            var dialogOpts = {
                modal: true,
                width: 400,
            };
            $("#myDialogErrors").dialog(dialogOpts);
        });
        </script>
    <?php endif; ?>
    
    

    And this is the error I get :

    ErrorException [ Fatal Error ]: Call to a member function errors() on a non-object
    
  • ORM save() method throws Kohana_Exception and you need to use try/catch block, like so:

    $errors = array();
    
    // Form submitted?
    if (HTTP_Request::POST == $this->request->method())
    {
        $post = $this->request->post();         
    
        try
        {
            $activity->name = $post['name'];
            $activity->description = $post['description'];
            if (isset($post['active']))
            {
                $activity->active = 1;
            } 
            else 
            {
                $activity->active = 0;
            }
            $activity->importance = $_POST['importance'];
            $activity->save();
        }
        catch (ORM_Validation_Exception $e)
        {
            $errors = $e->errors('models');
        }
    }
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion