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 with two levels object

Hi people,

Reading https://forum.phalcon.io/discussion/191/setting-default-value-for-a-form-element I have a question: It's possible get the same result with two levels object?. For example:

Objects:

class Ob1 {
    public $element;
}
class Ob2 {
    public $name;
}

Forms:

class Form extends \Phalcon\Forms\Form
{
    public function initialize()
    {
        $field = new Text("name");
    }
}

Controller:

$obj = new Ob1();
$obj2 = new Ob2();
$obj2->name = "Hi";
$obj->element = $obj2;
$this->view->form = new Form($obj2);

View:

{{ form.render('name') }}

Thanks a lot



2.1k

yes you can. and here is 2 ways

class obj1
{
    public $field;
}
class obj2
{
    public $field;
    public $ob1;
}

class Form extends \P\Form
{
    public function initialize($data, $options)
    {
        $this->field = $data->field;
        $this->field2 = $data->ob1->field;
    }
}
$obj2 = new obj2;
$obj2->obj1 = new obj1();
$f = new Form($obj2, NULL);
class obj1
{
    public $field;
}
class obj2
{
    public $field;
}

class Form extends \P\Form
{
    public function initialize($data, $options)
    {
        $this->field = $options['obj1']->field;
        $this->field2 = $options['obj2']->field;
    }
}

$f = new Form(NULL, ['obj1' => new obj1(), 'obj2' => new obj2()]);