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

Accessing $this->getModel() in Form class do not always return model provided #442

Closed
imTigger opened this issue Jul 10, 2018 · 3 comments
Closed

Comments

@imTigger
Copy link
Contributor

imTigger commented Jul 10, 2018

Problem: Accessing $this->getModel() in Form class do not always return model provided

Affected Version: 1.15.0 (1.14.0 not affected)

Excepted Behavior: $this->getModel() always return Model provided
Actual Behavior: $this->getModel() sometimes returns input array, when $form->isValid() is called

This behavior seems introduced by 43346b2 and affecting version 1.15.0

How to reproduce:

  1. Create a Controller that provide model to form builder
<?php

namespace App\Http\Controllers;

use App;

use App\Models\User;
use Kris\LaravelFormBuilder\FormBuilderTrait;
use Illuminate\Routing\Controller as BaseController;

class DebugController extends BaseController
{
    use FormBuilderTrait;

    public function handle() {
        $form = $this->form(App\Forms\UserForm::class, [
            'model' => User::whereId(1)->first()
        ]);

        $form->isValid();
    }
}

  1. Create a Form that use $this->getModel()
<?php

namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class UserForm extends Form
{
    public function buildForm()
    {
        $method = $this->getMethod();
        $entity = $this->getModel();

        $this->add('username', 'text', [
            'label' => 'Username',
            'rules' => ['required', 'max:255', in_array($method, ['get', 'post']) ? 'unique:users' : "unique:users,username,{$entity->id}"]
        ]);
    }
}
  1. Whoops!
ErrorException (E_NOTICE)
Trying to get property 'id' of non-object

Because $entity is not an object, $entity->id cause error

@philhq
Copy link

philhq commented Jul 10, 2018

I think this commit is what causing your issue:

43346b2

@imTigger
Copy link
Contributor Author

Yes, I also found out that was the cause.
But I don't quite understand the use-case of that commit.
Can't submit PR to fix that. :/

@kristijanhusak
Copy link
Owner

I fixed it in new release. I wasn't able to find an issue where it was reported, so i removed it.

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

3 participants