Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MrRobot993's avatar

Form with that edit user profile with avatar don't work

Hi I'm trying to build a form that edit user profile whit a new avatar too but I have some problems to perform a request with avatar. The error in console is: PATCH http://127.0.0.1:8000/profile 500 (Internal Server Error) and the request bag is empty. Thnks

This is a page with form

import InputError from '@/Components/InputError';
import InputLabel from '@/Components/InputLabel';
import PrimaryButton from '@/Components/PrimaryButton';
import TextInput from '@/Components/TextInput';
import FileInput from '@/Components/FileInput';
import { Link, useForm, usePage } from '@inertiajs/inertia-react';
import { Transition } from '@headlessui/react';
import { useState } from 'react';

export default function UpdateProfileInformation({ mustVerifyEmail, status, className }) {
    const user = usePage().props.auth.user;

    const [avatar, setAvatar] = useState("https://ui-avatars.com/api/?name="+user.name+user.surname+"?rounded=true");

    const { data, setData, patch, errors, processing, recentlySuccessful } = useForm({
        name: user.name,
        email: user.email,
        avatar: ''
    });

    const submit = (e) => {
        e.preventDefault();

        patch(route('profile.update'));
    };

    const changeAvatar = (e) => {
        setAvatar(URL.createObjectURL(e.target.files[0]));
        setData('avatar', e.target.files[0]);
    }

    return (
        <section className={className}>
            <header>
                <h2 className="text-lg font-medium text-gray-900">Profile Information</h2>

                <p className="mt-1 text-sm text-gray-600">
                    Update your account's profile information and email address.
                </p>
            </header>

            <form onSubmit={submit} className="mt-6 space-y-6">
                <div>
                    <InputLabel for="name" value="Name" />

                    <TextInput
                        id="name"
                        className="mt-1 block w-full"
                        value={data.name}
                        handleChange={(e) => setData('name', e.target.value)}
                        required
                        autofocus
                        autocomplete="name"
                    />

                    <InputError className="mt-2" message={errors.name} />
                </div>

                <div>
                    <InputLabel for="email" value="Email" />

                    <TextInput
                        id="email"
                        type="email"
                        className="mt-1 block w-full"
                        value={data.email}
                        handleChange={(e) => setData('email', e.target.value)}
                        required
                        autocomplete="email"
                    />

                    <InputError className="mt-2" message={errors.email} />
                </div>

                <div className='flex flex-col md:flex-row items-center'>
                    <div>
                        <InputLabel for="avatar" value="Avatar" />
                        <FileInput
                            id="avatar"
                            type="file"
                            className="mt-1 block w-full"
                            handleChange={changeAvatar}
                            required
                        />
                        <InputError className="mt-2" message={errors.email} />
                    </div>

                    <div className="avatar-cont mt-6 md:mt-0 py-2">
                        <img
                            className="h-20 w-20 rounded-full"
                            src={avatar}
                            alt=""
                        />
                    </div>
                </div>

                {mustVerifyEmail && user.email_verified_at === null && (
                    <div>
                        <p className="text-sm mt-2 text-gray-800">
                            Your email address is unverified.
                            <Link
                                href={route('verification.send')}
                                method="post"
                                as="button"
                                className="underline text-sm text-gray-600 hover:text-gray-900 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                            >
                                Click here to re-send the verification email.
                            </Link>
                        </p>

                        {status === 'verification-link-sent' && (
                            <div className="mt-2 font-medium text-sm text-green-600">
                                A new verification link has been sent to your email address.
                            </div>
                        )}
                    </div>
                )}

                <div className="flex items-center gap-4">
                    <PrimaryButton processing={processing}>Save</PrimaryButton>

                    <Transition
                        show={recentlySuccessful}
                        enterFrom="opacity-0"
                        leaveTo="opacity-0"
                        className="transition ease-in-out"
                    >
                        <p className="text-sm text-gray-600">Saved.</p>
                    </Transition>
                </div>
            </form>
        </section>
    );
}

This is a input file component

import { forwardRef, useEffect, useRef } from 'react';

export default function FileInput( { type = 'file', name, id, className, required, handleChange }) {

    return (
        <div className="flex flex-col items-start">
            <input
                type={type}
                name={name}
                id={id}
                className={
                    `border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm ` +
                    className
                }
                required={required}
                onChange={(e) => handleChange(e)}
            />
        </div>
    )
}
0 likes
0 replies

Please or to participate in this conversation.