CodeIgniter Forums

Full Version: Invalid argument supplied for foreach() when using parser
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello all, Im just moving from CI3 to CI4 , im still playing around with it and;
here some code example

in my controller auth.php
PHP Code:
<?php

namespace App\Controllers;

class 
Auth extends BaseController
{
    public function index($ext null)
    {
        $data = [
            'submit_url'        => base_url('auth/submit'),
            'error'             => $ext['error'],
            'csrf_token'        => csrf_token(),
            'csrf_hash'         => csrf_hash()
        ];
        echo view('inc/header');
        echo $this->parser->setData($data)->render('auth');
        echo view('inc/footer');
    }

    public function submit()
    {
        $this->validation->setRules([
            'email'         => 'required|valid_email',
            'password'      => 'required',
        ]);
        $valid $this->validation->withRequest($this->request)->run();
        if ($valid === false) {
            $ext = [
                'error' => $this->validation->getErrors(),
            ];
            return $this->index($ext);
        }
        $email $this->request->getPost('email');
        return $email;
    }


in my view auth.php
PHP Code:
{error}
{
email}
{
password}
{/
error}

<
form class="ui form" method="post" action="{submit_url}">
    <div class="field">
        <label>Email</label>
        <input type="text" name="email" placeholder="Email">
    </div>
    <div class="field">
        <label>Password</label>
        <input type="password" name="password" placeholder="Password">
    </div>
    <input type="hidden" name="{csrf_token}" value="{csrf_hash}" />
    <button class="ui button" type="submit">Submit</button>
</
form


but i get this error after try surfing the page
[attachment=1687]


it is same like i did in CI3 when using parser, hope someone can help me. thank you
What is the value of $ext['error'] ? Any chance it's a string?
(01-26-2020, 10:31 PM)kilishan Wrote: [ -> ]What is the value of $ext['error'] ? Any chance it's a string?
 
first of all, thanks for replying

so I did a var_dump var $data

here I get:-

Code:
array(4) {
  ["submit_url"]=>
  string(47) "http://192.168.1.103/test/public/auth/submit"
  ["error"]=>
  array(2) {
    ["email"]=>
    string(28) "The email field is required."
    ["password"]=>
    string(31) "The password field is required."
  }
  ["csrf_token"]=>
  string(12) "csrf_test"
  ["csrf_hash"]=>
  string(32) "154e1de7b3679b11720940ce8e6cc2ae"
}
hello.
is there already a solution here? i have the same problem. in the ci4 documentation the example does not work either.

greeting, mike


https://codeigniter4.github.io/userguide...ight=parse

the following example from the documentation sends folgwenden error: CRITICAL - 2020-02-19 14:50:57 --> Invalid argument supplied for foreach()

$template = '{name} lives in {location}{city} on {planet}{/location}.';

$data = [
        'name'    => 'George',
        'location' => [ 'city' => 'Red City', 'planet' => 'Mars' ]
];

echo $parser->setData($data)->renderString($template);
// Result: George lives in Red City on Mars.
no solution yet, currently I'm using this way to solve my problem

PHP Code:
<?php

namespace App\Controllers;

class 
Auth extends BaseController
{
    public function index()
    {
        $data = [
            'submit_url'    => base_url('auth/submit'),
            'csrf_token'    => csrf_token(),
            'csrf_hash'     => csrf_hash(),
            'responses'     => $this->session->getFlashdata()
        ];
        echo $this->parser->setData($this->header)->render('inc/header');
        echo $this->parser->setData($data)->render('auth');
        echo $this->parser->setData($this->footer)->render('inc/footer');
    }

    public function submit()
    {
        $this->validation->setRules([
            'email'         => 'required|valid_email',
            'password'      => 'required',
        ]);
        $valid $this->validation->withRequest($this->request)->run();
        if ($valid === false) {
            $ext = [
                'email'     => $this->validation->getError('email'),
                'password'  => $this->validation->getError('password'),
                'message'   => ''
            ];
            return redirect()->back()->with('error'$ext);
        }
        $email $this->request->getPost('email');
        return $email;
    }


but this way not really nice since I need to declare each error to their own var rather than using array.
(02-19-2020, 02:34 PM)MikiStoni Wrote: [ -> ]the following example from the documentation sends folgwenden error: CRITICAL - 2020-02-19 14:50:57 --> Invalid argument supplied for foreach()

$template = '{name} lives in {location}{city} on {planet}{/location}.';

$data = [
        'name'    => 'George',
        'location' => [ 'city' => 'Red City', 'planet' => 'Mars' ]
];

echo $parser->setData($data)->renderString($template);
// Result: George lives in Red City on Mars.

Dear MikiStoni,

There is a typo in the documentation. Location should be enclosed in additional square brackets to form an array, e.g.
PHP Code:
$data = [
        'name'    => 'George',
        'location' => [['city' => 'Red City''planet' => 'Mars']]
]; 

This comes because Parser expects attribute to be either a string (like George), or an Array-of-Arrays (e.g. $location[0] = ['city' => 'Red City', 'planet' => 'Mars']) in order to be able to iterate over it, and then process each pair inside the outer array as normal key:value pair like it was processing 'name'=>'George' pair before.

Probably topicstarter should do the same with errors array, and enclose it into additional array.
Thx Zahhar

that's exactly right. thank you very much. who is writing the documentation? this urgently needs to be corrected!