/** * Returns a PHP string representing options to pass to a validator for a given column. * * @param sfDoctrineColumn $column * @return string The options to pass to the validator as a PHP string */ public function getValidatorOptionsForColumn($column) { $options = array(); $isForeignKey = $column->isForeignKey(); $requiresEm = $isForeignKey || $column->isPrimaryKey(); if ($isForeignKey) { $options[] = sprintf('\'model\' => \'%s\'', $column->getForeignMetadata()->name); } else { if ($column->isPrimaryKey()) { $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->modelName, $column->getName()); } else { switch ($column->getDoctrineType()) { case 'string': if ($column['length']) { $options[] = sprintf('\'max_length\' => %s', $column['length']); } if (isset($column['minlength'])) { $options[] = sprintf('\'min_length\' => %s', $column['minlength']); } if (isset($column['regexp'])) { $options[] = sprintf('\'pattern\' => \'%s\'', $column['regexp']); } break; case 'enum': $values = array_combine($column['values'], $column['values']); $options[] = "'choices' => " . str_replace("\n", '', $this->arrayExport($values)); break; } } } // If notnull = false, is a primary or the column has a default value then // make the widget not required if (!$column->isNotNull() || $column->isPrimaryKey() || $column->hasDefinitionKey('default')) { $options[] = '\'required\' => false'; } return ($requiresEm ? '$this->em, ' : '') . (count($options) ? sprintf('array(%s)', implode(', ', $options)) : ''); }
/** * Returns a PHP string representing options to pass to a validator for a given column. * * @param sfDoctrineColumn $column * @return string The options to pass to the validator as a PHP string */ public function getValidatorOptionsForColumn($column) { $options = array('\'required\' => false'); $isForeignKey = $column->isForeignKey(); $requiresEm = $isForeignKey || $column->isPrimaryKey(); if ($isForeignKey) { foreach ($column->getForeignMetadata()->fieldMappings as $name => $fieldMapping) { if (isset($fieldMapping['id']) && $fieldMapping['id']) { break; } } $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getForeignMetadata()->name, $column->getName()); } else { if ($column->isPrimaryKey()) { $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->modelName, $column->getName()); } else { switch ($column->getDoctrineType()) { case 'boolean': $options[] = "'choices' => array('', 1, 0)"; break; case 'date': $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDateTime(array('required' => false))"; break; case 'datetime': case 'timestamp': $options[] = "'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59'))"; break; case 'enum': $values = array_combine($column['values'], $column['values']); $options[] = "'choices' => " . $this->arrayExport($values); break; } } } return ($requiresEm ? '$this->em, ' : '') . (count($options) ? sprintf('array(%s)', implode(', ', $options)) : ''); }