Sunday, April 25, 2010

Re: Clear field after validation failure

On Apr 25, 1:19 am, Greg Skerman <gsker...@gmail.com> wrote:
> Hey guys,
>
> Trying to work out how to clear a form field if that field fails validation.
>
> I've tried doing an unset($this->data['Model']['fieldname']) but the data
> still seems to persist in the form when it is returned to the user to
> correct validation errors.

Are you sure that you're not overwriting it yourself? A common mistake
is to do something like this:

public function edit($id = null)
{
if (!empty($this->data))
{
// pseudocode
if (validate + save)
{
// redirect
}
}

// if above fails you'll be here

$this->data = $this->Foo->read(null, $id);
}

The read() line should be wrapped in an else block so that it's not
called if data was submitted.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: