0

Trying to get validation error in react component if a form is empty but nothing receiving validation errors

This is function calling on when form is submit

`const handleSubmit = (e) => {
e.preventDefault()
const formData = new FormData()
formData.append('name', values.name)
formData.append('email', values.email)
formData.append('password', values.password)
formData.append('password_confirmation', values.password_conformation)
Inertia.post(base_url+'/users', formData)
}`

Getting errors in component

`const  Create = ({errors}) => {
       // component code
}`

In controller function

` public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
 ]);
 User::create($request->all());
 return redirect()->route('users.index');
}`

How I get validation errors from controller please help.

Trying to get validation error in react component if a form is empty but nothing receiving validation errors

4
  • show the component code Commented Mar 19, 2023 at 18:23
  • Thanks for your response Issue has been resolved after share inertia method add in AppServiceProvider File Commented Mar 20, 2023 at 7:31
  • @GhulamHaider Can you share the code you added to your AppServiceProvider.php file and where, please? Commented Mar 20, 2023 at 17:09
  • Add this function in AppServiceProvider.php boot function Inertia::share([ 'errors' => function () { return Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object) []; }, 'base_url' => env('APP_URL') ]); Commented Mar 22, 2023 at 1:22

1 Answer 1

0

Please, Use this code in your, AppServiceProvider.php (Inside the boot method)

Inertia::share([ 'errors' => function () { return Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object) []; }]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.