[eluser]juliano.ma[/eluser]
Hello,
I am trying to implement this validation in real time on my form, but I must be missing something.
I have installed the JSON.
You could post an full example in the controller and view, please?
Code:
<?php
class Form extends Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('validation');
$rules['username'] = "callback_chk_username";
$this->validation->set_rules($rules);
if ($this->validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
function chk_username($name)
{
$b = true;
$this->db->from('user');
$this->db->where('username', $name);
$query = $this->db->get();
if($query->num_rows > 0)
{
$this->validation->set_message('chk_username', 'Not available');
$b = false;
}
return $b;
}
}
?>
Where i use this below, in controller or view?:
Code:
$json = array();
$json['validation'] = validation_errors();
echo json_enchode($json);
Look my view:
Code:
<?=$this->validation->error_string; ?>
<?=form_open('register');?>
<?
$dados = array(
'class' => 'username',
'name' => 'username',
'id' => 'username',
'value' => '',
'size' => '20',
);
echo form_input($dados);
?>
<?
$dados_submit = array(
'type' => 'submit',
'name' => 'submit_user',
'value' => 'Send',
'id' => 'submit',
);
echo form_submit($dados_submit);
?>