Reputation: 71
I'm using CodeIgniter's form validation, and I've spent a lot of time trying to fix this, with no luck. I have this field:
<input type="text" name="user" id="user" length="20" placeholder="Username">
And I'm using this to validate:
$this->form_validation->set_rules('user', 'Username', 'trim|required|min_length[3]|max_length[20]|alpha_dash|is_unique[users.user]');
My db has a table users
and user
is a field in it, so I don't know what I'm doing wrong, or what the problem is. The table is empty (but I've also tried with it having records) and in phpmyadmin the "unique
" icon is selected.
I know the db connection is working fine, because if I remove that rule and enter otherwise valid data and submit the form, then the user is added to the database.
Unless is_unique uses another db configuration file that I haven't configured? I don't really know. It's kind of frustrating and I'm thinking that I may as well just drop the use of a framework...
Your help would be great! Thanks.
Upvotes: 7
Views: 1519
Reputation: 1
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);
Upvotes: 0
Reputation: 3237
I know your question is very old but I just recently encountered a similar problem with a custom form validation and after some serious debugging I found a cause and workaround.
Apparently, the CI core has a small bug: the database library isn't instanced (at least in the latest versions of CI) when calling the is_unique
form validation method, thus preventing the check from actually being performed and always returning false
as the validation result.
Here is the workaround for the form validation library (system/libraries/Form_validation.php
)
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
// add the following line
$this->CI->load->database();
return isset($this->CI->db)
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
By adding the line after the comment, you'll make sure the database library is correctly instanced for the is_unique
method and you'll get it to work. Without that line, the isset($this->CI->db)
check will always return false
You could also put that line in the library's constructor, but then you'd be instancing the database library in all form validation rules which is not necessary (only is_unique
needs it).
Upvotes: 0
Reputation: 146
try this :-
$this->form_validation->set_rules('user', 'Username', 'trim|required|min_length[3]|max_length[20]|alpha_dash|unique[users.user]');
Upvotes: 1
Reputation: 293
In Transact-SQL the word "USER" is a special word. Try surrounding uses.user with a back-ticks like so:
`users.user`
...see if that helps.
Upvotes: 1
Reputation: 1679
This might/might not help: You seem to be loading the DB after running the form validation. There's also a typo uses.user
.
Upvotes: 1