Skip to content

Form request object attribute returns empty when have some string #10403

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

Closed
kneipp opened this issue Sep 28, 2015 · 9 comments
Closed

Form request object attribute returns empty when have some string #10403

kneipp opened this issue Sep 28, 2015 · 9 comments

Comments

@kneipp
Copy link
Contributor

kneipp commented Sep 28, 2015

Hello,
Is this correct? I was validating an image when this happens:

public function store(Requests\FormRequest $request)
{
    var_dump($request->image); // #1
    var_dump(empty($request->image)); // #2
    die;

public function store(Requests\FormRequest $request)
{
    $t = $request->image;
    var_dump($t); // #1
    var_dump(empty($t)); // #3
    die;

#1 ~/work/vila/app/Http/Controllers/FormController.php:53:string 'Screen Shot 2015-09-25 at 06.49.32.png' (length=38)
#2 ~//work/vila/app/Http/Controllers/FormController.php:55:boolean true
#3 ~/work/vila/app/Http/Controllers/FormController.php:55:boolean false

@GrahamCampbell
Copy link
Member

Yes.

@kneipp
Copy link
Contributor Author

kneipp commented Sep 28, 2015

Why string attributes returns empty when is not empty on Laravel Form Request Object?

@beeblebrox3
Copy link

When you do $request->image you are calling the __get method from Request and getting the returned value. The empty function only works with variables (php.net/empty).

@kneipp
Copy link
Contributor Author

kneipp commented Sep 30, 2015

Hey @beeblebrox3 thx for reply, btw testing this again, work like expected with pure php (5.6):

    public function test()
    {
        return 'b';
    }
    var_dump(empty($this->test())); #bool(false) 

@beeblebrox3
Copy link

Yeah, sorry about that.

Look this gist: https://gist.github.com/beeblebrox3/0b5c2efd176ce3c4370e
When I do empty($obj->a) the __get method is not called.

Technically, $obj->a doesnt exists, so empty will return true

@rentalhost
Copy link
Contributor

@GrahamCampbell Look, it's really a Laravel bug. When you do empty($instance->property) it will check first for __isset() method on $instance and after for __get() method.

In this case, nether FormRequest or Request or ArrayAccess defines the __isset() method. And it is needly to it works. And considering that you are working with a FormRequest, you should be capable to do it.

Currently you should "hack" to make it works property, by two ways (but not that it's a workaround):

  • Copying attribute to variable: $value = $instance->value; then empty($value)
  • Force casting to jump over __isset(): empty( (string) $instance->value )

@kneipp
Copy link
Contributor Author

kneipp commented Sep 30, 2015

It' isn't laravel bug (yea, it's laravel bug): https://gist.github.com/kneipp/aee5802d08505d605676

[Gist updated]

@rentalhost
Copy link
Contributor

Yes, it's. Note that empty() fail on line 16 because you not have defined the method __isset(). And empty() will first try call __isset() and, only if it returns true, __get() will be called.

Try to define this method and you will see that __get() will be called as should be expected.

@kneipp
Copy link
Contributor Author

kneipp commented Sep 30, 2015

Yea, we're right it's a laravel bug.

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

No branches or pull requests

4 participants