2

im using a config form_validation.php, how can i send a variable from controller to this file (form_validation.php) ?

In my form_validation.php i have:

 array(
    'field' => 'edituser_email',
    'label' => 'Email',
    'rules' => "required|trim|xss_clean|valid_email|edit_unique[users.email.$user_id]",
    'errors' => array(
             'required' => 'Campo obligatorio.',
             'valid_email' => 'Formato de correo no válido.',
             'edit_unique' => 'Ya existe un usuario con este correo.'
              )
    )

i need send "user_id" variable through Controller.

I already tried with:

$data['user_id'] = $id;
if ($this->form_validation->run('edit_user',$data) === FALSE)

But I get errors :

Message: Undefined variable: user_id.

Thanks for your help.

1

3 Answers 3

1

No need to pass arguments to $this->form_validation->run() , just set rules before executing it.

$this->form_validation->set_rules('name', lang('title'), 'required|trim');
$this->form_validation->set_rules('description', lang('description'), 'required');
$this->form_validation->set_rules('slug', lang('slug'), 'trim|required|is_unique[categories.slug]|alpha_dash');

 if ($this->form_validation->run() == true)
 {

        $data = array(
            'name' => $this->input->post('name'),
            'description' => $this->input->post('description'),
            'parent_id' => $this->input->post('parent_category'),
            'slug' => $this->input->post('slug'),
            'active' => $this->input->post('active'),
            'private' => $this->input->post('private'),
        );
 }
Sign up to request clarification or add additional context in comments.

Comments

1

I don't try, but I think, if you add a variable to array post, like this, it works.

$this->input->post['user_id'] = $user_id;

Comments

1

as documentation here In the documentation if you want to override post data you need to set data first before run validation.

for ex:

$post_data = array_merge(array('user_id' => $id), $this->input->post(NULL, TRUE)); //merge existing post data with your custom field

$this->form_validation->set_data($post_data);

then

if ($this->form_validation->run('edit_user') === FALSE){
  // error view
}
else{
// success view

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.