1
votes

How can i validate something i did not get from a form. I want to validate my variable and i want this rule to be is_uniqe() to check for duplicates.

I have tried setting a rule in the $rules array as array( 'field' => $this->characterNAME, 'rules' => 'is_unique[members.char_name]) yet no effect i tried calling the is_unique() on its own yet no effect and i tried to asign the variable to $_POST['charNAME'] = $this->characterNAME; and then pass that to set_rules() yet no effect.

How can i validate my variable ?

My code:

<?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;
}


}
1
Instead of gettin Form Validation error as for everything else i get DATABASE Duplicate Entry error. - Sterling Duchess

1 Answers

2
votes

You can validate that your form input isn't a duplicate by calling the is_unique function directly, via (example):

$this->form_validation->is_unique($email, 'users.email');

This will return a boolean true/false. True = Is Unique -- in this case

Therefore, you can put that in an if() and check it that way...

Hope this helps