public static function customValidation() { $base = Event::$data; if (!$base or !$base instanceof Number) { return TRUE; } $base['number'] = trim($base['number']); // Get the list of NumberContexts, the generic save will have added // all contexts in this list but we need to filter invalids (0) and // unassignments if (!($assigned_contexts = arr::get($_POST, 'number', 'NumberContext'))) { $assigned_contexts = array(); } // Loop each of the number contexts remove those that are not in the // post'd list or invalid (context_id == 0) foreach ($base->NumberContext as $pos => $context) { if (empty($context['context_id']) or !arr::search_collection($assigned_contexts, 'context_id', $context['context_id'])) { unset($base->NumberContext[$pos]); } } // If there are no contexts left... if (!$base->NumberContext->count()) { // see if there are more than one on this account, because if there // is only one then we know which to assign it to ;) $contexts = Doctrine::getTable('NumberContext')->findAll(); if ($contexts->count() == 1) { $base->NumberContext[] = $contexts[0]; } else { // sorry we tried... what do you want to do? throw new Exception('Please assign this number to at least one context'); } } if (!($assigned_pool = arr::get($_POST, 'number', 'NumberPool'))) { $assigned_pool = array(); } foreach ($base->NumberPool as $pos => $pool) { if (empty($pool['number_type_id']) or !arr::search_collection($assigned_pool, 'number_type_id', $pool['number_type_id'])) { unset($base->NumberPool[$pos]); } } if (!empty($base['class_type'])) { $numberType = Doctrine::getTable('NumberType')->findOneByClass($base['class_type']); if ($number_type_id = arr::get($numberType, 'number_type_id')) { if (!arr::search_collection($base->NumberPool, 'number_type_id', $number_type_id)) { $numberPool = new NumberPool(); $numberPool['number_type_id'] = $number_type_id; $base->NumberPool[] = $numberPool; } } else { kohana::log('error', $base['class_type'] . ' submitted but there is no such pool type!'); throw new Exception('Number does not belong to a number pool matching the destination'); } } if (!$base->NumberPool->count()) { throw new Exception('Please assign this number to at least one number pool'); } }
<ul> <?php foreach ($contexts as $context_id => $name) { ?> <li> <div class="field"> <?php echo form::label('context_' . $context_id, $name); ?> <?php echo form::checkbox(array('name' => 'number[NumberContext][][context_id]', 'id' => 'context_' . $context_id, 'class' => 'number_context_option'), $context_id, (bool) arr::search_collection($number['NumberContext'], 'context_id', $context_id)); ?> </div> </li> <?php } ?> </ul> <?php echo form::close_section(); ?>
<ul> <?php foreach ($numberTypes['numberTypes'] as $numberType) { ?> <li> <div class="field"> <?php echo form::label('numberType_' . $numberType['number_type_id'], inflector::humanizeModelName(str_replace('Number', '', $numberType['class']))); ?> <?php echo form::checkbox(array('name' => 'number[NumberPool][][number_type_id]', 'id' => 'numberType_' . $numberType['number_type_id']), $numberType['number_type_id'], (bool) arr::search_collection($number['NumberPool'], 'number_type_id', $numberType['number_type_id'])); ?> </div> </li> <?php } ?> </ul> <?php echo form::close_section();