Re: Validate belongsTo model in select element - inList does not seem to work?

2009-02-04 Thread brian

On Wed, Feb 4, 2009 at 7:02 AM, grigri  wrote:
>
> Something like this should work:
>
> (code not tested, but theory sound)
>
> class Customer extends AppModel {
>  var $validate = array(
>'country_id' => array(
>  array(
>'rule' => array('custom', '/^[a-z]{2}$/'),
>'required' => true,
>'message' => 'Please select the country that you reside in.'
>  ),
>  array(
>'rule' => array('checkCountry'),
>'message' => Please stop playing with firebug.'
>  ),
>)
>  );
>
>  function checkCountry($data) {
>$countryCode = reset($data);
>return $this->Country->hasAny(array('Country.code' =>
> $countryCode));
>  }
> }
>
> hth
> grigri

That's much more sound. And thanks for the laugh.

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Validate belongsTo model in select element - inList does not seem to work?

2009-02-04 Thread grigri

Something like this should work:

(code not tested, but theory sound)

class Customer extends AppModel {
  var $validate = array(
'country_id' => array(
  array(
'rule' => array('custom', '/^[a-z]{2}$/'),
'required' => true,
'message' => 'Please select the country that you reside in.'
  ),
  array(
'rule' => array('checkCountry'),
'message' => Please stop playing with firebug.'
  ),
)
  );

  function checkCountry($data) {
$countryCode = reset($data);
return $this->Country->hasAny(array('Country.code' =>
$countryCode));
  }
}

hth
grigri

On Feb 4, 7:01 am, Braindead  wrote:
> Brian, your solution seems to be ok, but there is one problem:
>
> Let's assume the selectbox contains the 3 items de, nl and dk.
> Selecting one of the items would pass the validation.
> But aa, ab, ac, ad, ae and so on would also pass the validation
> without being valid.
>
> So what is the way to go? I guess you have to write a custom
> validation rule, that checks if the selected item is part of the
> association.
>
> Hope that helps.
> Markus
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Validate belongsTo model in select element - inList does not seem to work?

2009-02-03 Thread Braindead

Brian, your solution seems to be ok, but there is one problem:

Let's assume the selectbox contains the 3 items de, nl and dk.
Selecting one of the items would pass the validation.
But aa, ab, ac, ad, ae and so on would also pass the validation
without being valid.

So what is the way to go? I guess you have to write a custom
validation rule, that checks if the selected item is part of the
association.

Hope that helps.
Markus
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Validate belongsTo model in select element - inList does not seem to work?

2009-02-03 Thread brian

On Tue, Feb 3, 2009 at 2:40 PM, Mickiii  wrote:
>
> Hi,
>
> I am trying to validate data from an association the I output to the
> view like:
>
> $this->set('countries', $this->Customer->Country->find('list'));
>
> and in the view:
>
> echo $form->input('Customer.country'); // Results in select
>
> How would I validate the input of this form? I was thinking I could
> use "inList", but that does not seem to provide this functionality.
>

It depends on the primary key you're using for Country. I use the ISO
code, others might use an auto-increment field. Either way, just check
that Customer.country_id is not empty and conforms to whatever kind of
PK you're using.

**
I just noticed that your field is "country" not "country_id". The
latter is the way Cake would expect it by default.
**
In your Customer's $validate array ...

'country_id' => array(
'rule' => array('custom', '/^[a-z]{2}$/'),
'required' => true,
'message' => 'Please select the country that you reside in.'
)

or, for integer PK:

'country_id' => array(
'rule' => array('custom', '/^[0-9]+$/'),
'required' => true,
'message' => 'Please select the country that you reside in.'
)

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Validate belongsTo model in select element - inList does not seem to work?

2009-02-03 Thread Mickiii

Hi,

I am trying to validate data from an association the I output to the
view like:

$this->set('countries', $this->Customer->Country->find('list'));

and in the view:

echo $form->input('Customer.country'); // Results in select

How would I validate the input of this form? I was thinking I could
use "inList", but that does not seem to provide this functionality.

Thanks,

Michael
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---