【问题标题】:Kohana 3.2, displaying errors in the formKohana 3.2,在表单中显示错误
【发布时间】:2012-01-15 04:30:57
【问题描述】:

我想在我的表单上显示错误,突出显示有错误的字段,并在字段旁边显示错误文本。如果没有优雅的方式显示在每个字段旁边,上面会很好。

我找到了早期版本的示例,但 API 似乎发生了变化,它们不适用于 3.2。

这只是我学习 Kohana 的一个项目,所以它并不重要。我只是想知道处理这个问题的“kohana”方式。

在我的控制器中,我有这个:

if (isset($_POST) && Valid::not_empty($_POST))
{
    $post = Validation::factory($_POST)
    ->rule('zipcode', 'not_empty'));

    if ($post->check()) {
        $errors = $post->errors('zipcode');
    }
}

$this->template->content = View::factory('myview', $data)
->bind('errors', $errors);

这是我在“myview.php”中的表单:

<?php echo Form::open(); ?>
<dl>
    <dt><?php echo Form::label('zipcode', 'Zip Code') ?></dt>
    <dd><?php echo Form::input('zipcode') ?></dd>
</dl>
<p><?php echo Form::submit(NULL, 'Get Records'); ?></p>
<?php echo Form::close(); ?>

【问题讨论】:

    标签: kohana kohana-3


    【解决方案1】:

    我采取了扩展 Form helper 类的方法,在表单字段上添加一个“错误”类名称,并在字段标签中显示错误消息。

    <?php defined('SYSPATH') or die('No direct script access.');
    
    class Form extends Kohana_Form {
    
        private static function attributes($name, & $attributes = NULL, $errors = NULL)
        {
            // Set the id attribute
            if (!isset($attributes['id']))
            {
                $attributes['id'] = $name;
            }
    
            if ($errors !== NULL)
            {
                // Merge in external validation errors.
                $errors = array_merge($errors, (isset($errors['_external']) ? $errors['_external'] : array()));
    
                // Set the error classname
                if (isset($errors[$name]))
                {
                    $attributes['class'] = trim( (string) @$attributes['class'].' error-field');            
                }
            }
        }
    
        public static function input($name, $value = NULL, array $attributes = NULL, array $errors = NULL)
        {
            static::attributes($name, $attributes, $errors);
    
            return parent::input($name, $value, $attributes);
        }
    
        public static function select($name, array $options = NULL, $selected = NULL, array $attributes = NULL, array $errors = NULL)
        {
            static::attributes($name, $attributes, $errors);
    
            return parent::select($name, $options, $selected, $attributes);
        }
    
        public static function password($name, $value = NULL, array $attributes = NULL, array $errors = NULL)
        {
            static::attributes($name, $attributes, $errors);
    
            return parent::password($name, $value, $attributes);
        }
    
        public static function textarea($name, $body = '', array $attributes = NULL, $double_encode = TRUE, array $errors = NULL)
        {
            static::attributes($name, $attributes, $errors);
    
            return parent::textarea($name, $body, $attributes, $double_encode);
        }
    
        public static function file($name, array $attributes = NULL, array $errors = NULL)
        {
            static::attributes($name, $attributes, $errors);
    
            return parent::file($name, $attributes);
        }
    
        public static function label($input, $text = NULL, array $attributes = NULL, array $errors = NULL, $view = 'messages/label_error')
        {
            if ($errors !== NULL)
            {
                // Merge in external validation errors.
                $errors = array_merge($errors, (isset($errors['_external']) ? $errors['_external'] : array()));
    
                // Use the label_error view to append an error message to the label
                if (isset($errors[$input]))
                {
                    $text .= View::factory($view)->bind('error', $errors[$input]);
                }
            }
    
            return parent::label($input, $text, $attributes);
        }    
    } 
    

    然后将$errors 数组传入标签和字段辅助方法:

    <?php echo
        Form::label('username', 'Username', NULL, $errors),
        Form::input('username', $user->username, NULL, $errors);
    ?>
    

    这个想法是在 Kohana 论坛上提出的,但我一直在努力寻找原始线程。无论如何,我发现这种方法最适合我。

    [编辑] 在此处查看此方法的示例:http://kohana3.badsyntax.co/contact(提交表单)

    【讨论】:

    • 谢谢,这就是我正在寻找的功能。不错的解决方案,应该内置。这也是一个很棒的小演示站点。那个不能下载是吗?
    • 欢迎您在这里看看:github.com/badsyntax/kohana3-examples(基本上是该演示站点背后的代码)但我不建议尝试下载并运行它,因为事情已经坏了.代码已经很老了,所以对所有东西都加点盐。在很多情况下,我现在会做不同的事情。
    • 如果可以的话,我会放弃更多的投票来支持后期绑定、良好地使用传递引用、@attributes[...] 以及 github 上一些编写良好的参考代码。非常感谢!如果您可以更新代码库以使其成为最新版本,那就太好了。
    【解决方案2】:

    这是我用于 Kohana 表单的个人实验的一些示例代码。它是联系表格的一部分。这应该适合你。

    下面的代码显示了一个联系表格。用户提交表单后,它会给出反馈(失败+错误/成功)。

    if (isset($errors) && count($errors) > 0)
    {
        echo '<ul>';
    
        foreach ($errors as $error)
        {
            echo '<li>' . $error . '</li>';
        }
    
        echo '</ul>';
    }
    // form
    echo Form::open(null);
    
        // fields
        echo Form::label('firstname') . Form::input('firstname', null, array('id' => 'firstname')) . '<br />';
        echo Form::label('email') . Form::input('email', null, array('id' => 'email')) . '<br />';
        echo Form::label('message') . Form::textarea('message', '', array('id' => 'message')) . '<br />';
    
        // submit
        echo Form::submit('submit', 'Send message');
    
    echo Form::close();
    

    在控制器中,我验证表单并将错误和成功消息分配给视图。

    public function action_index()
    {
        // make view
        $view = View::factory('pages/contact')
            ->bind('post', $post)
            ->bind('errors', $errors)
            ->bind('success', $success);
    
        // check if form is submitted
        if ($_POST)
        {
            // trim fields
            $post = array_map('trim', $_POST);
    
            $post = Validation::factory($_POST)
                ->rules('firstname', array(
                    array('not_empty'),
                    array('min_length', array(':value', '2'))
                ))
                ->rules('email', array(
                    array('not_empty'),
                    array('email')
                ))
                ->rule('message', 'not_empty');
    
            if ($post->check())
            {
                $success[] = 'Thank you for your message!';
            }
            else
            {
                $errors = $post->errors('contact');
            }
        }
    
        // view
        $this->response->body($view);
    }
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      相关资源
      最近更新 更多