gpt4 book ai didi

php - 小花 3 : Example of model with validation

转载 作者:IT王子 更新时间:2023-10-29 01:04:11 25 4
gpt4 key购买 nike

我找到了有关模型和验证的示例和教程。我说验证(或至少大部分验证)应该在模型中,我同意这一点。但是我无法提供任何示例或教程来说明应该如何完成。

谁能帮我举一个简单的例子来说明如何做到这一点?模型中的规则在哪里?验证将在哪里进行? Controller 如何知道验证是通过还是失败? Controller 如何获得错误消息和类似信息?

希望有人能帮忙,因为在这里感觉有点迷茫:p

最佳答案

我也很难找到 Kohana3 的示例,bestattendance 的示例是针对 Kohana2 的。

这是我在自己的测试中拼凑的一个例子:

应用/类/模型/news.php

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

Class Model_News extends Model
{
/*
CREATE TABLE `news_example` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`title` VARCHAR(30) NOT NULL,
`post` TEXT NOT NULL);
*/

public function get_latest_news() {
$sql = 'SELECT * FROM `news_example` ORDER BY `id` DESC LIMIT 0, 10';
return $this->_db->query(Database::SELECT, $sql, FALSE)
->as_array();
}

public function validate_news($arr) {
return Validate::factory($arr)
->filter(TRUE, 'trim')
->rule('title', 'not_empty')
->rule('post', 'not_empty');
}
public function add_news($d) {
// Create a new user record in the database
$insert_id = DB::insert('news_example', array('title','post'))
->values(array($d['title'],$d['post']))
->execute();

return $insert_id;
}
}

application/messages/errors.php

<?php
return array(
'title' => array(
'not_empty' => 'Title can\'t be blank.',
),
'post' => array(
'not_empty' => 'Post can\'t be blank.',
),
);

应用程序/类/ Controller /news.php

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

Class Controller_News extends Controller
{
public function action_index() {
//setup the model and view
$news = Model::factory('news');
$view = View::factory('news')
->bind('validator', $validator)
->bind('errors', $errors)
->bind('recent_posts', $recent_posts);

if (Request::$method == "POST") {
//added the arr::extract() method here to pull the keys that we want
//to stop the user from adding their own post data
$validator = $news->validate_news(arr::extract($_POST,array('title','post')));
if ($validator->check()) {
//validation passed, add to the db
$news->add_news($validator);
//clearing so it won't populate the form
$validator = null;
} else {
//validation failed, get errors
$errors = $validator->errors('errors');
}
}
$recent_posts = $news->get_latest_news();
$this->request->response = $view;
}
}

application/views/news.php

<?php if ($errors): ?>
<p>Errors:</p>
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>

<?php echo Form::open() ?>
<dl>
<dt><?php echo Form::label('title', 'title') ?></dt>
<dd><?php echo Form::input('title', $validator['title']) ?></dd>
<dt><?php echo Form::label('post', 'post') ?></dt>
<dd><?php echo Form::input('post', $validator['post']) ?></dd>
</dl>
<?php echo Form::submit(NULL, 'Post') ?>
<?php echo Form::close() ?>
<?php if ($recent_posts): ?>
<ul>
<?php foreach ($recent_posts as $post): ?>
<li><?php echo $post['title'] . ' - ' . $post['post'];?></li>
<?php endforeach ?>
</ul>
<?php endif ?>

要使此代码在默认安装中运行,您必须启用数据库模块并将其配置为进行身份验证。然后您可以使用默认配置从 index.php/news 访问它。

它在 Kohana 3.0.7 中进行了测试,应该为您如何布局代码提供一个良好的起点。与其他框架不同,Kohana 似乎对您将逻辑放在哪里非常开放,所以这对我来说才有意义。如果你想使用 ORM 而不是滚动你自己的数据库交互,它有自己的验证语法,你可以找到 here

关于php - 小花 3 : Example of model with validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2462201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com