简体   繁体   中英

Passing errors to view

I'm using kohana 3.3 and kostache. Please help me on this one. How do you pass errors to a view.

Controller

public function action_add()
    {

        $renderer = Kostache_Layout::factory();
        $view = new View_Pages_Album_List();    
        try
        {

        $album = ORM::factory('Album_Information');

        $album_name = $this->request->post('inputAlbum');

        $artist = $this->request->post('inputArtist');

        $album->Album_Name = $album_name;

        $album->Artist = $artist;

        $album->save();

        }
        catch (ORM_Validation_Exception $e)
        {
            $errors = $e->errors('models');
            $view->errors = $errors;
        }

        $this->response->body($renderer->render($view));

    }

template file

<h3>Add A New Album</h3>
<form method="POST" action="album/add">

<label>Album Name:</label>
<input type="text" name="inputAlbum" /><br />

<label>Artist:</label>
<input type="text" name="inputArtist" /><br />

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

{{errors}}

Rules..

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

Messages..

<?php defined('SYSPATH') or die('No Direct Script Access');

return array(
            'not_empty' =>  ':field must not be empty',
    );

Everytime i click on the submit button i don't get any errors. What i get is Array to string conversion problem.

Stupid of me lol. Got it working.

<h3>Add A New Album</h3>
<form method="POST" action="album/add">

<label>Album Name:</label>
<input type="text" name="inputAlbum" /><br />

<label>Artist:</label>
<input type="text" name="inputArtist" /><br />

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

{{#errors}}{{inputAlbum}}{{/errors}}
{{#errors}}{{inputArtist}}{{/errors}}

I forgot that a key is needed in order for this to work. So that fixes the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM