如何验证我从表单中获得的内容。我想验证我的变量,我希望这个规则是is_uniqe()来检查重复项。
我已经尝试在$ rules数组中设置一个规则作为数组('field'=> $ this-> characterNAME,'rules'=>'is_unique [members.char_name])但是没有效果我尝试调用is_unique()本身没有效果,我试图将变量设为$ _POST ['charNAME'] = $ this-> characterNAME;然后将它传递给set_rules()但没有效果。
如何验证我的变量?
我的代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Registration extends CI_Controller {
var $characterNAME = "";
var $characterCORP = "";
var $characterALLY = "";
var $characterJDAT = "";
function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Registration_model', 'reg');
}
public function index()
{
$this->load->view('registration_view');
}
function insert()
{
$rules = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required|min_length[6]|max_length[250]|is_unique[members.username]'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|min_length[6]|max_length[250]|md5'
),
array(
'field' => 'apiid',
'label' => 'apiid',
'rules' => 'required|integer|min_length[6]|max_length[250]|callback_api_check[' . $this->input->post('apikey') . ']'
),
array(
'field' => 'apikey',
'label' => 'apikey',
'rules' => 'required|min_length[6]|max_length[255]'
),
);
$_POST['charNAME'] = $this->characterNAME;
$this->form_validation->set_rules('charNAME', 'CharacterName', 'is_unique[members.char_name]');
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == FALSE)
{
$this->load->view('registration_view');
} else {
// PROCESS REGISTRATION
$this->reg->add_user($_POST['username'], $_POST['password'], $_POST['apiid'], $_POST['apikey'], $this->characterNAME, $this->characterCORP, $this->characterALLY, $this->characterJDAT);
// REDIRECT
$this->load->view('registration_done');
}
}
function api_check($apiid, $apikey)
{
$url = 'http://api.eveonline.com/account/Characters.xml.aspx?keyID='.$apiid.'&vCode='.$apikey;
$xml = new DOMDocument();
$xml->load($url);
$chars = $xml->getElementsByTagName('row');
foreach ($chars as $character)
{
$charid = $character->attributes;
$curl = 'http://api.eveonline.com/eve/CharacterInfo.xml.aspx?keyID='. $apiid . '&vCode='.$apikey . '&characterID=' . $charid->item(1)->nodeValue;
$cxml = new DOMDocument();
$cxml->load($curl);
$corp = $cxml->getElementsByTagName("corporation");
$ally = $cxml->getElementsByTagName("alliance");
$char = $cxml->getElementsByTagName("characterName");
$jdat = $cxml->getElementsByTagName("corporationDate");
// Check database instead
if($this->reg->validate_entity($corp->item(0)->nodeValue) || $this->reg->validate_entity($ally->item(0)->nodeValue))
{
$this->characterNAME = $char->item(0)->nodeValue;
$this->characterCORP = $corp->item(0)->nodeValue;
$this->characterALLY = $ally->item(0)->nodeValue;
$this->characterJDAT = $jdat->item(0)->nodeValue;
return true;
}
}
$this->form_validation->set_message('api_check','None of the characters on this account are allowed to join.');
return false;
}
}
答案 0 :(得分:2)
您可以通过(示例)直接调用is_unique函数来验证表单输入是否重复:
$ this-&gt; form_validation-&gt; is_unique($ email,'users.email');
这将返回布尔值true / false。 True =是唯一的 - 在这种情况下
因此,您可以将其放在if()中并以此方式检查......
希望这有帮助