Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

isValid sets up old values? #6645

Closed
alexd5 opened this issue Sep 6, 2014 · 13 comments
Closed

isValid sets up old values? #6645

alexd5 opened this issue Sep 6, 2014 · 13 comments
Assignees
Milestone

Comments

@alexd5
Copy link

alexd5 commented Sep 6, 2014

Hello!

Strange work of isValid method in ZF 2.3.2. I have added comments.

     $form  = new AlbumForm();
     $form->bind($album); // $album with old values
     $form->get('submit')->setAttribute('value', 'Edit');

     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($album->getInputFilter());
         $form->setData($request->getPost());

         // form elements ($form->get('element')) get new values from $request->getPost().

         if ($form->isValid()) {
             $this->getAlbumTable()->saveAlbum($album);

             // form elements ($form->get('element')) get old values !

             return $this->redirect()->toRoute('album');
         }
     }
@samsonasik
Copy link
Contributor

have you try

$data = $form->getData();
echo $data['element'];

?

@alexd5
Copy link
Author

alexd5 commented Sep 7, 2014

Hello samsonasik!

I need to pass $form with new values to my template to show input field with formElement method.

PS: There is no that problem in ZF 2.3.1.

Thanks!

@samsonasik
Copy link
Contributor

@alexd5 yeah, I think it is a bug

@Martin-P
Copy link
Contributor

Martin-P commented Sep 7, 2014

Just tested with ZF 2.3.2 and my own forms and I think you simply forgot something in your view script:

$this->form->prepare();

When I leave out that line I get exactly what you describe and values are not remembered. Please see if you have that line in your view script.

@alexd5
Copy link
Author

alexd5 commented Sep 7, 2014

Hmmm...
$this->form->prepare() - not work for me.
Do you use something like that in your view script?
echo $this->formElement($form->get('element'));

Thank you!

@Martin-P
Copy link
Contributor

Martin-P commented Sep 7, 2014

Looking at your code, you are using the example from the manual. I tested it with that exact code and it works fine for me with ZF 2.3.2.

I can confirm your comments in the code in your opening post. If for some reason you need $form->get('element') in your controller, you need to prepare the form to get the new values:

if ($form->isValid()) {
    $this->getAlbumTable()->saveAlbum($album);

    // form elements ($form->get('element')) get old values !

    $form->prepare();
    // form elements ($form->get('element')) get NEW values !

    return $this->redirect()->toRoute('album');
}

It does not look like a bug to me, but it looks like you use the form in an incorrect way.

@samsonasik
Copy link
Contributor

Martin,
$form->prepare() already in the view, and it works for 2.3.1 but doesn't work for 2.3.2 with Album tutorial, just comment the saveAlbum() and redirect().

Warm regards,

Abdul Malik Ikhsan

Pada 8 Sep 2014, pukul 05.36, Martin-P notifications@github.com menulis:

@samsonasik
Copy link
Contributor

If saveAlbum() and redirect() is commented, then the issue showed.
It works for 2.3.1 but not in 2.3.2

Warm regards,

Abdul Malik Ikhsan

Pada 8 Sep 2014, pukul 05.36, Martin-P notifications@github.com menulis:

Looking at your code, you are using the example from the manual. I tested it with that exact code and it works fine for me with ZF 2.3.2.

I can confirm your comments in the code in your opening post. If for some reason you need $form->get('element') in your controller, you need to prepare the form to get the new values:

if ($form->isValid()) {
$this->getAlbumTable()->saveAlbum($album);

// form elements ($form->get('element')) get old values !

$form->prepare();
// form elements ($form->get('element')) get NEW values !

return $this->redirect()->toRoute('album');

}
It does not look like a bug, but it looks like you use the form in an incorrect way.


Reply to this email directly or view it on GitHub.

@Martin-P
Copy link
Contributor

PR #6295 introduced this bug. The line $objectData = $this->extract(); is the source of the problem.

@vixriihi
Copy link
Contributor

vixriihi commented Oct 3, 2014

Another solution to this issue is PR #6614

@Martin-P I added the test you had in your pull request to that

@Martin-P
Copy link
Contributor

Martin-P commented Oct 3, 2014

Nice, thanks.

@Martin-P
Copy link
Contributor

Martin-P commented Jan 3, 2015

@Ocramius This issue is fixed in #6614 and can be closed.

@Ocramius Ocramius added this to the 2.3.4 milestone Jan 3, 2015
@Ocramius Ocramius self-assigned this Jan 3, 2015
@Ocramius
Copy link
Member

Ocramius commented Jan 3, 2015

@Martin-P thanks, closing.

@Ocramius Ocramius closed this as completed Jan 3, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants