Skip to content

I need to call session() if I want to be able to use old() in the forms. #1795

Description

@daljit3

name: Bug report

Describe the bug
If you fill only username and leave password empty below, then the testone() below doesn't retain the entered username. However, if I just call the session() method in the testtwo(), then it keeps the username.

It's a bit confusing for a new user as it's not suggested in the documentation.

Is this the correct behaviour?

public function testone()
{
	//This test fails to keep the old value
	$data_post = $this->request->getPost();
	$username = $data_post['username'] ?? '';
	$password = $data_post['password'] ?? '';
	
	if($username == '' || $password == '')
	{
		return redirect()->back()->withInput();
	}
	echo view('login');
}

public function testtwo()
{
	//This test preserves the old value
	
	$session = session();
	
	$data_post = $this->request->getPost();
	$username = $data_post['username'] ?? '';
	$password = $data_post['password'] ?? '';
	
	if($username == '' || $password == '')
	{
		return redirect()->back()->withInput();
	}

	echo view('login');
}

//Views/login.php
<form method="post" class="text-center border border-light p-5">
	<p class="h4 mb-4">Sign in</p>

	<input type="text" name="username" id="defaultLoginFormEmail" class="form-control mb-4" placeholder="Username" value="<?= old('username') ?>" />
	<input type="password" name="password" id="defaultLoginFormPassword" class="form-control mb-4" placeholder="Password" value="">

	<!-- Sign in button -->
	<button class="btn btn-info btn-block my-4" type="submit">Sign in</button>
</form>

CodeIgniter 4 version
Latest CI4 Beta.1

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions