Search code examples
phplaravelvalidationlumen

Lumen Custom Validation


I am trying to implement a custom validation rule within lumen and I am following the docs for lumen 5.6. It says to refer to laravel validation to see how to use the validation. I am currently trying to make a validation to check if the value is a true null or not. So $x === "" would mean it fails Here is my rule located in App\Rules folder I created.

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class TrueNull implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if($value === "") {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute cannot be an empty string.';
    }
}

I copied this straight from lumen docs and make my modification to the passes function. Within my modal have

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use App\Rules\TrueNull;
use Validator;

Then

public function validate($data)
{
    // make a new validator object
    $v = Validator::make($data, 
    [
        'x' => ['regex:/^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$/', new TrueNull]
    ]
}

But the validation for TrueNull never happens am I missing a connection or something its really frustrating because the docs says this should work. Here is my controller calling the update I am validating.

public function update(Request $request, $id)
    {
        /*
            In middleware need to add community id to request.
        */
        try {
            $site = Site::findOrFail($id);

            if ($site->validate($request->all())) {
                $site->fill($request->all());

                // save
                $site->save();
            } else {
                return response()->json($site->errors(), 422);
            }
        } catch (Exception $e) {
            return response()->json($e, 422);
        }

        return response()->json($site, 200);
    }

Solution

  • For future reference I found a random snippet of code that offset the basic docs of Lumen. In my class for TrueNull instead of implements Rule I changed this to implements ImplicitRule and changed the use to \ImplicitRule and it is now catching that "" is not a null.

    <?php
    
    namespace App\Rules;
    
    use Illuminate\Contracts\Validation\ImplicitRule;
    
    class TrueNull implements ImplicitRule
    {
        /**
         * Determine if the validation rule passes.
         *
         * @param  string  $attribute
         * @param  mixed  $value
         * @return bool
         */
        public function passes($attribute, $value)
        {
            if($value === "") {
                return false;
            } else {
                return true;
            }
        }
    
        /**
         * Get the validation error message.
         *
         * @return string
         */
        public function message()
        {
            return 'The :attribute cannot be an empty string.';
        }
    }