I'm using a Form class to create a form, all works fine except I can't seem to pre populate the form with old form data. For example, when a user completes the form, if they make an error I want to have the form fields populated with the values they already entered.
I'm following the documentation, but it's not very clear. Here is the controller method that handles the post request from the form. I thought I'd be able to just pass $_POST to the CreateSnapForm constructor, and the Form will auto populate:
public function createAction()
{
if (!$this->security->checkToken()) {
$this->flashSession->error('Sorry, we couldn\'t create your snap');
return $this->response->redirect('/');
}
$form = new CreateSnapForm();
# Tried ...
// $form->bind($_POST, new Snap());
if(!$form->isValid($this->request->getPost()))
{
$this->flashSession->error($form->getMessages()[0]);
return $this->response->redirect('/');
}
...
}
The documentation mentioned binding an entity, but the only entity I have is the model 'Snap', and if I bind that to the form, with the $_POST variable, it doens't work. Here is the model, it contains more properties than the form (not sure if that would cause a problem, hence why I posted the model). But to be honest I don't understand the reason why an entity must be provided to the form as I don't want the form to be responsible for saving the data:
use Phalcon\Mvc\Model;
class Snaps extends Model
{
public $id;
public $image;
public $username;
public $age;
public $gender;
public $votes;
public $views;
public $is_ghosted;
public $browser;
public $ip;
public $proxy;
public $deleted_at;
public $created_at;
}
Any ideas?