gpt4 book ai didi

php - Kohana ORM 和验证,有问题

转载 作者:可可西里 更新时间:2023-11-01 01:06:45 25 4
gpt4 key购买 nike

尝试使用适用于 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/

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