Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0.6 upgrade causes crash when rendering a view with a flux checkbox component to a variable before output to a file #1330

Open
3 tasks done
colinmackinlay opened this issue Mar 15, 2025 · 8 comments · May be fixed by #1356

Comments

@colinmackinlay
Copy link

Flux version

2.0.6

Livewire version

3.6.2

Tailwind version

4.0.7

Browser and Operating System

Edge WIndows

What is the problem?

Upgrading flux to 2.0.6 from 2.0.5 has introduced an issue when I render a view containing checkboxes using Illuminate\Contracts\View\Factory for output to a pdf file.

The error I get is 'Illuminate\View\ViewException: Undefined variable $errors (View: .....\vendor\livewire\flux\stubs\resources\views\flux\error.blade.php)'

I can trace this back to this code: $html = view($view, $data)->render(); when Illuminate\Contracts\View\Factory is being called using the view() helper method.

My view contains this code (simplified from actual to remove variables):

        <flux:checkbox
            :checked="true"
            label="some text"
        />

If everything else in the review is removed the error still occurs with this simplified code.
If this last bit of code is removed there is no error.
If I revert to flux 2.0.5 there is no error

Code snippets

        <flux:checkbox
            :checked="true"
            label="some text"
        />

How do you expect it to work?

If this code is contained in a view and the view is rendered to a file then no error should occur - just as was the case in flux 2.0.5

Please confirm (incomplete submissions will not be addressed)

  • I have provided easy and step-by-step instructions to reproduce the bug.
  • I have provided code samples as text and NOT images.
  • I understand my bug report will be closed if I haven't met the criteria above.
@jeffchown
Copy link

@colinmackinlay While simplified code snippets are good, this one isn't complete enough to be copy/pasteable to test/reproduce the issue. Can you provide one that is?

@colinmackinlay
Copy link
Author

@colinmackinlay While simplified code snippets are good, this one isn't complete enough to be copy/pasteable to test/reproduce the issue. Can you provide one that is?

Thanks for quick response @jeffchown . Perhaps I wasn't clear about simplifying - I've just taken all my app specific stuff out and tested with this code as the entire content of a view.

The code will render in a browser so there's no point in creating a volt component to show it. The error only happens when I call $html = view($view, $data)->render(); to get HTML to pass on to Browsershot to turn into PDF. It never gets that far though as the error occurs in Illuminate\Contracts\View\Factory

I've narrowed the change that causes the issue to #1308 . If I edit the props in error.blade.php so that 'nested' => false, then everything is fine again.

I don't explicitly render the error part of the flux component but it must be being triggered by some part of the way Laravel renders it to data rather than to a browser.

If I deconstruct checkbox so that it has no errors like this then the error goes away:

        <flux:field>
            <flux:label>some text</flux:label>
            <flux:checkbox
                    :checked="true"
            />
        </flux:field>

So I've found a workaround but it feels a bit of a kludge. The problemn is probably somewher within Illuminate\View rather than Flux I suspect

@jeffchown
Copy link

@colinmackinlay Thanks for the further detail.

Interesting. Sounds like @joshhanley might have some insight(s) given his work on that nested attributes validation/error PR.

@joshhanley
Copy link
Member

joshhanley commented Mar 17, 2025

@colinmackinlay thanks for reporting! I'm unable to replicate the issue with the code provided. Can you provide a repo that demonstrates the issue so we can investigate it? Thanks!

@colinmackinlay
Copy link
Author

@colinmackinlay thanks for reporting! I'm unable to replicate the issue with the code provided. Can you provide a repo that demonstrates the issue so we can investigate it? Thanks!

Thanks @joshhanley - I'll have a go at producing a clean repo to narrow it down. I'll get back to you when I've done that

@joshhanley
Copy link
Member

@colinmackinlay great, thanks!

@colinmackinlay
Copy link
Author

colinmackinlay commented Mar 18, 2025

Here's a repository @joshhanley : https://github.com/colinmackinlay/flux

  • Clean install of laravel
  • Flux checkbox on the dashboard page after logging in
  • Volt component to print the page to pdf (this is how the issue arises in my project)
  • Renders fine in browser
  • Renders the pdf (using puppeteer installed by npm) when clicking to call the print method in the component
  • Testing the component fails though (in my tests I check that the expected content is in the pdf)
  • Use ray calls to show that the error occurs when the view is internally rendered to html, so nothing to do with the browsershot rendering to pdf - you could just comment out the browsershot call, not install it and not install puppeteer.

@joshhanley joshhanley linked a pull request Mar 19, 2025 that will close this issue
@joshhanley
Copy link
Member

@colinmackinlay thanks for that!

So I can replicate the issue and I can see why the changes we have made are now triggering the error for you. But I also wanted to know why the error wasn't thrown previously and why it's thrown at all.

The reason is previously we only had this, in the errors component:

$message ??= $name ? $errors->first($name) : null;

So as you don't have a name parameter defined on your checkbox component, it's never calling $errors->first().

If I use Flux before the latest update and add a name attribute to the checkbox in your test app, it throws the same error.

Image

So then I experimented with removing the Flux component all together and using trying to render a view that might have errors in them, like you would normally do with Blade, by outputting {{ $errors->first() }} in the dashboard.blade.php somewhere. Again the same error occurs.

It turns out there is an issue with testing Blade views that have been loaded manually, as you can see by this Laracasts discussion https://laracasts.com/discuss/channels/testing/undefined-variable-errors-when-testing-a-view

One could argue that this is more of a Laravel issue than a Flux one, as this could happen anywhere that you might use $error in your views.

But that being said, we can fix it in Flux for this specific scenario.

Recently we added this, which is what is causing $errors->first() to be called, hence throwing the error in your case.

if ((is_null($message) || $message === '') && filter_var($nested, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== false) {
    $message = $errors->first($name . '.*');
}

So all we need to do is add a check for $name in the conditional first, which then matches how the original ternary works, and it solves this issue.

if ($name && (is_null($message) || $message === '') && filter_var($nested, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== false) {
    $message = $errors->first($name . '.*');
}

I've submitted a PR with a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants