- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
尝试使用适用于 Kohana 3.2 的 ORM 进行验证。
目前我有我的模型:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_Brand extends ORM {
protected $_has_many = array('models' => array());
protected $_rules = array(
'name' => array(
'not_empty' => NULL,
'min_length' => array(3),
'max_length' => array(20),
),
'sku' => array(
'not_empty' => NULL,
'min_length' => array(3),
'max_length' => array(6),
),
);
}
这是我的 Controller :
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Brand extends Controller_Layout {
public function action_view()
{
$brands = ORM::factory('brand')->find_all();
$this->template->title = __('Brands');
$this->template->content = View::factory('brands/view' );
$this->template->content->set('brands', $brands);
}
public function action_edit()
{
if($_POST)
{
try
{
$brand = ORM::factory('brand', $this->request->param('id'));
$brand->values($_POST);
if($brand->check())
{
$brand->update();
$brand->save();
//go to brand/views
}
}
catch (ORM_Validation_Exception $e)
{
//pass errors to brand/edit
}
}
else
{
$brand = ORM::factory('brand', $this->request->param('id'));
$this->template->title = __('Edit Brand');
$this->template->content = View::factory('brands/edit' );
$this->template->content->set('brand', $brand);
}
}
}
我什至还没有谈到错误部分。我遇到的问题是它对任何输入进行验证而不是使用模型中的规则。另外,如果有人能告诉我应该如何设计这样的更新操作,那将是一个很大的帮助。谢谢。
最佳答案
我就是这样做模型验证的,我觉得最直接也最优雅。
首先,我在 rules() 方法中设置我的规则:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_Brand extends ORM {
public function rules()
{
return array(
'name' => array(
array('not_empty'),
array('min_length', array(':value', 3)),
array('max_length', array(':value', 20)),
)
'sku' => array(
array('not_empty'),
array('min_length', array(':value', 3)),
array('max_length', array(':value', 6)),
)
);
);
}
这就是我管理编辑操作的方式:
public function action_edit()
{
$brand = ORM::factory('brand', $this->request->param('id'));
if (!$brand->loaded())
{
throw new Kohana_Exception('Brand not found.');
}
$this->template->title = __('Edit Brand');
$this->template->content = View::factory('brands/edit')
->set('brand', $brand)
->bind('errors', $errors);
if ($this->request->method() === Request::POST)
{
try
{
$brand->values($this->request->post());
$brand->save();
// Success! You probably want to set a session message here.
$this->request->redirect($this->request->uri());
}
catch(ORM_Validation_Exception $e)
{
// Fail!
$errors = $e->errors('brand');
}
}
}
在我看来:
<?php if ($errors) {?>
<!-- display errors here -->
<?php } ?>
<?php echo Form::open()?>
<fieldset>
<div class="field">
<?php echo
Form::label('name', __('Name')),
Form::input('name', $brand->name)
?>
</div>
<?php echo Form::submit('save', 'Save')); ?>
</fieldset>
<?php echo Form::close()?>
如您在 View 中所见,我没有进行任何条件检查以查看在表单字段中显示的内容,因为它由模型中的数据管理,而模型中的数据由 Controller 管理。
希望这对您有所帮助,如果您需要进一步说明,请提出。
关于php - Kohana ORM 和验证,有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9123684/
我一直在使用 Kohana 2.3.7。有一些因素阻碍我们使用 Kohana 3,其中之一是与 2.3.7 相比缺少模块。例如,我似乎找不到2.3至3中存在的支付模块(Paypal、2C0、Autho
如果我在 https://www.example.com/test?page=123 页面上,我如何使用 Kohana 获得这个完整路径(proto + host + URI + query)? 我试
在我的应用程序中,我有一个执行大约 1000 次的循环,在其中我创建对象并保存它。这是应用程序的一部分,我用数据填充数据库。一般来说,这看起来像这样: foreach(...){ ...
如果我在 https://www.example.com/test?page=123 页面上,我如何使用 Kohana 获得这个完整路径(proto + host + URI + query)? 我试
我正在使用 ORM Auth 模块,但很难弄清楚如何做到这一点。我尝试过这种情况: $user = ORM::factory('user', $id);$user->roles->delete_all
好的,我试过到处搜索,但似乎无法得到一个简单直接的答案。 我想将日志消息( INFO 、 ERROR 等)写入 Kohana 日志文件 /application/logs/YYYY/MM/DD.php
我的问题是关于多词 MySQL 表名的约定,例如“comments_flags”,它不是数据透视表。 “comments_flags”的正确数据库表名是什么? 相应的型号名称是什么? 感谢您的任何建议
尝试在我的 Kohana 2.3.4 安装上运行 PHPUnit: phpunit --colors --bootstrap=index.php ../../modules/phpunit/libra
我是 kohana 的新手。我正在使用 ORM 来获取数据。 我想建立一个查询,其中 BETWEEN条款在那里。如下 SELECT `rooms`.* FROM `rooms` LEFT JOIN `
在 Kohana 3.3 的 DOCROOT 文件夹中,有一个新的 vendor 文件夹。 它有什么用? 最佳答案 它不应该在那里。这是 Composer 留下的。 关于kohana - Kohana
如何让它发挥作用? $stuff = ORM::factory('mytable') ->with('user') ->with('other_stuff') ->find_a
我注意到,当我开始使用它们时,Kohana 3 ORM 会为我的每个模型运行“显示完整列”: SHOW FULL COLUMNS FROM `mytable` 此查询可能需要几个时钟周期才能执行(在
全新安装了 Kohana 3.1。试图让我自己的 helper 。我在 application/classes/helpers/目录中创建了一个助手。我调用了文件 javascript.php,该类称
在 Kohana 2.x 中,有一个 $file = Kohana::log_directory(); 可以根据发送的参数获取或设置目录。 如何在Kohana 3.1下得到类似的效果? 我想读取日志目
使用 native ORM 在 Kohana(3.3) 中防止 SQL 注入(inject)的最佳方法是什么? 或者默认情况下它是“安全的”.. 最佳答案 默认情况下是安全的,因为它使用 Kohana
如何在 Kohana 3.2 中调用验证对象的 trim 函数?我正在使用: $post = Validation::factory($this->request->post()); $post->r
首先,我对 kohana 很陌生,但我对更流行的 Yii 和 Yii2 有丰富的经验。 我正在处理复杂的配置,它使用查询生成器从跳跃模型调用 MsSQL 数据库。现在我不得不优化代码,但遇到了一个错误
我正在尝试在 Kohana 3.3.0 中将 Auth 模块与 ORM 驱动程序一起使用,但我唯一能做的就是在数据库中插入新用户。我不能用他们登录。 我从一个空白的 Kohana 项目、一个简单的路由
我有一个简单的users表,我想找到所有用户在哪里email_notifications = 1。 逻辑规定以下应该起作用: class Controller_Test extends Control
我需要生成这样的查询: SELECT * FROM `table1` WHERE `date1` where('date1','execute(); $foo = array(); foreach (
我是一名优秀的程序员,十分优秀!