1

Validation in Laravel

 1 year ago
source link: https://aungkoman.github.io/howto/2023/05/30/laravel-validation.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Validation in Laravel – – Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.

Validation in Laravel

ကိုယ် Validate လုပ်ချင်တဲ့ Request ကို Class တစ်ခု ဆောက်မယ်။

php artisan make:request LoginRequest

/app/http/request မှာ ဖိုင်အသစ်ရောက်လာမယ်။

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class LoginRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    // for this request validation I don’t need to authorised user so I remove “authorize()” function from the file.
    /*
    public function authorize()
    {
        return false;
    }
    */

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        // Rule တွေ Add
        return [
            'email' => 'required|email|exists:users,email|max:50',
            'password' => 'required|min:6'
        ];
    }

    // Failed ခဲ့ရင် ဘာလုပ်ရမယ်ဆိုတဲ့ function ထည့်
    public function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            'success'   => false,
            'message'   => 'Validation errors',
            'data'      => $validator->errors()
        ]));
    }

}

သက်ဆိုင်ရာ Controller မှာ ဒီ Request ကို import လုပ်။

use App\Http\Requests\LoginRequest;

Ref; https://medium.com/@sgandhi132/how-to-validate-an-api-request-in-laravel-35b46470ba88

Written on May 30, 2023

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK