Error 500 when submitting a form with validation in ajax and fortify Aravel

Asked

Viewed 81 times

0

I am trying to create a simple registration form in fortify Aravel and using validation with ajax. The form when it is submitted inserts the values into the database as intended, but ends up not redirect to the user page after this registration, as intended, and this appears error I don’t know how to fix: "Failed to load Resource: the server responded with a status of 500 (Internal Server Error)"

The form:

<form class="multisteps-form__form" id="wizard" method="POST" action="{{route('register')}}">
    @csrf
    <div class="step-content-field">    
    <div class="form-inner-area">
        <label>First Name:</label>
        <input type="text" name="firstname" id="firstname" class="form-control "  placeholder="First Name" required>
        <div class="text-danger error" data-error="firstname"></div>
    </div>
    <div class="form-inner-area">
        <label>Last Name:</label>
        <input type="text" name="lastname" id="lastname" class="form-control "  placeholder="Last Name" required>
        <div class="text-danger error" data-error="lastname"></div>
    </div>
    <div class="form-inner-area">
        <label>Email:</label>
        <input type="text" name="email" class="form-control "  id="email" placeholder="Email" required>
        <div class="text-danger error" data-error="email"></div>
    </div>
    <div class="form-inner-area">
        <label>Mobile:</label>
        <input type="text" name="mobile" class="form-control " id="mobile" placeholder="Mobile" required>
        <div class="text-danger error" data-error="mobile"></div>
    </div>
    <div class="form-inner-area">
        <label>Country:</label>
        <input type="text" name="country" class="form-control "  id="country" placeholder="Country" required>
        <div class="text-danger error" data-error="country"></div>
    </div>
    <div class="form-inner-area">
        <label>Password:</label>
        <input type="password" name="password" class="form-control "  id="password" placeholder="Password" required>
    </div>
    <div class="form-inner-area">
        <label>Confirm password:</label>
        <input type="password" name="password_confirmation" class="form-control"  id="password_confirm" placeholder="Confirm password" required>
        
    </div>
    <span>The password must be at least 8 characters and contain at least one uppercase character, one number, and one special character.</span>
    </div>
   <div class="actions">
      <ul>
       <li><span class="js-btn-prev" title="BACK"><i class="fa fa-arrow- 
       left"></i> BACK </span></li>
      <li><button type="submit" title="SUBMIT">SUBMIT <i class="fa fa- 
      arrow-right"></i></button></li>
       </ul>
    </div>
</form>

The script in ajax:

<script>
    $('#wizard').on('submit', function(e){
    e.preventDefault();
    $('.error').html('');
    $.ajax({
    url: $(this).attr('action'),
    method: $(this).attr('method'),
    data: $(this).serialize(),
    dataType: 'json',
    success(response)
    {
    console.log('User created successfully.');
    },
    error(error)
    {
    let errors = error.responseJSON.errors
    for(let key in errors)
    {
        let errorDiv = $(`.error[data-error="${key}"]`);
        if(errorDiv.length )
        {
            errorDiv.text(errors[key][0]);
        }
        }
    }
});
});
    </script>

And the controller Createnewuser of fortify:

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    public function create(array $input)
    {
        Validator::make($input, [
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                Rule::unique(User::class),
            ],
            'mobile' => ['required', 'int'],
            'country' => ['required', 'string', 'max:255'],
            'password' => $this->passwordRules(),
        ])->validate();

        $user = User::create([
            'firstname' => $input['firstname'],
            'lastname' => $input['lastname'],
            'email' => $input['email'],
            'mobile' => $input['mobile'],
            'country' => $input['email'],
            'password' => Hash::make($input['password']),
            
        ]);
            $user->notify(new WelcomeEmailNotification());

            return response()->json(['success' => true]);
    }
}

  • and where is the code that redirects?

  • Because you are creating, it may be notify with some problem or return.. I suggest you go to the logs in 'Storage/logs' and see if there are any errors there.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.