Blear's avatar
Level 1

Why don't error messages show up?

Why don't error messages show up? This is my function with validation and blade template .After pressing the button, if some input is incorrect, nothing happens instead of showing a message.

 public function save()
{
    // Validate the request data
    $validated = $this->validate([ 
        'street' => 'required|string',
        'country' => 'required|string',
        'zip_code' => 'required|string|regex:/^\d{2}-\d{3}$/',
        'post_office' => 'required|string',
        'building_number'=> 'required',
        'apartment_number' => 'required',
        'commune' => 'required|string',
        'district' => 'required|string',
        'voivodeship' => 'required|string',
        'branch_id' => 'required',
        'is_main_branch' =>'required'
    ]);
    if ($this->id) {
        $address = Address::find($this->id);
        $address->update($validated);
       
    } else {
        // If id is not set, create a new address
        Address::create([
            'street' => $validated['street'],
            'country' => $validated['country'],
            'zip_code' => $validated['zip_code'],
            'post_office' => $validated['post_office'],
            'building_number' => $validated['building_number'],
            'apartment_number' => $validated['apartment_number'],
            'commune' => $validated['commune'],
            'district' => $validated['district'],
            'voivodeship' => $validated['voivodeship'],
            'branch_id' => $validated['branch_id'],
            'is_main_branch' => $this->is_main_branch, // Assuming $this->is_main_branch contains the value
        ]);
    }

    $this->updateData();
    $this->dispatch('closeModal')->to(Table::class);
    $this->resetForm();
}


 <form class="space-y-4">
    @csrf

    <div class="flex space-x-4">
        <div class="flex-1">
            <label for="country" class="block text-sm font-medium text-gray-700">{{ __('Country')}}</label>
            <input wire:model="$parent.country" type="text" name="country" id="country" class="mt-1 p-2 w-full border rounded-md input-xs">
            @error('$parent.country')
                <div class="text-red-500">Puste</div>
            @enderror
        </div>
    </div>

    <div class="flex space-x-4">
        <div class="flex-1">
            <label for="street" class="block text-sm font-medium text-gray-700">{{ __('Street')}}</label>
            <input wire:model="$parent.street" type="text" name="street" id="street" class="mt-1 p-2 w-full border rounded-md input-xs" required>
            @error('$parent.street')
                <div class="alert alert-danger">{{ $message }}</div>
            @enderror
        </div>
        <div class="flex-1">
            <label for="building_number" class="block text-sm font-medium text-gray-700">{{ __('Building number')}}</label>
            <input wire:model="$parent.building_number" type="text" name="building_number" id="building_number" class="mt-1 p-2 w-full border rounded-md input-xs" required>
        </div>
        <div class="flex-1">
            <label for="apartment_number" class="block text-sm font-medium text-gray-700">{{ __('Apartment')}}</label>
            <input wire:model="$parent.apartment_number" type="text" name="apartment_number" id="apartment_number" class="mt-1 p-2 w-full border rounded-md input-xs" required>
        </div>
    </div>

    <div class="flex space-x-4">
        <div class="flex-1">
            <label for="post_office" class="block text-sm font-medium text-gray-700">{{ __('Post Office')}}</label>
            <input wire:model="$parent.post_office" type="text" name="post_office" id="post_office" class="mt-1 p-2 w-full border rounded-md input-xs" required>
        </div>
        <div class="flex-1">
            <label for="zip_code" class="block text-sm font-medium text-gray-700">{{ __('Zip Code')}}</label>
            <input wire:model="$parent.zip_code" type="text" name="zip_code" id="zip_code" class="mt-1 p-2 w-full border rounded-md input-xs" required>
            @error('$parent.zip_code')
                <div class="alert alert-danger">{{ $parent.message }}</div>
            @enderror
        </div>
    </div>

    <div class="flex space-x-4">
        <div class="flex-1">
            <label for="commune" class="block text-sm font-medium text-gray-700">{{ __('Commune')}}</label>
            <input wire:model="$parent.commune" type="text" name="commune" id="commune" class="mt-1 p-2 w-full border rounded-md input-xs" required>
        </div>
        <div class="flex-1">
            <label for="district" class="block text-sm font-medium text-gray-700">{{ __('District')}}</label>
            <input wire:model="$parent.district" type="text" name="district" id="district" class="mt-1 p-2 w-full border rounded-md input-xs" required>
        </div>
    </div>

    <div class="flex space-x-4">
        <div class="flex-1">
            <label for="voivodeship" class="block text-sm font-medium text-gray-700">{{ __('Voivodeship')}}</label>
            <input wire:model="$parent.voivodeship" type="text" name="voivodeship" id="voivodeship" class="mt-1 p-2 w-full border rounded-md input-xs" required>
        </div>
    </div>

    <div class="flex space-x-4">
        <div class="flex-1">
            <label for="branch_id" class="block text-sm font-medium text-gray-700">{{ __('Branch ID')}}</label>
            <select wire:model="$parent.branch_id" name="branch_id" id="branch_id" class="mt-1 p-2 w-full border rounded-md" required>
                <option wire:model="$parent.branch_id">Wybierz gałąź</option>
                @foreach($branch as $index => $option)
                    <option value="{{ $option['id'] }}">{{ $option['name'] }}</option>
                @endforeach
            </select>
        </div>
        <div class="flex-1">
            <label for="is_main_branch" class="block text-sm font-medium text-gray-700">{{ __('Is Main Branch')}}</label>
            <input wire:model.boolean="$parent.is_main_branch" type="checkbox">
        </div>
    </div>
        <button type="submit" wire:click.prevent="$parent.save" class="btn btn-primary text-white mx-2">
            @if(isset($id)) {{ __('Update')}} @else {{ __('Add')}} @endif</button>

</form>
0 likes
4 replies
gych's avatar

Add {{ $errors->all() }} to your view for testing purposes and see if this contains any errors.

Blear's avatar
Level 1

@gych It doesn't show anything

 @foreach($errors->all() as $error)
                {{  $error }} // this will work
        @endforeach
gych's avatar

@Blear So the code that you included in this last reply shows the actual errors?

Have you tried using for example @error('zip_code') instead of @error('$parent.zip_code')

Blear's avatar
Level 1

@gych I mean code that i included dont show anything but also dont say that $errors->all() are undefined. Yeah I tried this, also i tried switching wire.click to wire.submit but this solution meant that in the case of an incorrect form, the entire modal blocked and nothing could be done except turning it off

Please or to participate in this conversation.