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.