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

demonz's avatar

How do I show error messages instead of the code

hello , I am using laravel breeze with react,inertiajs. and I need to show error messages and not the code of the website when error happens , as far I understand there're validation errors for the inputs , and other errors like when instance cannot be find . for validation error i see this const errors from useForm hook

   const { data, setData, post, delete: destroy, processing, errors, reset } = useForm({

how do you guys make error messages ? for example when i write CourseStoreRequest containing rules , how do i put error message for each rule so the user understand what he typed wrong .

and for other errors , how do I handle them so no one can see website code ? please help with these so i can finally deploy my website thanks

0 likes
2 replies
LaryAI's avatar
Level 58

For validation errors, you can use the errors object from the useForm hook to display the error messages. You can access the errors for each field by using the field name as the key. For example, if you have a field called name, you can access the error message for that field by using errors.name.

You can also customize the error messages for each field by passing an errorMessages object to the useForm hook. This object should contain the field name as the key and the error message as the value. For example:

const { data, setData, post, delete: destroy, processing, errors, reset } = useForm({
    errorMessages: {
        name: 'Please enter a valid name',
    }
});

For other errors, you can use the try/catch block to catch any errors that occur and display a custom error message. For example:

try {
    // Your code here
} catch (error) {
    console.log(error);
    alert('An error occurred. Please try again.');
}
eggplantSword's avatar

You mean validation errors from the backend? You could create a FormRequest or validate the request in the controller method, that should return any validation errors to the front where you just display them. I don't use react but I'm sure it's similar as vue, in vue I would do something like {{$page.props.errors.attribute}} (where attribute is the field like name) to see the error.

Please or to participate in this conversation.