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

Setting default value for a form element

Let's say I have this:

$form->add(new Text('number'));

How do I set a default value, e.g 1?



98.9k

The default value is taken from the related entity:

<?php

class Contact 
{
    public function $telephone = '+44 123-456-789';
}

$form = new Form(new Contact());

$form->add(new Text('telephone'));


10.0k

Thanks, that works.

I guess you meant "public $telephone" and not "public function $telephone"

@BlueShark Phalcon first checks 'getTelephone' function, than public property 'telepone' if first doesn't exist. From Phalcon 1.1.0 you can set default value by $formElement->setDefault($value); (without using entity)