English 中文(简体)
Kohana 3.2, 显示形式错误
原标题:Kohana 3.2, displaying errors in the form

我愿以我的形式显示错误,强调有错误的领域,并在实地旁展示错误案文。 如果无法在每一领域旁展示,则以上将被罚款。

我从以前的版本中发现了例子,但APIC似乎有所变化,没有达到3.2。

它只是一个I m学习Kohaana的项目,因此并不重要。 我只想知道处理这一问题的“Khana”方式。

在我的控制者中,我有:

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);

我在这里讲的是我的看法。 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(); ?>
最佳回答

我采取了扩大形式助手类别的做法,在形式领域添加错误的类别名称,并显示外地标签上的错误信息。

<?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);
    }    
} 

You then pass in the $errors array into the label and field helper methods:

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

在科加纳论坛提出了这一想法,但我竭力寻找最初的线索。 不管怎么说,我发现这一办法对我来说是最好的。

[edit] View an example of this approach in action here: http://kohana3.badsyntax.co/contact (submit the form)

问题回答

This is some sample code I have used for a personal experiment with Kohana forms. It is part of a contact form. This should work for you.

下面的代码显示接触表。 在用户提交表格后,该表格提供了反馈(没有+错误/成功)。

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);
}

我希望这一帮助!





相关问题
Kohana - subfolders within views folder

I m working on the admin section of a site using Kohana. I ve created a "admin" subfolder within the views folder to store admin views. I m also using a modified instance of the Template Controller ...

approach for "site down for maintenance"

I have been using Joomla and I love its administrative facility to put the site down for maintenance. As I have seen, all requests to the site if it is in maintenance mode is routed to a single page. ...

Kohana 3: using maintainable routes

I m using Kohana v3 for a web project, and today I found myself writing this: echo Html::anchor( user/view/ .$user->id, "See user s profile"); If I rename the action_view method in the User ...

Is using a Mail Model in MVC incorrect?

I have built a model in some of my MVC websites to assist with sending emails, generally I do something like this $mail = new Mail_Model; $mail->to( me@somewhere.com ); $mail->from( you@...

help on building a basic php search engine

i looked for tutorials everywhere but just can t seem to get a good one... a search page with pagination, column header sorting, and multiple filtering(filters are in checkboxes) the problem: had ...

What s the proper MVC way to do this....?

Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I m actually using Kohana). I m new to MVC and don t want to get this wrong... so I m wondering if i have ...

Kohana Error... Attempt to assign property of non-object

So I m trying to go through the Version 3 Guide of Kohana and keep getting an error on the hello world create view part. ErrorException [ Warning ]: Attempt to assign property of non-object Line 8: $...

热门标签