Callback set_message not working with multidimensional array post

351 Views Asked by At

On my codeigniter project my callback is not working as would like with my multidimensional array post

I am trying on my callback set message $key['image'] to display correct value of the multidimensional array post. But when I var dump it returns NULL

On my set rules the multidimensional array validation works.

But for some reason the $key['image'] on my callback function set_message('banner_image_regex', 'This banner' .' '. $key['image'] .' '. 'image contains a underscore cannot upload file.') not picking up.

Question on my callback function banner_image_regex() how can I make $key['image'] pick up the post value and display it.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banner_add extends MX_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $data['title'] = "Banner Add";

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

        $this->form_validation->set_rules('banner_name', 'Banner Name', 'required|callback_validate_form');
        $this->form_validation->set_rules('banner_status', 'Banner Status', 'required');

        if (!empty($this->input->post('banner_image'))) {
            foreach ($this->input->post('banner_image') as $key => $value) {
                $this->form_validation->set_rules('banner_image['.$key.'][image]', 'Banner Image', 'callback_banner_image_regex');
            }
        }

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

            $this->load->view('template/banner/banner_add', $data);

        } else {

            redirect('admin/banner/banner_list');

        }
    }

    public function banner_image_regex() {
        $banner_image = $this->input->post('banner_image');

        foreach ($banner_image as $key => $value) {
            if (preg_match('/^[a-z0-9]+$/', $key['image'])) {

                return TRUE;

            } else {

                $this->form_validation->set_message('banner_image_regex', 'This banner' .' '. $key['image'] .' '. 'image contains a underscore cannot upload file.');
                return FALSE;
            }
        }
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

Asuming you were following CI specs about naming array fields in form validation, you are already sending specific value (string/image name) to callback function. So it need to be like:

public function banner_image_regex($image_name) {
    if (preg_match('/^[a-z0-9]+$/', $image_name)) {
        return TRUE;
    } else {
        $this->form_validation->set_message('banner_image_regex', 'This banner' .' '. $image_name .' '. 'image contains a underscore cannot upload file.');
        return FALSE;
    }

}