Is_Unique for Codeigniter Form Validation

is_unique for codeigniter form validation

Using your code as an example, the is_unique validation rule works by looking for a field called user_name in your users database table. If the field with the same value exists it validates as false.

To make sure it runs only when the user submits a new value, you could check the posted value $this->input->post('user_name') against the value you pulled from the database to populate your form with. If they are the same, don't validate is_unique;

if($this->input->post('user_name') != $original_value) {
$is_unique = '|is_unique[users.user_name]'
} else {
$is_unique = ''
}

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);

CodeIgniter - is_unique form validation rule not working right

Did you miss this?

$this->load->library('database');

You can use this in view to check is_unique is work or not.

<?php if(validation_errors()) echo validation_errors(); ?>

Use CodeIgniter is_unique form validation with Postgres Table in a schema

you can make your own 'is_unique' function in '/system/libraries/Form_validation.php'

 public function is_unique_with_schemas($str, $field)
{
list($table, $field)=explode('_', $field);
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));

return $query->num_rows() === 0;
}

i changed delimiter . to _

you can then use these new function 'is_unique_with_schemas[schema.tableName_columnName].

p.s don't forget to set new 'error message'

Using CodeIgniter form_validation rule is_unique with more than one database loaded

I think you can use a callback function to call a specific function that does the checking:

So change this:

$userRules = "trim|required|min_length[4]|max_length[25]|xss_clean|is_unique[users.username]";

to this:

$userRules = "trim|required|min_length[4]|max_length[25]|xss_clean|callback_is_unique";

you will need to have a function to process it:

function is_unique($username){ // $username recieves the parameter automatically
//Go on with your query as bellow
$this->db2->
}

Codeigniter 4 is_unique rule validation throws not unique error when I update record

You can pass the Id of row as parameter in the is_unique rule. Like

is_unique[tasks.Title,Id,{Id}]

Hope this helps :)

UPDATE: More Detailed Explanation

Second parameter Id is database field name. Third one is the Id that is passed from the form. For this purpose add a hidden field in the edit form then set its name = Id and its value=$data['Id']. Where $data['Id'] is the Id of the row fetched from the Database and passed to the view. So when the form will be submitted and Id will be submitted in $_POST. Which is then passed to rule parameter as:
{Id}

HOPE THIS HELPS :(

CodeIgniter + hmvc form_validation rule is_unique not applied

system/libraries/Form_validation.php
in the 1127 row

change isset() to is_object()

public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
return is_object($this->CI->db)
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}

Codeigniter: Form validation is_unique error not showing

I changed my controller to the following:

$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');
$this->form_validation->set_rules('userPassword','Password', 'required|trim|max_length[99]|xss_clean');

if($this->form_validation->run() === TRUE)
{
$userData = array(
'fName' => $this->input->post('userFirstName', TRUE),
'lName' => $this->input->post('userLastName', TRUE),
'email' => $this->input->post('userEmail', TRUE),
'password' => sha1($this->input->post('userPassword', TRUE))
);

$this->db->escape($userData);
$this->user_model->addUser($userData);
}

$data['contentMangement'] = $this->options_model->systemOptions();
$data['pageTitle'] = 'Add User';
$this->load->view('_assets/header', $data);
$this->load->view('addUser', $data);
$this->load->view('_assets/footer');
}

Can't Show custom error message for is_unique in codeigniter

Please check this :

$this->form_validation->set_rules(
'email', 'Email',
'required|is_unique[users.email]',
array(
'is_unique' => 'A User is already registered with this E-mail : %s'
)
);


Related Topics



Leave a reply



Submit