0

Array values in form are always a problem for form validation in CI. Now I've to input multiple values and those array values are to be stored in DB. Now the user can keep some fields blanks by mistake as shown below in the links:

Input values.

Values in the array on submission.

I used this tutorial to add input boxes on + button click. On submission the blank values will be truncated and then not null array values will added to database. I tried this using a sample program in native PHP but could not implement it in CI.

I used the following code in native PHP to insert values in DB truncating null values:

<?php
    include 'sql_connect.php';
    $str = array();
    for($i=0;$i<count($_POST["txtSiteName"]);$i++)
    {
        $str[] = $_POST["txtSiteName"][$i];
    }
    $str = array_filter($str, function($item) {if(!is_null($item)) return $item;});
    foreach($str as $loop_str)
    {
        $arr_str[] = $loop_str;
    }
    for($k = 0; $k<count($arr_str);$k++)
    {
        mysql_query("INSERT INTO sitename (name) VALUES ('".$arr_str[$k]."')") or die(mysql_error());
    }
    print_r($arr_str);
?>

How can I achieve this in CI ? I tried to use callback function but the array value is not being passed to the callback function.

EDIT:

The below code shows my callback function :

On entering 3 urls it is called 3 times. Is that normal ? Also my array_walk's callback function is not working. Calling a callback function inside another callback function is not possible ?

public function null_check()
{
    $urls = $this->input->post('link_name');
    array_walk($urls, 'prepurl');
    var_dump($urls);
}

prepurl function:

public function prepurl($item, $key)
{
    $item = "http://".$item;
}
5
  • Are you dunamically adding textboxes? Commented Feb 9, 2013 at 9:36
  • Can you show your CodeIgniter code that you have tried ? Commented Feb 9, 2013 at 9:54
  • @SheikhHeera There is nothing new. I just created a callback function public function null_check($str) and gave $str as an argument to it. Added multiple values but it gets only a single string in $str instead of the complete array. And in the callback function I will add the above code as in the question in native PHP excluding the DB query. But I can do that only if have the array available. Commented Feb 9, 2013 at 10:03
  • are you generating these text fields statically or dynamically Commented Feb 9, 2013 at 10:17
  • @Venkat Dynamically on button click event. Commented Feb 9, 2013 at 10:24

2 Answers 2

1

Your question is not clear to me but since you mentioned to validate multiple text boxes. So if your text boxes are something like this

<input type="text" name="txtSiteName[]" />
<input type="text" name="txtSiteName[]" />

then simply you can use

$this->form_validation->set_rules('txtSiteName[]', 'Site Name', 'required|xss_clean');

to validate all the text boxes against null or empty.

Update (TO pass an argument in to the callback)

$this->form_validation->set_rules('txtSiteName[]', 'Site Name', 'callback_sitename_check[' . $this->input->post('txtSiteName') . ']');

function sitename_check($str, $sitenames) {
    // $sitenames will be your textboxe's array
}

The first argument is used by CodeIgniter by default so second argument is your parameter. Also take a look at insert_batch() to insert multiple records.

Also you can do this like

$this->form_validation->set_rules('txtSiteName[]', 'Site Name', 'callback_sitename_check');

function sitename_check() {
    $sitenames =  $this->input->post('txtSiteName');
}

Update: (For array_walk)

array_walk($urls, array($this, 'prepurl'));
Sign up to request clarification or add additional context in comments.

9 Comments

I already know this. This is not my issue. Some text boxes can be blank. Suppose user wants to add 3 values but he generates 4 text boxes by mistake then he cannot remove the extra text box. Hence if her enters 3 values and keeps the 4th text box blank then the blank value in the array will be truncated and stored in DB which will be the work of callback function.
In this case you can remove the required rule or use client side check to remove the empty text box before the submission.
but still people can hack if you don't do server side validation
- I removed required but I cannot accept null values also. -Yes I will be doing that too. I am thinking of creating a - button for removing the textbox.
@SheikhHeera $_POST['txtSiteName'] is throwing Undefined Variable error before submission. Also on submission no value is available. On var_dump this is shown : string 'Array' (length=5)
|
0

Here is a simple process of doing it

Set message like this

$this->form_validation->set_rules('txtSiteName[]', 'Site Name', 'required|xss_clean|callback_check_array');

Here is callback

function check_array()
{
    $txtSiteName    =   $this->input->post('txtSiteName');
    $error = 0;
    foreach($txtSiteName as $key => $value)
    {
        if(!empty($value)){
            $error = $error + 1;
        }
    }       
    if($error == 0){
        return TRUE;
    }else{
        $this->form_validation->set_message('check_array', 'All fields are empty. Please provide at leaset 1');
        return FALSE;
    }
}   

When validation is successfull run this code or modify according to your requirements

if ($this->form_validation->run() == FALSE)
{
    $this->load->view('myform');
}
else
{
    $txtSiteName    =   $this->input->post('txtSiteName');
    foreach($txtSiteName as $key => $value)
    {
        if(!empty($value)){
            $data['name']   =   $value;
            $this->mymodel->insert($data);
        }
    }
}

Model Method

function insert($data)
{
    $this-db->insert('table_name',$data);
}   

2 Comments

Are you sure $this->input->post('txtSiteName'); will take the complete array ?
if you have used checkboxes array than this is the same thing. if you are not sure than directly access $_POST['txtSiteName'] inside callback method.

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.