1

here is my simplified view

<?php
echo form_open('form/submit');

echo form_label('User Name:*', 'u_name');
$data= array('name' => 'u_name','placeholder' => 'Your Name');
echo form_input($data);

echo form_label('User emails:', 'u_email');
$data= array('type' => 'email','name' => 'u_email[0][email]', 'placeholder' => 'Email');
echo form_input($data);

$data= array('type' => 'email','name' => 'u_email[1][email]', 'placeholder' => 'Email');
echo form_input($data);

$data = array('type' => 'submit','value'=> 'Submit','class'=> 'submit');
echo form_submit($data); 

echo form_close();
 

and here is my sample controller

$this->form_validation->set_rules('u_name', 'Name', 'trim|required|max_length[20]');
if ($this->form_validation->run() == FALSE)
{
    $this->load->view('myview', $data);
}
 

Deviation from Expected:

When i submit my form without filling name field and after server side form validation goes false, i was expecting form validation error for name field. But it generates a Severity: Notice of Array to string conversion

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: helpers/form_helper.php

Line Number: 972

My Debugging Finding:

After trouble shooting i find out that my input as array fields are causing this Error Notice. But when i submit by filling out all required fields it works fine and input as array fields do not generate any Notice.

form_helper.php

function _parse_form_attributes($attributes, $default)
{
    if (is_array($attributes))
    {
        foreach ($default as $key => $val)
        {
            if (isset($attributes[$key]))
            {
                $default[$key] = $attributes[$key];
                unset($attributes[$key]);
            }
        }

        if (count($attributes) > 0)
        {
            $default = array_merge($default, $attributes);
        }
    }

    $att = '';

    foreach ($default as $key => $val)
    {
        if ($key === 'value')
        {
            $val = html_escape($val);
        }
        elseif ($key === 'name' && ! strlen($default['name']))
        {
            continue;
        }

        $att .= $key.'="'.$val.'" '; //Line Number: 972
    }

    return $att;
}

Please take a deep look on it and suggest me some solution for this.

Edit:

Take a look on below code from actual scenario where i am working, for better understanding

<?php
if(empty($additional_alert[0]))
{
   echo '<td id ="empty">No additional alert data found.</td>';
}
else
{
foreach($additional_alert as $key=>$additional_alert):
 ?><div>
<?php
$data   = array('placeholder'=>'Name', 'name'=>'additional_alert['.$key.'][name]', 'value'=>set_value('additional_alert', $additional_alert['name']), 'class'=>'');
   echo form_input($data);
$data   = array('placeholder'=>'Email', 'name'=>'additional_alert['.$key.'][email]', 'value'=>set_value('additional_alert',$additional_alert['email']), 'class'=>''); 
   echo form_input($data);
   echo '<button type="button" name="remove" id="" class="btn btn-danger btn_removes"><i class="fa fa-trash-o"></i></button>';
?><br>
</div>
<?php
endforeach; 
}
?>

and here is image from front-end and here is image from front-end

Community
  • 1
  • 1
Mobeen Sarwar
  • 514
  • 5
  • 23

2 Answers2

2

Retraction of "first" answer

My original answer (below) is wrong. It turns out that the syntax I claimed as invalid (e.g. 'name' => 'u_email[0][email]' ) is, in fact, perfectly OK. Either I'd never seen that syntax before or had forgotten about it. (So, I learned (or re-learned) something today which is one of the reasons I like hanging out at SO.)

The NEW (correct) answer

First off, using the original example code does not reproduce the problem. At least I cannot make it do so. The question's Edit code and DOM screen-shot give better, truer information by providing a better understanding of exactly what the goal is and where the problem might be.

In the OP you are not using "arrays as field names" - not for u_name at any rate. But in the edited question, you are! additional_alert is definitely an array.

So it turns out @Alex mostly pinpointed the problem. What wasn't clear is that using arrays as field names applies to all the "helper" functions.

The fix is to use the EXACT array name.

The "actual scenario" code uses this syntax which produces the Array to string conversion error.

$data = array(
    'placeholder' => 'Name',
    'name' => 'additional_alert['.$key.'][name]',
    'value' => set_value('additional_alert', $additional_alert['name']),
    'class' => '');
echo form_input($data);

What took some time to realize is that the error happens during set_value() because, being a "helper", it requires the EXACT array name. If you use the following code there is no error and life is beautiful.

$data = array(
    'placeholder' => 'Name', 
    'name' => 'additional_alert['.$key.'][name]',
    'value' => set_value('additional_alert['.$key.'][name]', $additional_alert['name']),
    'class' => '');
echo form_input($data);

Obviously, (it is isn't it?) you have to do the same for the email.

$data = array('placeholder' => 'Email',
    'name' => "additional_alert[$key][email]",
    'value' => set_value("additional_alert[$key][email]", $additional_alert['email']),
    'class' => '');
echo form_input($data);

(Note the use of double quotes for the values of 'name'=> and 'value'=>? I like that syntax instead of using the dot (.) to concatenate strings. IMO, it is easier to read. And no, you don't need to put single quotes around $key.)

A Suggestion:

You use the construct

foreach($additional_alert as $key=>$additional_alert):

That's certainly works but it confused me momentarily a little further on in the line set_value('additional_alert', $additional_alert['name']).

To avoid such confusion I suggest you use the form

foreach($plural_values as $key => $singular_value) 

In your code then use the plural $additional_alerts, i.e.

foreach($additional_alerts as $key => $additional_alert):`

-- Original (wrong) Answer --

I believe the error is in 'name' => 'u_email[0][email]', should probably be 'name' => 'u_email[]'.

And 'name' => 'u_email[1][email]' should be 'name' => 'u_email[]'

When your view tries to load the bad syntax makes form_input('$data') choke. When you reference the field in the controller then you use the indexing, e.g.

$emails = $this->input->post('u_email');
$email_1 = $emails[0];
$email_2 = $emails[2];
DFriend
  • 8,869
  • 1
  • 13
  • 26
1

https://codeigniter.com/user_guide/libraries/form_validation.html#using-arrays-as-field-names

Thus:

$this->form_validation->set_rules('u_name[]', 'Name', 'trim|required|max_length[20]');
Alex
  • 9,215
  • 8
  • 39
  • 82
  • thanks for your response. But im not using name field as array input. – Mobeen Sarwar Sep 16 '19 at 12:35
  • array input is email field and that is not required in my controller as i mentioned in my question – Mobeen Sarwar Sep 16 '19 at 12:37
  • so please can you take a look again on it for this unexpected notice – Mobeen Sarwar Sep 16 '19 at 12:38
  • 2
    Sorry to say, but `'name' => 'u_email[0][email]'` **is an array input**. It's an invalid one because array inputs are a single dimension arrays. – DFriend Sep 16 '19 at 21:54
  • 1
    I believe that what @DFriend is saying still applies. Your format should be `email[]` for each email input .etc. Any variation of `somestr[index][name]` isn't valid. – Alex Sep 17 '19 at 06:02