Lewati ke konten
Rama's logo Qisthi Ramadhani
Go back

Rebuilding Laravel's `old()` and `@error` Magic in Hypervel — The Real Way It Works

Laravel developers are used to things “just working” — form validation, error messages, and input retention all feel seamless. But when you start building with Hypervel — a Laravel-inspired, coroutine-native PHP framework — you’ll quickly realize that this magic needs a manual touch.

In this guide, I’ll show you how I fully recreated Laravel’s form UX in Hypervel, with working validation, session flashing, and Blade rendering — and zero Laravel internals.


🧩 What Laravel Does Silently

In Laravel, when you do:

$request->validate([
    'email' => 'required|email',
]);

It:

In Hypervel, you get control, not magic — so we’ll build it all step-by-step.


✅ Final Working Hypervel Setup

📂 LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use Hypervel\Http\Request;
use Hypervel\Support\Facades\Auth;
use Hypervel\Support\Facades\Validator;
use Psr\Http\Message\ResponseInterface;
use Hypervel\Support\MessageBag;

class LoginController
{
    public function show(): ResponseInterface
    {
        $errors = $this->errors();
        $old = $this->old();

        return view('auth.login', compact('errors', 'old'));
    }

    public function store(Request $request): ResponseInterface
    {
        $validator = Validator::make($request->all(), [
            'email' => ['required', 'email', 'exists:users,email'],
            'password' => ['required', 'min:2'],
        ]);

        if ($validator->fails()) {
            session()->flash('_errors', $validator->errors());
            session()->flash('_old_input', $request->all());

            return response()->redirect(url()->previous());
        }

        if (Auth::attempt($validator->validated())) {
            $request->session()->regenerate();
            return response()->redirect('/dashboard');
        }

        session()->flash('_errors', ['email' => ['Invalid credentials.']]);
        session()->flash('_old_input', $request->all());

        return response()->redirect(url()->previous());
    }

    private function errors(): MessageBag
    {
        $errors = session()->get('_errors', []);
        return $errors instanceof MessageBag ? $errors : new MessageBag($errors);
    }

    private function old(): array
    {
        return session()->get('_old_input', []);
    }
}

🧾 Blade View Snippet

<input type="email" name="email" value="{{ $old['email'] ?? '' }}">
@if ($errors->has('email'))
    <p class="text-red-500">{{ implode(', ', $errors->get('email')) }}</p>
@endif

You can build rich Tailwind layouts, password visibility toggles, and dynamic validations — all while still manually controlling the state via $old and $errors.


🧠 Key Takeaways

FeatureLaravelHypervel
old()Helper with session bindingManual: session()->get('_old_input')
@error()Blade directiveUse $errors->has() and get()
validate()Auto-redirects on failureUse Validator::make() and flash
MessageBagAuto-injectedMust be constructed manually

🛠 Tips for Further Optimization


Share this post on:
LLM-friendly version:
Open in ChatGPT Open in Claude

Related Posts


Previous Post
The Black Box Effect: How to Learn New Skills Fast as a Full Stack Programmer
Next Post
Step-by-Step Guide: Creating a Web Authentication System in Hypervel