I got strange redirects with Inertiajs errors on data post submitting
With Laravel 9/Inertiajs 3 I have home page with code in control :
return Inertia::render('Frontend/Home/Home');
In modal form (with "vue-final-modal": "^3.4.3") I made User register form as wizard with several steps and when user submit details form
-
I run sendUserRegisterStep1 function with code :
<div class="content frontend_modal_content_editor_form frontend_modal_content_wrapper" v-show="user_register_wizard_step === 1"> ... <input type="text" class="form-control" id="modal_user_register_name" v-model="formUserRegisterEditorStep1.name" maxlength="100"> <div class="fs-error mb-3" v-if="formUserRegisterEditorStep1.errors" :class="{ 'd-block' : formUserRegisterEditorStep1.errors && formUserRegisterEditorStep1.errors.name}"> {{ formUserRegisterEditorStep1.errors.name }} </div> </div> ... function sendUserRegisterStep1() { console.log('sendUserRegisterStep1::') formUserRegisterEditorStep1.value.post(route('user_register_step_1'), { preserveScroll: true, onSuccess: (resp) => { user_register_wizard_step.value = 2 // Open next step form formUserRegisterEditorStep1.value.id = resp.props.user.id // Pass parameter next steps form formUserRegisterEditorStep2.value.user_email = formUserRegisterEditorStep1.value.email formUserRegisterEditorStep2.value.user_id = resp.props.user.id userRegisterAvatarUploader.value.user_id = resp.props.user.id userRegisterAvatarUploader.value.user_email = resp.props.user.email new_registered_user_id.value = resp.props.user.id new_registered_user_email.value = formUserRegisterEditorStep1.value.email Toast.fire({ icon: 'success', title: 'You have registered successfully. Check your email with confirmation code' }) }, onError: (e) => { console.log(e) } })} // sendUserRegisterStep1
and control action :
public function register_step_1(RegisterStep1Request $request)
{
$newUser= null;
try {
// NEW USER CREATION CODE
} catch (QueryException $e) {
...
}
// I suppose lines below raise problems
return Inertia::render('Frontend/Home/Home',
[ 'user'=> $newUser]
);
} // public function register_step_1(RegisterStep1Request $request)
I the method above I can not return response()->json(, As I got error : All Inertia requests must receive a valid Inertia response
when data are valid new user is created but url of my site is changed from home page
http://local-bi-currencies.com/
into post url
http://local-bi-currencies.com/register_step_1
as in routes I have :
Route::post('/register_step_1', [AuthController::class, 'register_step_1'])->name('user_register_step_1');
I think returning lines
return Inertia::render('Frontend/Home/Home',
are invalid, but I do not know which way can be used here?
-
On the second step I have similar form where user must enter confirmation code send by email. Having similar control with custom request :
public function register_step_confirmation_code( UserRegisterConfirmationCodeRequest $request) { ... $user->status= 'A'; // active $user->confirmation_code= null; $user->save(); // And again I need to return some inertia responce... return Inertia::render('Frontend/Home/Home', ['user'=> $user] ); }
when entered data are invalid and UserRegisterConfirmationCodeRequest with custom rulke :
public function rules()
{
$request = request();
return [
'confirmation_code' => 'required|min:8| ' . 'check_valid_confirmation_code:' . $request->confirmation_code . ','
. $request->user_email . ',' . $request->user_id
];
and check_valid_confirmation_code defined as :
Validator::extend('check_valid_confirmation_code', function ($attribute, $value, $parameters, $validator) {
$confirmation_code = $parameters[0] ?? null;
$user_email = $parameters[1] ?? null;
$user_id = $parameters[2] ?? null;
$user = User
::getByEmail($user_email)
->getById($user_id)
->first();
\Log::info(varDump($user, ' -12 check_valid_confirmation_code $user::'));
if (empty($user)) {
return false;
}
\Log::info(varDump(-13, ' -13 check_valid_confirmation_code ::'));
// Checking log file I see that line above was written
if ($user->confirmation_code != $confirmation_code) {
return false;
}
// But line below WAS NOT WRITTEN
\Log::info(varDump(-14, ' -14 check_valid_confirmation_code ::'));
and I get very strange GET request at
http://local-bi-currencies.com/register_step_1
and error as I do not have such GET request in my routes.
I try to use inertia in somewhat uncommon way(At least I did not find methods in docs I cxan use here). Are the some usefull methods?
I know how axios works in such case, but I do not want to use it, as I want to keep laravel validation/errors in my app. I suppose I can not use
{{ formUserRegisterEditorStep1.errors.name }}
with axios request.
How that can be salved ?
Thanks!
Please or to participate in this conversation.