We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Keep Old Inputs

Hello. Is there any way to keep inputs values after submiting form and got validation errors ? for example in the laravel framework the Input::flash() save posted values to the session in the controller and in the view input::old("inputname") return the input value than entered before submiting form.

Hi ksharifi :)
You can use persistent component to store values in the same controller space

I wrote example some days ago, look at this https://forum.phalcon.io/discussion/1865/how-to-create-a-delete-confirmation-in-other-page-



7.7k
edited Mar '14

I use this (after populating all of the vars for $robot): Phalcon\Tag::setDefaults(get_object_vars($robot));

Then the fields that were submitted are populated when the form is shown again, with the errors. Note that I'm not using the official Form component, just Tag to generate the form fields. It works exactly how I want it to.



15.4k

Okay but i think its useful when you are editing some records, in the first time we dont have any data yet to fill the fields



7.7k
edited Mar '14

It works good for me for inserts and updates (and deletes).

When you do an insert it looks like this:

$request = $this->request;
if (!$request->isPost()) {
// Show the form
} else {
  // Try to add the robot
  $robot = new Robots();
  $robot->name = $request->getPost('name', 'string');
  // etc
  if($robot->create() == false) {
  foreach ($file->getMessages() as $message) {
  // flash your message
  }
  // Keep the posted values
  Phalcon\Tag::setDefaults(get_object_vars($robot));
}else{
// Insert successful
}

Updates are the same, except you load the robot first and pre-populate the fields, otherwise its the same:

$robot = Robots::findFirstById($id);
if (!$request->isPost()) {
Phalcon\Tag::setDefaults(get_object_vars($robot));
}

(Sorry, not sure why my code isn't getting rendered right...)



15.4k

Thank you, I will try that, yes my code not rendered some time to and in dont know why to :)