question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Unable to save empty input with relation HasOne

See original GitHub issue

Bug report

What I did

Sometimes it is necessary to delete data from input with relation HasOne

I try to store at least one input blank from the list:

$this->crud->addFields([
            [
                'name' => 'userInfo.first_name',
                'label' => __('First name'),
                'type' => 'text',
                'tab' => __('User info'),
                'wrapper' => [
                    'class' => 'form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-3',
                ],
            ],[
                'name' => 'userInfo.last_name',
                'label' => __('Last name'),
                'type' => 'text',
                'tab' => __('User info'),
                'wrapper' => [
                    'class' => 'form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-3',
                ],
            ],[
                'name' => 'userInfo.hrp_number',
                'label' => __('HRP - number'),
                'type' => 'text',
                'tab' => __('User info'),
                'wrapper' => [
                    'class' => 'form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-3',
                ],
            ],[
                'name' => 'userInfo.approved_hrp',
                'label' => __('HRP approved'),
                'type' => 'select_from_array',
                'tab' => __('User info'),
                'options' => UserInfo::getStatusValues(),
                'allows_null' => false,
                'default' => UserInfo::STATUS_APPROVE_PENDING,
                'wrapper' => [
                    'class' => 'form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-3',
                ],
            ],[
                'name' => 'userInfo.address',
                'label' => __('Address'),
                'type' => 'text',
                'tab' => __('User info'),
                'wrapper' => [
                    'class' => 'form-group col-xs-12 col-sm-12 col-md-6 col-lg-6',
                ],
            ],[
                'name' => 'userInfo.private_mobile',
                'label' => __('Private mobile'),
                'type' => 'text',
                'tab' => __('User info'),
                'wrapper' => [
                    'class' => 'form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-3',
                ],
            ],[
                'name' => 'userInfo.date_of_birth',
                'label' => __('Birthday'),
                'type' => 'date',
                'format' => 'd/m/Y',
                'tab' => __('User info'),
                'wrapper' => [
                    'class' => 'form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-3',
                ],
            ],
        ]);

What I expected to happen

I expected to see an empty field when I delete data from it

What happened

Data in input can only be written, changed but not deleted. I want to be able to delete data from the input field.

What I’ve already tried to fix it

I was looking for a solution on the Internet, in the official documentation, stackowerflow and other resources, but could not find anything

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug… is it still there? Yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

PHP 8.0.10 (cli) (built: Sep 3 2021 17:04:26) ( NTS ) Copyright © The PHP Group Zend Engine v4.0.10, Copyright © Zend Technologies

LARAVEL VERSION:

v8.70.2@dec9524cd0f9fa35a6eb8e25d0b40f8bbc8ec225

BACKPACK VERSION:

4.1.60@bec5cfa11f0cd0712c79256748acf5850080c0ee

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kiddtangcommented, Nov 30, 2021

@CinexUA another idea that I workaround for this issue. As I know this field can be cleared, I reverse ConvertsEmptyStringsToNull with the method

protected function handleEmptyInput($request)
{
    if (is_null($request->input('profile.address_2'))) {
        $profile = $request->input('profile');
        $profile['address_2'] = '';
        $request->request->set('profile', $profile);
    }
    return $request;
}

Inspired by handlePasswordInput, in UserCrudController.php I tried $request->request->set('profile.address_2', ''); but it is not working. So, I just working on the array.

Apply it like handlePasswordInput

public function update()
{
    $this->crud->setRequest($this->crud->validateRequest());
    $this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest()));
    $this->crud->setRequest($this->handleEmptyInput($this->crud->getRequest()));
    $this->crud->unsetValidation(); // validation has already been run

    return $this->traitUpdate();
}

Hope it helps 😃

@tabacitu @pxpm Maybe another field attribute allows_null or allow_clear to tell which field needs to reverse ConvertsEmptyStringsToNull ? Just a wild idea. XD

0reactions
tabacitucommented, Mar 1, 2022

Hi @CinexUA ,

In short, Backpack v4.2 == Backpack 5.0. But it should be a free upgrade for you, if you purchased Backpack after Feb 9th 2021. You can read more about that here. And the upgrade process itself shouldn’t be a long one at all.

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

error occured when save data using hasmany relationnship in ...
Error Comes: Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of ...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
To define this relationship, we will place a phone method on the User model. The phone method should call the hasOne method and...
Read more >
Saving Data - 4.x - CakePHP Cookbook
If you are using newEntity() and the resulting entities are missing some or all of the data they were passed, double check that...
Read more >
Django Tutorial Part 3: Using models - Learn web development
In this article we've learned how models are defined, and then used this information to design and implement appropriate models for the ...
Read more >
NetLogo 6.3.0 User Manual: NetLogo Dictionary
back (bk) <breeds>-at <breeds>-here <breeds>-on can-move? clear-turtles (ct) create-<breeds> ... If any? gets nobody as input, an error results.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found