Hi
i'm new to Phalcon and i realy do love the speed an simplicity. Unfortunately, it's very hard ( for an 'old' ZF-Dev ) to find out how the simpliest Tasks should be realized with Phalcon. Many solutions i try (not only this particular problem) don't work as expected/usual. In most cases "my way" is not working with Phalcon and i can't find informations on how it should be done with Phalcon :( ...
The (current) Problem:
I want to display a form, prefill it from DB/Model, submit it, filter and validate it, save it an display the saved data or display the error messages and submitted data again in the form.
In my opion an every-day-task for everyone of us. ... but i just don't get it fully to work with Phalcon.
Usualy i do something like this (bit simplified):
// Controller
public function editAction()
{
$object = Model::fecthByPk($this->getParam('id'));
$form = new Form(['defaults'=>$object]); // Form defines filtering and validation
if ($this->isPost() && $form->isValid($this->getParams())) {
$object->fromArray($form->getValues());
$object->save();
}
$this->view->assign(array(
'form' => $frm,
'object' => $object,
));
}
// View
<form ...>
<? if ($form->hasError()): ?>
<div class="error">There was an error</div>
<? endif ?>
<div class="<?=$form->field->hasError() ? 'error' : ''?>">
<?= $form->field->render() ?>
</div>
...
</form>
After dozens of pitfalls with forms, form-elements, form-errors, form-validation vs. model-valitation and so on it's nearly working, except the display of the form after submisson.
The form always displays the submitted data, but i want it to display the sanitized and stored values.
Example:
-
- field with "trim"-Filter:
-
- submited value: " test "
-
- stored value: "test" (as wanted/expected)
-
- re(displayed) Value in the Form: " test " (why?)
(sry, markdown-list not working !?)
I tried to clear the form and (re)set the entity after validating saving, with no effect. :/
So, what's wrong, how can i solve it and what am i missing?
my current action
public function editAction($id=null)
{
$success = null;
$project = Projects::findFirst("id = {$id}");
$this->_getProjectForm()->setEntity($project);
if ($this->request->isPost()) {
$this->_getProjectForm()->bind($this->request->getPost(), $project);
$success = $this->_getProjectForm()->isValid() && $project->save();
}
$this->view->setVars(array(
'project' => $project,
'form' => $this->_getProjectForm(),
'success' => $success,
));
}
thanks in advace for your help (... to find my way through Phalcon)
(P.S.: I'm Missing a "Forms"-Category. )