Skip to content

Old() - Seems to have an issue with retrieving array values #1492

Description

@daljit3

name: Bug report
about: Help us improve the framework by reporting bugs!


Describe the bug
My input field is named as "locations[]". When the form failed validation - I tried to retrieve it as $locations = old('locations'); Which throws up an error - strpos() expects parameter 1 to be string, array given - BASEPATH/Common.php at line 786

The line 786 is in old() method in System/Common.php is
// If the result was serialized array or string, then unserialize it for use... if (strpos($value, 'a:') === 0 || strpos($value, 's:') === 0) { $value = unserialize($value); }
Here are the full methods from all the files which I think are related to this issue.
You can see the $value is being parsed through $request->getOldInput($key); in this old() method. There isn't any where I can see the arrays in POST data being serialised as I looked into WithInput() method too.

	function old(string $key, $default = null, $escape = 'html')
	{
		$request = Services::request();

		$value = $request->getOldInput($key);

		// Return the default value if nothing
		// found in the old input.
		if (is_null($value))
		{
			return $default;
		}

		// If the result was serialized array or string, then unserialize it for use...
		if (strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)
		{
			$value = unserialize($value);
		}

		return $escape === false ? $value : esc($value, $escape);
	}

** File system/HTTP/IncomingRequest.php **

	public function getOldInput(string $key)
	{
		// If the session hasn't been started, or no
		// data was previously saved, we're done.
		if (empty($_SESSION['_ci_old_input']))
		{
			return;
		}

		// Check for the value in the POST array first.
		if (isset($_SESSION['_ci_old_input']['post'][$key]))
		{
			return $_SESSION['_ci_old_input']['post'][$key];
		}

		// Next check in the GET array.
		if (isset($_SESSION['_ci_old_input']['get'][$key]))
		{
			return $_SESSION['_ci_old_input']['get'][$key];
		}

		helper('array');

		// Check for an array value in POST.
		if (isset($_SESSION['_ci_old_input']['post']))
		{
			$value = dot_array_search($key, $_SESSION['_ci_old_input']['post']);
			if (! is_null($value))
			{
				return $value;
			}
		}

		// Check for an array value in GET.
		if (isset($_SESSION['_ci_old_input']['get']))
		{
			$value = dot_array_search($key, $_SESSION['_ci_old_input']['get']);
			if (! is_null($value))
			{
				return $value;
			}
		}
	}

** File System/HTTP/RedirectResponse.php **

	public function withInput()
	{
		$session = $this->ensureSession();

		$input = [
			'get'  => $_GET ?? [],
			'post' => $_POST ?? [],
		];

		$session->setFlashdata('_ci_old_input', $input);

		// If the validator has any errors, transmit those back
		// so they can be displayed when the validation is
		// handled within a method different than displaying the form.
		$validator = Services::validation();
		if (! empty($validator->getErrors()))
		{
			$session->setFlashdata('_ci_validation_errors', serialize($validator->getErrors()));
		}

		return $this;
	}

CodeIgniter 4 version
4.0.0-alpha.2 Released

Affected module(s)
System/Common.php, System/HTTP/IncomingRequest.php, System/HTTP/RedirectResponse.php

Expected behavior, and steps to reproduce if appropriate
It should return full array.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix themin progress

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions