is_unique
is the form validation which not allow the value exist in the database.
But, can I do the opposite one? for example, I would like to need the value which is exist
in the database, so, I make the rules like this:
$this->form_validation->set_rules('email', 'Email', 'required|max_length[32]|valid_email|(!(is_unique[users.email]))');
But it seems not success as I expected, any recommends? Thanks.
CodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();
In addition, it has an error logging class that permits error and debugging messages to be saved as text files. By default, CodeIgniter displays all PHP errors. You might wish to change this behavior once your development is complete. You'll find the error_reporting() function located at the top of your main index.
The set_value function just sets the value, the only benefit to using it is it simplifies setting the value to an already submitted value when redisplaying the form or showing a default value when the form has yet to be submitted.
There is an inbuilt library available to perform Form Validation in CodeIgniter. With the help of this form validation library, you can validate any form very easily. Let’s know what “form validation” library in CodeIgniter is and how to validate a form in CodeIgniter using the form_validation library.
Just click on the following link to easily Download the Codeigniter 4 application. To easily debug the application, we will enable the error reporting in our Codeigniter app. Open app/Config/Boot/development.php file and set the display_errors to 1 instead of 0. Do the same thing in app/Config/Boot/production.php file.
If there are no messages it returns an empty string. The controller (Form.php) has one method: index (). This method initializes the validation class and loads the form helper and URL helper used by your view files. It also runs the validation routine. Based on whether the validation was successful it either presents the form or the success page.
To set validation rules you will use the set_rules () method: The above method takes three parameters as input: The field name - the exact name you’ve given the form field. A “human” name for this field, which will be inserted into the error message. For example, if your field is named “user” you might give it a human name of “Username”.
You can do it like this
$this->form_validation->set_rules('email', 'Email', 'required|max_length[32]|valid_email|callback_email_available');
Controller method
public function email_available($str)
{
// You can access $_POST variable
$this->load->model('mymodel');
$result = $this->mymodel->emailAvailability($_POST['email']);
if ($result)
{
$this->form_validation->set_message('email_available', 'The %s already exists');
return FALSE;
}else{
return TRUE;
}
}
Model mymodel.php
public function emailAvailability($email)
{
$this->db->where('email',$email);
$query = $this->db->get('tablename');
return $query->row();
}
Extend the Form_validation library by adding a class like MY_Form_validation.php
in your /application/core
folder (assuming your subclass prefix is MY
, which it is with default settings. This is a quick example, which just inverts the existing is_unique
function by changing the last line from return $query->num_rows() === 0;
.
class MY_Form_validation extends Form_validation
{
/**
* Match one field to others
*
* @access public
* @param string
* @param field
* @return bool
*/
public function is_not_unique($str, $field)
{
list($table, $field)=explode('.', $field);
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
return $query->num_rows() > 0;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With