Question

I'm having problem saving data to the database since catch exception is always being called. try always get ignored. I don't really know what's happening. I've been working for this for hours and I can't get it to work. I'm using kohana 3.3 and kostache.

So here's the controller.

Controller

APPATH/classes/controller/album.php

public function action_create()
    {
                $view = Kostache_Layout::factory();
                $layout = new View_Pages_Album_List();
                $album = ORM::factory('Album_Information');
                $album_name = $this->request->post('inputAlbum');           
                $artist = $this->request->post('inputArtist');  
                $album->Album_Name = $album_name;
                $album->Artist = $artist;

                try
                {
                    $album->save();
                    HTTP::redirect('album');
                }
                catch(ORM_Validation_Exception $e)
                {
                    $layout->errors = $e->errors('models');
                }
            }   

        $this->response->body($view->render($layout));
    }

Templates

APPATH/templates/pages/album/list.mustache

<h3>Add A New Album</h3>
<form action="album/create" method="post">

<label for="inputAlbum">Album Name:</label>
<input id="inputAlbum" type="text" name="inputAlbum" /><br />
<label for"inputAlbum" class="error">{{#errors}}{{inputAlbum}}{{/errors}}</label>

<label for="inputArtist">Album Artist:</label>
<input id="inputArtist" type="text" name="inputArtist" /><br />
<label for="inputArtist" class="error">{{#errors}}{{inputArtist}}{{/errors}}</label>

<input type="submit" name="submit" value="Add" />
</form>

Model Rules

APPATH/classes/model/album/information.php

class Model_Album_Information extends ORM
{
    protected $_primary_key = 'ID';
    protected $_table_name = 'album_information';

    public function rules()
    {
        return array(
                'inputAlbum' => array(
                    array('not_empty'),
                    ),
                'inputArtist' => array(
                    array('not_empty'),
                    ),
        );
    }

Messages

APPATH/messages/models/album.php

return array(
        'inputAlbum' => array(
            'not_empty' =>  ':You must provide Album Name',     
            ),
        'inputArtist' => array(
            'not_empty' => ':You must provide Album Artist',
        ),
    );

The errors are showing when there's no input on the input field when i hit on the submit button, no problem with that, but even there's an input the errors are still being shown. So catch is always being called. When i remove try and catch I can easily save data to the database but there's no validation.

Thank you and more power.

Was it helpful?

Solution

You expect the ORM class to magically know the value of $album->Album_Name came from a HTTP form input named inputAlbum. It won't.

Create rules for the Album_Name and Artist properties of the ORM object itself. Not the possible input method.

The Controller knows what data to pass to models. The Model is only concerned with the data it received. Not where it came from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top