Thanks,

I like the $this->field('name', array('id' => $id)); Nice and simple.

K

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of cricket
Sent: Sunday, February 20, 2011 2:41 PM
To: [email protected]
Subject: Re: Variables for Validation

On Sun, Feb 20, 2011 at 8:22 AM, Krissy Masters
<[email protected]> wrote:
> Not sure why I only thought of this now.
>
> Any form: quick dummy function
>
> function edit() {
>
> If(!empty($this->data)) {
>
>       If($this->save());
>
>        // whatever
>       }
> $countries = $this->Model->_getCountries(); // returns list of countries
> $this->set(compact('countries'));
>
>
> }
>
> So you have a full list of countries in the form user can select from.
>
> $validate array() in model I have a function to check the country_id value
> and that function also uses _getCountries();
>
> public function checkCountryValues( $data, $field ){
>
> $valid = false;
>        if( !empty( $this->data[$this->alias][$field] ) ) {
>                if ( array_key_exists($this->data[$this->alias][$field],
> $this->_getCountries() ) ){
>                        $valid = true;
>                }
>        }
>        return $valid;
> }
>
>
>
> So I am essentially grabbing that data twice. How can I reuse that initial
> $countries throughout the whole process rather than grabbing the same data
> over and over?
> Might sounds trivial but some models might have 6 or more related models
and
> then validating pulling all the same values used in the view / form it is
> just added work.

You're making the mistake of focusing on the action instead of the
request. In your example, "the whole process" occurs over two separate
requests.

In the validation scenario, I think it's just as well to check if
$this->field('name', array('id' => $id)) returns empty or not.

It's just an example, I know, but your model's method is also
protected, so the controller could not call it.

-- 
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help
others with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to