Set default values to form via Ajax handle

Notice: This thread is very old.
JaxP
Member | 62
+
0
-

Hello,
I am trying to set default values to the form via ajax handle function.

I have something like this

public function handleGetUnitData($uid) {
    //...
    //accUnitForm is my form
    $this['accUnitForm']->setDefaults(array(
        "name" => "default name"
    ));
    //...
    $this->redrawControl('unitManager'); //unitManager is snippet where is form included
}

Snippet UnitManager is being updated (by redrawControl) but default values are not set (network console doesn't give any error – everything just goes fine).

Any ideas how to load default values “onclick” to the form?
Thank you

wohral
Member | 13
+
+1
-

Hi, problem is, that you try to set default values which is already rendered. I think what you need, is not to set default values but Values. I know that is not so clear solution, but try

$this['accUnitForm']->setValues(array(
        "name" => "default name"
    ), true);
mikeb
Member | 30
+
0
-

i'm adding a note for the next person struggling with this.
if you look at the code for setDefaultValue the value is only set if the form is not submitted ($form->isSubmitted() ==false). (also the form must not be disabled or anchored). else no value is set.

However isSubmitted() will return true when there is any getHttpData – ie even when another form on the page has ajaxed or posted.

so if the another (even non-nette) form has ajaxed/posted then setDefaultValues won't set your values. instead use setValues. like wohral's post above says. This behaviour may be documented somewhere but i haven't see it.

HTH