This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Maz
Maz

I previously tried to implement the "form initialization", the one from the onSave handler, but with no success, even the static CONTEXT_CREATE is not defined.

I said to myself "I just have to reproduce the way onSave access the file to access them", but it was a total fail.

I've just try the first line from your answer (I've seen it yesterday in my searches), and it directly fail on:

$model = $this->formCreateModelObject();

I will reproduce the form with the Form markup, not such easy since it's a huge one and I need this feature in many models but... If it works, it may be the only way for now. Thanks for the investigation.

Want me to open the issue on GH for the formRender?

mjauvin
mjauvin

Want me to open the issue on GH for the formRender?

Sure, go ahead

mjauvin
mjauvin

The following handler will display a form preview in the debug bar:

public function onPreview()
{
    $model = $this->formCreateModelObject();
    $model = $this->formExtendModel($model) ?: $model;
    $this->initForm($model);

    debug($this->formRenderPreview());
}
Maz
Maz

Thanks for the tip, but it doesn't work for me:

"Call to a member function formCreateModelObject() on null"

$this is null? awesome :)

[EDIT] It works in a fresh controller. Need to investigate. This could be the solution since in a fresh controller I can retrieve the file with deferred binding:

public function onPreview($context = null)
{
    $model = $this->formCreateModelObject();
    $model = $this->formExtendModel($model) ?: $model;

    $session_key = Input::get('_session_key');
    debug($model->photos()->withDeferred($session_key)->get());
}

Now I just need to understand why my controller is screwed up. I've commented all the stuff but the onPreview method so it is as fresh as possible and it still doesn't work... hmmm...

[EDIT²] Ok, find my b****hit, in fact it was working: I've just left some messy code after the debug() which was previously dd().

But hey, now I can retrieve the photos! I was about to make the things much more complicated than needed. Here is my final code, I don't even need to use the formCreateModelObject(), when you understand how the deferred binding works -_-:

public function onPreview($context = null)
{
    return response(null, 200)
        ->withCookie('formData', json_encode(Input::get('Land')), 1)
        ->withCookie('preview_sessionKey', Input::get('_session_key'), 1);
}

And then in my component displayed on frontend:

if ($this->property('preview')) {
    $session_key = Cookie::get('preview_sessionKey');
    $this->land = new Land(json_decode(Cookie::get('formData'), true));
    $this->preview_photos = $this->land->photos()->withDeferred($session_key)->get();
}

I need to add some validation in the ajax handler but it looks pretty good now!

Need to thank you again @mjauvin :)

P.S: More than 24 hours for 10 lines of code XD sometimes it's really hard...

Last updated

mjauvin
mjauvin

Oh, nice, didn't know about withDeferred()

Last updated

mjauvin
mjauvin

Ok, for prosperity:

    public function onPreview()
    {   
        // using the formController
        $model = $this->formCreateModelObject();
        $this->initForm($model);
        $form = $this->formGetWidget();
        $sessionKey = $form->getSessionKey();
        $photos = $model->photos()->withDeferred($sessionKey)->get();

        // manually using the Input data
        $model = new Land(Input::get('Land'));
        $sessionKey = Input::get('_session_key');
        $photos = $model->photos()->withDeferred($sessionKey)->get();
    }

21-26 of 26

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.