/** * Validates one-to-* related records * * @param string $class The class to validate the related records for * @param array &$values The values for the object * @param array &$related_records The related records for the object * @param string $related_class The name of the class for this record set * @param string $route The route between the table and related table * @return array An array of validation messages */ private static function validateOneToStar($class, &$values, &$related_records, $related_class, $route) { $schema = fORMSchema::retrieve($class); $table = fORM::tablize($class); $related_table = fORM::tablize($related_class); $relationship = fORMSchema::getRoute($schema, $table, $related_table, $route); $first_pk_column = self::determineFirstPKColumn($class, $related_class, $route); $filter = self::determineRequestFilter($class, $related_class, $route); $pk_field = $filter . $first_pk_column; $input_keys = array_keys(fRequest::get($pk_field, 'array', array())); $related_record_name = self::getRelatedRecordName($class, $related_class, $route); $messages = array(); $one_to_one = fORMSchema::isOneToOne($schema, $table, $related_table, $route); if ($one_to_one) { $records = array(self::createRecord($class, $values, $related_records, $related_class, $route)); } else { $records = self::buildRecords($class, $values, $related_records, $related_class, $route); } foreach ($records as $i => $record) { fRequest::filter($filter, isset($input_keys[$i]) ? $input_keys[$i] : $i); $record_messages = $record->validate(TRUE); foreach ($record_messages as $column => $record_message) { // Ignore validation messages about the primary key since it will be added if ($column == $relationship['related_column']) { continue; } if ($one_to_one) { $token_field = fValidationException::formatField('__TOKEN__'); $extract_message_regex = '#' . str_replace('__TOKEN__', '(.*?)', preg_quote($token_field, '#')) . '(.*)$#D'; preg_match($extract_message_regex, $record_message, $matches); $column_name = self::compose('%1$s %2$s', $related_record_name, $matches[1]); $messages[$related_table . '::' . $column] = self::compose('%1$s%2$s', fValidationException::formatField($column_name), $matches[2]); } else { $main_key = $related_table . '[' . $i . ']'; if (!isset($messages[$main_key])) { if (isset(self::$validation_name_methods[$class][$related_class][$route])) { $name = $record->{self::$validation_name_methods[$class][$related_class][$route]}($i + 1); } else { $name = $related_record_name . ' #' . ($i + 1); } $messages[$main_key] = array('name' => $name, 'errors' => array()); } $messages[$main_key]['errors'][$column] = $record_message; } } fRequest::unfilter(); } return $messages; }
/** * Validates one-to-* related records * * @param string $class The class to validate the related records for * @param array &$values The values for the object * @param array &$related_records The related records for the object * @param string $related_class The name of the class for this record set * @param string $route The route between the table and related table * @return array An array of validation messages */ private static function validateOneToStar($class, &$values, &$related_records, $related_class, $route) { $table = fORM::tablize($class); $related_table = fORM::tablize($related_class); $first_pk_column = self::determineFirstPKColumn($class, $related_class, $route); $filter = self::determineRequestFilter($class, $related_class, $route); $pk_field = $filter . $first_pk_column; $input_keys = array_keys(fRequest::get($pk_field, 'array', array())); $related_record_name = self::getRelatedRecordName($class, $related_class, $route); $messages = array(); $one_to_one = fORMSchema::isOneToOne($table, $related_table, $route); if ($one_to_one) { $records = array(self::createRecord($class, $values, $related_records, $related_class, $route)); } else { $records = self::buildRecords($class, $values, $related_records, $related_class, $route); } // Ignore validation messages about the primary key since it will be added $primary_key_name = fValidationException::formatField(fORM::getColumnName($related_class, $route)); $primary_key_regex = '#^' . preg_quote($primary_key_name, '#') . '.*$#D'; fORMValidation::addRegexReplacement($related_class, $primary_key_regex, ''); foreach ($records as $i => $record) { fRequest::filter($filter, isset($input_keys[$i]) ? $input_keys[$i] : $i); $record_messages = $record->validate(TRUE); foreach ($record_messages as $record_message) { $token_field = fValidationException::formatField('__TOKEN__'); $extract_message_regex = '#' . str_replace('__TOKEN__', '(.*?)', preg_quote($token_field, '#')) . '(.*)$#D'; preg_match($extract_message_regex, $record_message, $matches); if ($one_to_one) { $column_name = self::compose('%1$s %2$s', $related_record_name, $matches[1]); } else { $column_name = self::compose('%1$s #%2$s %3$s', $related_record_name, $i + 1, $matches[1]); } $messages[] = self::compose('%1$s%2$s', fValidationException::formatField($column_name), $matches[2]); } fRequest::unfilter(); } fORMValidation::removeRegexReplacement($related_class, $primary_key_regex, ''); return $messages; }