简体   繁体   English

传递错误以查看

[英]Passing errors to view

I'm using kohana 3.3 and kostache. 我正在使用kohana 3.3和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. 这样就解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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