これはCIフォーラムからの回答です(元のリンク)。
何らかの形式の階層を使用して、作成/編集のルールを定義できます。
<?php
$this->form_validation->set_group_rules('createModule');
$this->form_validation->set_group_rules('editModule');
if($this->form_validation->run() == FALSE) {
// whatevere you want
}
?>
または、これを行うこともできます。
<?php
// This will validate the 'accidentForm' first
$this->form_validation->set_group_rules('createModule');
if($this->form_validation->run() == FALSE) {
// whatevere you want
}
// Now we add the 'locationForm' group of rules
$this->form_validation->set_group_rules('editModule');
// And now we validate *both* sets of rules (remember that the createModule rules are still
// there), but it doesn't necessarily matter, since it will simply redo the 'createModule'
// validation while also doing the 'editModule' validation
if($this->form_validation->run() == FALSE) {
// whatevere you want
}
?>
以下は、アプリケーション ライブラリ フォルダーに MY_Form_validation.php として保存されている拡張 Form_validation クラスのコードです。
<?php
class MY_Form_validation extends CI_Form_validation {
/**
* Set Rules from a Group
*
* The default CodeIgniter Form validation class doesn't allow you to
* explicitely add rules based upon those stored in the config file. This
* function allows you to do just that.
*
* @param string $group
*/
public function set_group_rules($group = '') {
// Is there a validation rule for the particular URI being accessed?
$uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
if ($uri != '' AND isset($this->_config_rules[$uri])) {
$this->set_rules($this->_config_rules[$uri]);
return true;
}
return false;
}
}
?>