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

Camelcase properties don't get shortened in validation messages properly #2079

Closed
JoshMoreno opened this issue Nov 22, 2020 · 3 comments
Closed

Comments

@JoshMoreno
Copy link
Contributor

Description

The attribute name is not shortened when using model property binding if the property name is camelcase.
In the example below naming the property $companyProfile results in this bug. Snake case and all lower case work as expected:

$company_profile
$companyprofile

Exact steps to reproduce

  1. Create the CompanyProfile model with a string name column.
  2. Create the livewire component and blade template.
  3. Without filling the form out, submit and you'll see an error message that reads:

The company profile.name field is required.

It should read:

The company name field is required.

Stripped-down, copy-pastable code snippets

app/Http/Livewire/UpdateCompanyProfileForm.php

<?php

namespace App\Http\Livewire;

use App\Models\CompanyProfile;
use Livewire\Component;

class UpdateCompanyProfileForm extends Component
{
    public CompanyProfile $companyProfile;

    protected $rules = [
        'companyProfile.name' => 'required|string',
    ];

    public function mount()
    {
        $this->companyProfile = new CompanyProfile;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        return view('livewire.update-company-profile-form');
    }

    public function save()
    {
        $this->validate();
    }
}

resources/views/livewire/update-company-profile-form.blade.php

<form wire:submit.prevent="save">
    <input
        wire:model.defer="companyProfile.name"        
    />
    @error('companyProfile.name')
        <p>{{ $message }}</p>
    @enderror

</form>

Fix

https://github.com/livewire/livewire/blob/master/src/ComponentConcerns/ValidatesInput.php#L212

protected function shortenModelAttributes($data, $rules, $validator)
    {
        // If a model ($foo) is a property, and the validation rule is
        // "foo.bar", then set the attribute to just "bar", so that
        // the validation message is shortened and more readable.
        foreach ($rules as $key => $value) {
            $propertyName = $this->beforeFirstDot($key);

            if ($data[$propertyName] instanceof Model) {
-                if (str($key)->replace('_', ' ')->is($validator->getDisplayableAttribute($key))) {
+                if (str($key)->snake()->replace('_', ' ')->is($validator->getDisplayableAttribute($key))) {
                    $validator->addCustomAttributes([$key => $validator->getDisplayableAttribute($this->afterFirstDot($key))]);
                }
            }
        }
    }

Context

  • Livewire version: 2.3.2
  • Laravel version: 8.15.0
  • Alpine version: 2.7.3
  • Browser: Chrome 87.0.4280.67
@iksaku
Copy link
Contributor

iksaku commented Nov 23, 2020

If your provided solution does indeed work, would you mind opening a PR and add the necessary tests?

@austenc
Copy link
Collaborator

austenc commented Dec 16, 2020

@JoshMoreno thanks for the suggestion on the fix -- added a PR for it #2229 with a test.

calebporzio added a commit that referenced this issue Dec 22, 2020
fixed property name shortening when camelcased - closes #2079
@squishythejellyfish
Copy link
Collaborator

👋 Oh Hi! I'm Squishy, the friendly jellyfish that manages Livewire issues.

I see this issue has been closed.

Here in the Livewire repo, we have an "issues can be closed guilt-free and without explanation" policy.

If for ANY reason you think this issue hasn't been resolved, PLEASE feel empowered to re-open it.

Re-opening actually helps us track which issues are a priority.

Reply "REOPEN" to this comment and we'll happily re-open it for you!

(More info on this philosophy here: https://twitter.com/calebporzio/status/1321864801295978497)

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