String length in Agile Data

Is there a simple way to tell Agile Data the maximum length of a string or text field in the database? I’ve tried to set up my tables with large enough fields to store any reasonable values, but reasonable is in the eye of the beholder.

For now, I’ve added a few things like $m[‘name’] = substr($m[‘name’], 0, 40) in my beforeSave hook, but I’d love any tips you could give me to make it a little more user-friendly and informative.

Thanks in advance.

I don’t know of any max length limitation of the fields, but it is a good idea to override the validate() method of the model with your own validation for string lengths.

Check this out:

Thanks – that looks interesting, and was along the lines I’d started to go, with an intermediate class. I very much appreciate the pointer to the _default_seed_addField – I hadn’t explored that kind of thing, but had it on my todo list to find a way to code the validation instructions into the addField comment.

By the way, there’s a small error in the code in the writeup you linked to. In your validate method to the new ModelField class, you have:

            foreach($this->validate as $k=>$v) {
                $v->rule($k, $this->short_name, $v);

You’re using $v twice – once for the value, and once for the Validator that you passed from the new Model.

I changed this to:

            foreach($this->validate as $k=>$val) {
                if (is_int($k))
                    $v->rule($val, $this->short_name);
                    $v->rule($k, $this->short_name, $val);

The “is_int” business is a small improvement I made so that I could define validation strings like:

‘validate’ => [‘email’, ‘lengthMax’ => 100]]

Without the “is_int” business, the ‘email’ validation will return an error, since there’s no rule for “0” (or whatever integer gets assigned to any single-word Valitron validation rules.

Thanks again for the assistance – this made my life much easier.

I modified some of my agiletoolkit source files to accomodate a “maxlength” property, which uses the maxlength HTML tag to prevent the user from typing more than the alloted number of characters before they even submit the form. There are some other mods I did so the line numbers are only an approximate location. Once done you should be able to do something like this:

$this->addField('NAME', ['maxlength' => 30, 'type'=>'text', 'caption' => 'Name']);

Search “maxlength” (20 hits in 4 files)
\agiletoolkit\vendor\atk4\data\src\Field.php (4 hits)
Line 122: public $maxlength = null;
Line 460: return $this->maxlength ?: (isset($this->ui[‘maxlength’]) ? $this->ui[‘maxlength’] : null);
\agiletoolkit\vendor\atk4\ui\src\FormField\Input.php (6 hits)
Line 62: public $maxlength = null;
Line 91: $this->maxlength = $this->field->getMaxlength();
Line 100: ‘maxlength’ => $this->maxlength,