首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kohana 3.2,在表单中显示错误

问Kohana 3.2,在表单中显示错误
EN

Stack Overflow用户
提问于 2012-01-15 12:30:58
回答 2查看 2.9K关注 0票数 0

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

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

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

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

代码语言:javascript
复制
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’中的表单:

代码语言:javascript
复制
<?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(); ?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-15 23:12:27

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

代码语言:javascript
复制
<?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数组传递到标签和字段帮助器方法中:

代码语言:javascript
复制
<?php echo
    Form::label('username', 'Username', NULL, $errors),
    Form::input('username', $user->username, NULL, $errors);
?>

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

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

票数 3
EN

Stack Overflow用户

发布于 2012-01-15 17:39:59

这是我使用Kohana表单进行个人实验的一些示例代码。它是联系人表单的一部分。这对你来说应该是可行的。

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

代码语言:javascript
复制
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();

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

代码语言:javascript
复制
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);
}

我希望这能帮到你!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8867538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档