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

new Select setDefault not working

I'm using form object and am trying to set a default option from the options i retrieved from a model. Everywhere I searched it appears it should be possible to set the default value by setDefault. But I can't get it to work. What am I missing?

In my for object the field looks like below. It does populate everything it just sets the default to the first option found. And I want to set the default to 'drie maanden', which means 'three months'.

// Guarantee
        $guarantee = new Select('guarantee', GuaranteeType::find(), [
            'using'        => ['name', 'name'],
            'useEmpty'     => false,
            'emptyText'    => 'Select guarantee term',
            'emptyValue'   => '',
            'class'        => 'form-control select2',
        ]);
        $guarantee->setDefault();
        $guarantee->setLabel('Guarantee');
        $guarantee->addValidators(
            [
                new PresenceOf(
                    [
                        "message" => "Selecting a guarantee term is required",
                    ]
                )
            ]
        );
        $this->add($guarantee);

Two possible problems

  1. Check if you have a getCustomValue() method in your form
  2. Disable select2


13.8k

Hi, sorry for the late response, picked up some other feature requests first.

I have tried both below sugesstions but it didnt help.

Two possible problems

  1. Check if you have a getCustomValue() method in your form
  2. Disable select2


13.8k

Reading https://olddocs.phalcon.io/en/3.0.0/api/Phalcon_Forms_Element_Select.html

About the setDefault option it writes:

public Phalcon\Forms\ElementInterface setDefault (mixed $value) inherited from Phalcon\Forms\Element

Sets a default value in case the form does not use an entity or there is no value available for the element in _POST

But my form uses an entity (model), could this be the problem?