On Sat, Mar 26, 2011 at 9:00 PM, euromark <dereurom...@googlemail.com> wrote: > just wrote something myself - although its not tested yet, either: > > /** > * @param array $data > * @param array $options > * - scope (array of other fields as scope - isUnique dependent on > other fields of the table) > * - batch (defaults to true, remembers previous values in order to > validate batch imports) > * example in model: 'rule' => array ('validateUniqueExt', > array('scope'=>array('belongs_to_table_id', 'user_id'))), > * //TODO: test!!! > * 2011-03-27 ms > */ > function validateUniqueExt($data, $options = array()) { > foreach ($data as $key => $value) { > $fieldName = $key; > $fieldValue = $value; > } > $defaults = array('batch'=>true, 'scope'=>array()); > $options = array_merge($defaults, $options); > > # for batch > if ($options['batch'] !== false && > !empty($this->batchRecords)) { > if (array_key_exists($value, > $this->batchRecords[$fieldName])) { > return $options['scope'] === > $this->batchRecords[$fieldName] > [$value]; > } > } > > # continue with validation > if (!$this->validateUnique($data, $options['scope'])) { > return false; > } > > # for batch > if ($options['batch'] !== false) { > if (!isset($this->batchRecords)) { > $this->batchRecords = array(); > } > $this->batchRecords[$fieldName][$value] = $scope; > } > return true; > } > > this way it doesnt matter if its called from saveAll() or multiple > validates()
I was confused about $this->batchRecords, thinking you'd need to copy $this->data to $this->batchRecords. But I see that the validation method takes care of that. Nifty. But your foreach loop seems a bit awkward. list($fieldName, $fieldValue) = $data; -- 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 cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php