Faced the need to validate one form field with several callback parameters. For example: user registration field - it is necessary to check the maximum and minimum length (this is built in) and if the user is already registered. Is it possible to implement the verification by one of the rules of a single field for several parameters using form validation in CI.

    1 answer 1

    To check whether such a user is already in the database, you can use the standard is_unique validation library Codeigniter. For example, if users are stored in the users table and the field name is username

     $this->form_validation->set_rules('username', 'Username', is_unique[users.username]); 

    The validation rules themselves can be used together by separating the characters | . For example:

      $this->form_validation->set_rules( 'username', 'Username', 'required|min_length[3]|max_length[15]|is_unique[users.username]', array( 'required' => 'Вы забыли указать %s.', 'is_unique' => 'Пользователь %s уже существует.' ) ); //Обязательное поле $this->form_validation->set_rules('password', 'Password', 'required'); //Поле повторите пароль должно совпадать с полем пароль $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]'); 

    You can transfer the rules in the form of an array:

     $this->form_validation->set_rules('username', 'Username', array('required', 'min_length[3]', 'max_length[15]','is_unique[users.username]')); 

    is_unique that is_unique works only when is_unique is enabled in the Query Builder configuration file. But you can always create your own validation rules via callback. Add a new function to the controller and call it as a rule for validation with the callback prefix:

      class Form extends CI_Controller { public function index() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'Username', 'required|min_length[3]|max_length[15]|callback_username_check'); //вызываем собственную функцию $this->form_validation->set_rules('password', 'Password', 'required'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); if ($this->form_validation->run() == FALSE) { $this->load->view('myform'); } else { $this->load->view('formsuccess'); } } //собственное правило для проверки public function username_check($str) { //что-нибудь проверяем... } } 

    You can also set your own error messages in your function. You can also work with the form data sent to the callback function and return it. If cllback returns anything other than the logical TRUE / FALSE, it is assumed that the data will be processed again. You can use methods from other places, for example:

     $this->form_validation->set_rules( 'username', 'Username', array( 'required', 'min_length[3]','max_length[15]', array($this->users_model, 'exist_user') ) ); 

    Use the exist_user() method from the object's Users_model . You can also use just anonymous functions:

     $this->form_validation->set_rules( 'username', 'Username', array( 'required', 'min_length[3]','max_length[15]', function($value) { // Проверка $value } ) ); 

    The rule being invoked is not itself a string and is not a rule. This is a problem when creating error messages for them. To solve this problem, you can place the rules in the second element of the array, where the first is the name of the rule:

     $this->form_validation->set_rules( 'username', 'Username', array( 'required', 'min_length[3]','max_length[15]', array('username_callable', array($this->users_model, 'exist_user')) ) ); 

    With anonymous function:

     $this->form_validation->set_rules( 'username', 'Username', array( 'required', 'min_length[3]','max_length[15]', array( 'username_callable', function($str) { // Проверяет валидность $str и возвращает TRUE или FALSE } ) ) ); 
    • Thank you for your reply. I looked through the built-in check is_unique. - Vakulin Oleg