Forms, Modals, ViewField and $livewire
Appears I can easily use $livewire in an ->afterStateUpdated to set something like $livewire->criteria = Criteria::find($state);, which then i can acccess from my ViewField with $this->criteria. But as soon as I try that same form in a modal, its no longer available. I need this form with a ViewField to work in both a standalone livewire component and a action modal. Ive set public $criteria on the livewire component, but assuming because if the modal, its not available. Suggestions?
29 Replies
Haven't tested on v3, but maybe something like this
Oh sorry, if the issue is from within the ViewField, maybe
$getLivewire()
in the Blade view?well that does ive me the parent livewire component, but $criteria doesnt seem to get set on it. Its still null.
$getLivewire() on the blade view that is
Are you sure the state gets updated before the Blade view is rendered?
I mean, its a livewire property, so as that property is updated, the blade is rerendered. Everything works perfectly in a regular form, just not when that form is in a modal
I think you'll need to pass it to the action as $arguments or moutUsing().
not 100% sure though
but its all within the form. I have a select and when an option is picked, the viewfield is updated updated with a preview of that that that data
basically it gives you a better idea of what record you have selected before actually saving the form
so really has nothing to do with the action at this point
well it has something to do with the action considering it is launching the modal 🙂
and it's not working in the modal
yes, an Action is launching the modal of course, but the ViewField is entirely based on what is selected in the modal and is viewed in that modal and part of that form. Not related to anything that happens before that.
guess i would need to see the code. sorry.
@awcodes just something simple like
ive tried both a ViewField and a view
i'm surprised this is working in the page, to be honest
the criteria isn't getting passed to the view livewire component at all
so it shouldn't be able to mount it
@Mark Chaney What if you instead reach for
criteria_id
from the view, and then perform the query?the livewire component has public $criteria and $livewire->criteria is updating that
@pboivin i thought about htat, but that just seems wrong
right but the $livewire on the page is different from $livewire in a modal
dont think i should be doing a query directly from the blade file
modal is it's own livewire component
right, but you were talking about why you were surprised it worked on a page
I understand, not my first choice either but sometimes it can simplify things.
i really wish ViewData had closure support so i could just do ->viewData(fn($livewire) => ['criteria' => $livewire->criteria])
or somethign like that
I'm surprised because the View::make() is rendering a livewire component correct and $livewire in afterstate updated is the page component. So, how is the View even mounting with it as a property. Might be working because it's nested, not sure.
so either way, how can i pass a model or collection to a View or ViewField from afterStateUpdated()?
If you can reach from the view to the page component, can you call a method on it and encapsulate the query there? (get criteria_id from
$this->data
, then query and return the result)hmm, tried the following in the view blade .. but $criteria_id is null when in the modal. Gets the value just fine when doing the form on a regular page. grr. i hate these type of time sucks
Ok, just as a sanity check, from the Blade view, can you try
@dd($this)
? Does it give you the Livewire instance or the ViewField?its the parent livewire page. Same with $livewire or $getLivewire()
Ok. I'm not sure what to suggest honestly 😔
Does
criteria_id
belong to a form in the page or in the modal?its the select field within the form on the modal
i think i might be getting somewhere
Oh, so possibly this call to
$evaluate()
runs in the context of the page component?
I'm getting echos of another chat we had, where we had to reach into $this->mountedActionsData
. Does that make sense?yes, that was a bit different. I did look at that conversation first
well i have it working. Original method of is now working for both on page and modal. what the heck. lol