Welcome Guest, Not a member yet? Register   Sign In
Form Validation w/ Callbacks
#1

[eluser]CISCK[/eluser]
I have a form that posts two arrays when it is submitted – categories and subcategories:

Code:
// example post data

$_POST['categories'] = Array
(
    [0] => category_1
    [1] => category_2
)

$_POST['subcategories'] = Array
(
    [0] => subcategory_1
    [1] => subcategory_2
)

I'm using the form validation class and I would like to test if the categories array is empty and only return false if the subcategories array is also empty.

I know I need to use callbacks, but I'm not sure exactly how to test this and make the error message show up in my view.

Code:
$config = array(

    'categories' => array(
    array(
         'field' => 'categories[]',
         'label' => 'Categories',
         'rules' => 'callback_categories_check'
         )
    )

);

// ------------------------------------------------------------------------

function categories_check($array)
{
    // I'm using this to test...
    $this->validation->set_message('categories_check', 'This is my error message');
    return false;
}

Using this in my view worked until I started using the callback:

Code:
&lt;?= form_error('categories[]', '<p class="error">', '</p>'); ?&gt;
#2

[eluser]Isern Palaus[/eluser]
Hello,

I can't help you but I've a question related.

I'm coding a online shop with multilanguage suport. The application checks the languages in the database and then creates, for example, the name for each language:

Code:
if(isset($idiomas) && $idiomas != FALSE)
    {
        foreach($idiomas as $id => $idioma)
        {
            echo form_fieldset($this->lang->line('admin_categorias_configuracion_idioma').$idioma['nombre']);
            
            echo '<li>';
            echo '<label for="nombre">'.$this->lang->line('admin_categorias_nombre').':</label>';
            $data = array('name' => 'nombre-'.$id,'value' => '');
            echo form_input($data);
            echo '</li>';
            
            echo '<li>';
            echo '<label for="descripcion">'.$this->lang->line('admin_categorias_descripcion').':</label>';
            $data = array('name' => 'descripcion-'.$id,'value' => '','cols' => '60','rows' => '10');
            echo form_textarea($data);
            echo '</li>';
            
            echo form_fieldset_close();
        }
    }

I've seen that you're validating "categories[]"... There is anyway to use a general setting for validate this? If not... I don't know how to validate via PHP.

Thank you in advance. Sorry for doesn't help you!

Isern
#3

[eluser]CISCK[/eluser]
I think you want to check out the form validation class.

Before I started using the callback function, which is where I'm having problems, I successfully used something like this:

Code:
// config/form_validation.php

$config = array(

    'categories' => array(
    array(
         'field' => 'categories[]',
         'label' => 'Categories',
         'rules' => 'required' // put your rules here...
         )
    )

);

// controllers/form_controller.php

    function index()
    {
        // Validate        
        if (isset($_POST['submit']))
        {                        
            if ($this->form_validation->run('categories'))
            {
                 // do something...
            }
        }
        
        // Data
        $data['categories'] = $this->category_model->find_all();
        
        // View
        $this->load->view('form_view', $data);
    }

// views/form_view.php

&lt;!-- form open --&gt;

&lt;?= form_error('categories[]', '<p class="error">', '</p>'); ?&gt;
&lt;!-- code for categories field --&gt;

&lt;!-- form close --&gt;

Hope that helps.
#4

[eluser]Isern Palaus[/eluser]
It helps me a lot. I'm actually using the form_validation.php too, if you've to add and update it's very usueful to write less code.

I've seen that you have to show the inputs with name as: categories[] to have an array.

Thank you!

Isern
#5

[eluser]CISCK[/eluser]
Glad to hear that helped. The form validation class is pretty useful. Now, hopefully I can figure out this callback stuff.
#6

[eluser]andrewtheandroid[/eluser]
I know I need to use callbacks, but I'm not sure exactly how to test this and make the error message show up in my view.

I'm not sure if CI can check if selects or radios are empty but maybe you could have a callback for another field (eg. name) that as a callback checks to see whether your fields are empty and then set the error message for that callback as in the example given for the form_validation guide.

Alternatively you could do your own check via input->post() for the fields and seeing if any selected and display your own error message. then in your view you could have

show errors from form() AND show custom error(s) () in the same container.

EDIT: $data['my_error'] = 'Need to select a catagory';

$this->load->view('myview',$data);

// in view
&lt;?php echo form_errors(); echo $my_error; ?&gt;
#7

[eluser]CISCK[/eluser]
Thanks for your suggestions, but I wanted like to stick with the form validation class since I'm already making good use of it.

SOLUTION

It appears that everything above was correct, however, my callback function wasn't being recognized in config/form_validation.php. The solution was to move the function into the corresponding controller (or use MY_Controller). In my opinion, it would be nice to keep the callback functions with the validation rules in form_validation.php, but at least everything is working now.

BTW, CI can validate and populate radios and checkboxes. Smile
#8

[eluser]123wesweat[/eluser]
[quote author="CISCK" date="1258625116"]

BTW, CI can validate and populate radios and checkboxes. Smile[/quote]

how do you validate checkboxes??
#9

[eluser]CISCK[/eluser]
Same way you would validate any other input. Just use an array for the field name (e.g. 'field' => 'checkboxField[]'). Make sense?




Theme © iAndrew 2016 - Forum software by © MyBB