Thursday, January 7, 2010

Re: Validate Question

Hi Dave,

Is your rule made in accordance with CakePHP custom validation rules?
If yes, then I don't see a better solution.

See http://book.cakephp.org/view/150/Custom-Validation-Rules#Adding-your-own-Validation-Methods-152
for the custom validation rules.

Enjoy,
John

On Jan 7, 7:48 am, "Dave" <make.cake.b...@gmail.com> wrote:
> Using the inList validation rule I have 2 questions.
>
> 'rule' => array('inList', array('0', '1', '2','3', '4', '5', '6', '7')),
> will allow values matching 0 thru 7 anything else is invalid
>
> So my first question is if I created an variable array of values from a find
> could i just use 'rule' => array('inList', $myListVariable),
>
> and if that is possible how do i get the $myListVariable into the validation
> function?
>
> Or is better to just stick with what im currently doing. For example I have
> my country select list and it has a validation 'rule' =>
> array('checkCountryValues'),
>
> Model function for the country validation
> /*
>  Form Modification Validation
>  Checks that entered values for Country are Valid
>
>  */
>   public function checkCountryValues($data)
>       {
>     $valid = false;
>     $model_table = Inflector::classify($this->useTable);
>
>     if (!empty($this->data))
>     {
>      // this section matches values against database
>      $value = array($this->data[$model_table]['country_id']);
>      $results = array_diff($value, $this->Country->find('list',
> array('fields' => 'id')));
>
>      if (empty($results))
>
>      $valid = true;
>     }
>
>   return $valid;
>       }
>
> So if anyone messes with the form it returns invalid.
>
> Ideas?
>
> Dave

No comments: