Hi Seth,
I would say it's normal you do not get filtered values upon form submition, you use direct raw $_POST data.
Try replacing "$model->save($formData);" with "$model->save($form->getValues());".
Filters apply only when you call ->getValue() on a form element (what $form->getValues() does).
As for filtering applied when rendering the form, it may have the same origin, ->render() calls $input->getValue() I guess.
I hope it will help.
Remy
On Sun, Apr 19, 2009 at 3:42 AM, Seth Atkins <satkins@nortel.com> wrote:
Ok, I'm certain that I am doing something wrong, but I can't figure out what it is. The behavior I am seeing is that forms are not passing through the "filters" when I submit them. The filters are only applied when data is viewed (opening an edit form view). I set up a quick test to verify this. I created a custom filter that simply set the value of a field to '1' regardless of what is input. In my index view, I see the current row from the database, and the value of the column is empty. I click the link I setup that opens the form to edit the data. Boom, the field shows '1' in it. If I change the value to 2, then click the save button, the data that is written is 2, not 1. If the filter was being applied, the value should have been rewritten to 1 before being sent to my DB. Frankly, it's all backwards! I'd expect the edit view to show the current DB value, not the filtered value, and the DB write should be the filtered value, not the pre-filtered value.Here is the code snippet from my controller that deals with editing forms. Tell me what I am doing wrong.$form = new MyForm();
$form->submit->setLabel('Save');
$this->view->form = $form;
if ($this->_request->isPost()) {$formData = $this->_request->getPost();if ($form->isValid($formData)) {$model = new MyModel();$model->save($formData);$this->_redirect('/');} else {$form->populate($formData);}} else {$id = (int)$this->_request->getParam('id');if ($id > 0) {$model = new MyModel();$formData = $model->get($id);$form->populate($formData);}}according to the ZF docs, filtering is supposed to happen before validation, but that clearly is not happening in my case. It is skipping straight past the filter stage when I run "isValid()" on the data culled from the getPost(). BTW, if I am doing something wrong above, I'm sure it is a common mistake as some ZF tuts out there have this basic code structure for editing data. I would have never noticed this problem if I hadn't written a custom filter to turn empty form values to null database values (because it seems that "StringTrim" converts null form values to the empty string, and I need to fix that behavior with the custom filter)Thanks in advance!Seth
没有评论:
发表评论