Maxlength using Database Field Lengths

I wanted to have a maxlength="something" in my forms, but I needed the value to come from the database at least during development. When it goes live the data will cache so it will still be fast.

1. Create a cache folder in app/tmp/cache/fieldlengths

2. Create in app/controllers/components/fieldlength.php

 class FieldlengthComponent extends Object
{
  var $controller = true;

  function startup(&$controller)
  {
    $this->controller = &$controller;
    $controllerClass = $this->controller->name;
    $modelClass = $this->controller->modelClass;

    if (!$fieldLengths = cache('fieldlengths'.DS.$modelClass))
    {
      $cols = $this->controller->$modelClass->query('DESC ' . $this->controller->$modelClass->table);
          foreach ($cols as $column) {
              if (isset($column['COLUMNS'])) {
              if (preg_match('/^(?!.*int).*(?:[(]([0-9]+(?:,[0-9]+)?)[)])$/', $column['COLUMNS']['Type'], $regs))
              {
                $fieldLengths[$column['COLUMNS']['Field']] = $regs[1];
          }
          elseif (strpos($column['COLUMNS']['Type'],'date')!==false)
          {
                $fieldLengths[$column['COLUMNS']['Field']] = 'date';
          }
              }
          }
      $fieldLengths = CACHE_CHECK ? cache('fieldlengths'.DS.$table, serialize($fieldLengths)) : serialize($fieldLengths);
    }
    $this->controller->set('field_lengths', unserialize($fieldLengths));
  }
}

3. Add this to app/views/[-tablename-]/form.thtml
 echo $html->input('[-Model-]/[-field-]', array('size' => '50', 'maxlength' => $field_lengths['[-field-]']));

Post new comment

  • Allowed HTML tags: <b> <br> <p> <a> <strong> <cite> <em> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may use [img:xx] tags to display uploaded files or images inline.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options