PHP code example of hasan-22 / generate_validation

1. Go to this page and download the library: Download hasan-22/generate_validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hasan-22 / generate_validation example snippets




use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->string('title')->unique();
            $table->text('content');
            $table->string('image');
            $table->enum('status', ['draft', 'published', 'archived'])->default('draft');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('posts');
    }
};





namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //
}




namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
 * Class PostRequest
 *
 * This Form Request class is used to validate incoming requests for the
 * Post model. It separates validation rules for "store" (create)
 * and "update" operations, ensuring data integrity.
 *
 * @package App\Http\Requests
 */
class PostRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * In most cases, this should return true if the user is authenticated.
     * You can add custom authorization logic here if needed.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * This method dynamically returns a different set of rules based on the
     * HTTP method (POST for store, PUT/PATCH for update).
     *
     * @return array
     */
    public function rules(): array
    {
        return $this->isMethod('POST') ? $this->forStore() : $this->forUpdate();
    }

    /**
     * Get the validation rules for a "store" (create) request.
     *
     * These rules are applied when creating a new Post resource.
     *
     * @return array
     */
    private function forStore(): array
    {
        return [
            // Generated rules for a new resource will be injected here.
            'user_id' => ['



namespace App\Http\Controllers;

use App\Http\Requests\PostRequest;
use App\Models\Post;

class PostController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  PostRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PostRequest $request)
    {
        Post::create($request->all());

        return response()->json(['message' => 'Post created successfully!']);
    }
    
    /**
     * Update the specified resource in storage.
     *
     * @param  PostRequest  $request
     * @param  Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(PostRequest $request, Post $post)
    {
        $post->update($request->all());

        return response()->json(['message' => 'Post updated successfully!']);
    }
}

return [
    'custom_rules' => [
        // Model => [ column => rules... ]
        'Post' => [
            'title' => ['red', 'email', 'unique:users,email'],
            'password' => ['

use GenerateValidation\Strategies\BlobRuleGenerator;

// Set the maximum file size (in kilobytes)
BlobRuleGenerator::setMaxSize(5120); // 5MB

// This adds to the list of possible names used for images.
// or example, if you have a column that stores an image and its name is `my_thumbnail`, you need to add this name so it can be recognized. 
BlobRuleGenerator::addImageName('my_thumbnail');

// Add additional MIME types to the existing list
BlobRuleGenerator::setMimes(['webp', 'avif'], 'merge');

// Or replace the MIME types entirely
BlobRuleGenerator::setMimes(['webp', 'avif'], 'replace');
bash
php artisan vendor:publish --tag=generate_validation