[4.x] Encrypted Eloquent attributes are validated using the raw ciphertext with legacy model binding
Livewire version
v4.4.2
Laravel version
v13.26.1
PHP version
8.5.9
Browser and operating system
Edge on Windows
Describe the issue you're experiencing
After upgrading Livewire from v4.4.1 to v4.4.2, validation of a nested Eloquent model property with an encrypted cast uses the raw encrypted database attribute instead of the decrypted value submitted by the user.
This causes normal validation rules such as max:12 to fail unexpectedly. The issue is especially confusing because reading the property from the model returns the expected decrypted value, and strlen() reports the expected length.
In my case I had a phone number field with [ 'min:12','max:12'] validation rules because this is what I expect.
Im using livewire model binding : appointment.phone_number inside my livewire component. In the model, the phone_number is casted as « encrypted ». Before this version the validation was working but now it always say that the phone number can't have more than 12 characters.
I suspect that the problem has been introduced in this PR but I am not sure : https://github.com/livewire/livewire/pull/10559
Code snippets to reproduce the issue
1. Enable legacy model binding
In config/livewire.php:
<?php
return [
'legacy_model_binding' => true,
];2. Create the model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $fillable = [
'phone',
];
protected function casts(): array
{
return [
'phone' => 'encrypted',
];
}
}3. Create the migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->text('phone')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('contacts');
}
};4. Create the Livewire component
<?php
namespace App\Livewire;
use App\Models\Contact;
use Livewire\Component;
class EncryptedPhoneForm extends Component
{
public Contact $contact;
public function mount(): void
{
$this->contact = Contact::create([
'phone' => null,
]);
}
protected function rules(): array
{
return [
'contact.phone' => [
'nullable',
'min:12',
'max:12',
],
];
}
public function save(): void
{
$this->validate();
}
public function render()
{
return view('livewire.encrypted-phone-form');
}
}5. Create the Blade view
<div>
<form wire:submit="save">
<input
wire:model="contact.phone"
type="text"
placeholder="XXX XXX-XXXX"
>
<button type="submit">Save</button>
</form>
@error('contact.phone')
<p>{{ $message }}</p>
@enderror
</div>6. Add a route
use App\Livewire\EncryptedPhoneForm;
use Illuminate\Support\Facades\Route;
Route::get('/encrypted-phone-form', EncryptedPhoneForm::class);Steps to reproduce
- Install Livewire
v4.4.2. - Run the migration.
- Open
/encrypted-phone-form. - Enter
111 111-1111in the phone field. - Submit the form.
- Observe that the
max:12validation rule fails. - Temporarily downgrade Livewire to
v4.4.1and repeat the same steps. - Observe that the validation passes with
v4.4.1.
Screenshots/screen recordings
No response
How do you expect it to work?
Before the update, the validation was passing since I guess that the not encrypted value was used for validation but now it seems like the encrypted value is used for the validation check.
I find it odd that a minor version upgrade of the framework broke my form validation
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 removed if I haven't met the criteria above.
Source: livewire/livewire