We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Numeric but with leading zeros?

I have a field that I want to store in MySQL. I want to enforce that it is always numeric but it can have leading zeros that I do not want stripped off. How would I accomplish this? In the Form, create the Element as a Numeric but make the field a varchar in the database? Is there a way to make a "Text" element that forces only 0-9 in each character position?

TIA!

In Mysql create the column as an INT(X) where X is the number of zeros - 1 you want to pad, and set ZERO FILL

I.E. id INT UNSIGNED ZEROFILL NOT NULL

it will always have leading zeros in the database, now if you want the leading zeros in php you have to do one more step by using a getter.

<?php

use Phalcon\Mvc\Model;

class MyModel extends Model
{
    protected $id;

    public function getId()
    {
        return str_pad($this->id,[PUT PAD LENGTH HERE] - strlen($this->id),"0",STR_PAD_LEFT );
    }

}