2008年10月14日星期二

RE: [fw-mvc] Zend Form Improvement

See in my thinking is that the default values should already be validated.  Here is the simple one that I’m using:

 

    public function getChangedValues(array $defaultValues)

    {

        $changed = array();

        foreach($defaultValues as $key=>$val) {

            if(is_array($val)) {

                $ret = array_diff($this->getValue($key), $val);

                if(!empty($ret)) {

                    $changed[$key] = $val;

                }

            } else {

                if($this->getElement($key) !== null && $val !== $this->getValue($key)) {

                    $changed[$key] = $val;

                }

            }

        }

 

        return (!empty($changed)) ? $changed : false;

    }

 

Since my default array has all of the values in it I just loop though them.  This is by no means good code but it works for now.  I may expand on this once I get time to look at the Form Component more.

 

http://www.whitcraftconsulting.com/images/php5_zce_logo_new.gif

Jon Whitcraft
Indianapolis Motor Speedway
jwhitcraft@brickyard.com

P: (317) 492-8623 - F: (317) 492-6419


 

From: Hector Virgen [mailto:djvirgen@gmail.com]
Sent: Tuesday, October 14, 2008 3:44 PM
To: Pieter Kokx
Cc: Whitcraft, Jon; Zend Framework MVC
Subject: Re: [fw-mvc] Zend Form Improvement

 

The problem with using array_diff() is that it has to be done before validation, but I would imagine you only want to look for differences after validation.

 

I think this would be a nice addition to Zend_Form.

 

-Hector

On Tue, Oct 14, 2008 at 12:39 PM, Pieter Kokx <pieter@kokx.nl> wrote:

Hi,

PHP's array_diff() function and $form->getValues() should probably do the trick for you ;).

Regards,
 
Pieter Kokx
MaakSite.net
PHP Developer



Whitcraft, Jon schreef:

Hello everyone,

 

Today while working with Zend Form while using it to edit a form I wanted to have an easy way to check to see if anything changed and then return any fields that have changed.  The way I figured this would work is like this in your controller

 

$defaultValues = array(<some values here>);

$form = new My_Form();

If($form->isValid($_POST)) {

    $changedValues = $form->getChangedValues($defaultvalues);

}

 

That way you could only process any values that changed or skip the processing if nothing changed.

 

http://www.whitcraftconsulting.com/images/php5_zce_logo_new.gif

Jon Whitcraft
Indianapolis Motor Speedway
jwhitcraft@brickyard.com

P: (317) 492-8623 - F: (317) 492-6419


 


********************
********************
This E-mail (and attachments) may contain confidential/privileged information intended only for the named addressee(s). If you are not an intended recipient, do not read, copy, disseminate or take any action based on the content of this E-mail. Please notify the sender by reply E-mail and erase this E-mail from your system. Your assistance is appreciated. E-mail transmission may not be secure or error-free. The company is not responsible for any loss/damage arising from any virus transmitted.
********************
********************




--
-Hector

没有评论: