Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vidhyaprakash85's avatar

Laravel Custom Validation Rule

I have livewire component. In the rules need to create rule where i need to pass the value and validate. LiveWire

<?php

namespace App\Http\Livewire\Teacher;

use Livewire\Component;
use App\Models\InternalMark;
use App\Models\AssignTeacher;
use App\Models\InternalMarkEntry as IntenalMarkEntryModel;
use App\Models\MasterTables\InternalMarkEntryNo;
use App\Rules\Teacher\SecuredMarkRule;

class InternalMarkEntry extends Component
{
    public bool $showStudents = false;
    public $regulation;
    public $batch;
    public $section;
    public $programme;
    public $semester;
    public $entry;
    public $entries;
    public $subject;
    public $defaultTotalHour;
    public $defaultMaxMark;

    public $internalmarks;
    public $internalmarksentries;

    public AssignTeacher $assignteacher;

    public function rules()
    {
        return [
            'entry' => 'required',
            'internalmarksentries.*.total_hours' =>     ['nullable', 'integer'],
            'internalmarksentries.*.attended_hours' =>  ['nullable', 'regex:/^(100|[1-9][0-9]?|0)$|^A$/', 'lte:internalmarksentries.*.total_hours'],
            'internalmarksentries.*.max_mark' =>        ['nullable', 'integer'],
            'internalmarksentries.*.secured_mark' =>    ['nullable', new SecuredMarkRule()],
        ];
    }

    protected $messages = [
        'internalmarksentries.*.total_hours.nullable' => 'Total Hours is required',
        'internalmarksentries.*.total_hours.integer' => 'Must be Integer',
        'internalmarksentries.*.attended_hours.nullable' => 'Total Hours is required.',
        'internalmarksentries.*.attended_hours.regex' => 'A for Absent',
        'internalmarksentries.*.attended_hours.lte' => 'Less than or Equal to Total Hours',
        'internalmarksentries.*.max_mark.nullable' => 'Max Mark is required',
        'internalmarksentries.*.max_mark.integer' => 'Must be Integer',
        'internalmarksentries.*.secured_mark.nullable' => 'Secured Mark is required.',
        'internalmarksentries.*.secured_mark.regex' => 'A for Absent',
    ];
}

SecuredMarkRule

<?php

namespace App\Rules\Teacher;

use Illuminate\Contracts\Validation\Rule;

class SecuredMarkRule implements Rule
{
    public $MaxMark;
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return ($value <= $this->MaxMark);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Less :attribute than or equal to Max Mark';
    }
}

In this rule i need to send the max mark to compare. How to do that. Kindly help me.

https://ibb.co/Sd8PQBx

0 likes
17 replies
vincent15000's avatar

You probably need to initialize the variable in the constructor.

public function __construct($MaxMark)
{
	$this->MaxMark = $MaxMark;
}

And then when you use the rule, you pass it the max mark as a parameter.

vidhyaprakash85's avatar

@vincent15000 But how to send the value from livewire to rule. That too i have array of values.

'internalmarksentries.*.secured_mark' =>    ['nullable', new SecuredMarkRule(''internalmarksentries.*.max_mark')],

if i send like this, it is not working.

And i also tried like this

public function passes($attribute, $value)
    {
        $index = explode('.', $attribute)[1];
        $this->MaxMark = request()->input("internalmarksentries.{$index}.max_mark");
    }
1 like
vincent15000's avatar

@vidhyaprakash85

Assuming the max mark is the default max mark, you can try this.

public $defaultMaxMark;
...
'internalmarksentries.*.secured_mark' =>    ['nullable', new SecuredMarkRule($this->defaultMaxMark)]

And sure initialize the max mark value inside the constructor of the rule class.

vidhyaprakash85's avatar

@vincent15000 I want to check each and every secured mark should not be more than max mark and one alphabet A is allowed if student is absent.

This is what i want to achieve. For this i created a separated rule and i want to check the secured mark is not exceeding max mark.

https://ibb.co/Sd8PQBx

see this link sir, you will able to identify what i want to achieve.

1 like
vincent15000's avatar

@vidhyaprakash85 What's the result when you try this ?

'internalmarksentries.*.secured_mark' =>    ['nullable', new SecuredMarkRule($this->defaultMaxMark)]

You just answered Sometimes it may not be thats the problem., but this doesn't help me to know the result. This should work, but I don't know the content of the $defaultMaxMark for example. To be able to help you, I need the maximum of informations related to your problem.

vidhyaprakash85's avatar

@vincent15000 Problem default mark sometimes it will be given sometime it will not be. So it is always better to compare the values from the input and compare.

1 like
azimidev's avatar

To pass the MaxMark value to the SecuredMarkRule class, you can modify its constructor to accept the MaxMark parameter and set it to a class property. Then, you can pass the MaxMark value from the InternalMarkEntry component to the rule using Livewire's validation sometimes method.

// InternalMarkEntry.php

use App\Rules\Teacher\SecuredMarkRule;

class InternalMarkEntry extends Component
{
    // ...

    public function rules()
    {
        return [
            // ...
            'internalmarksentries.*.secured_mark' => ['nullable', function ($attribute, $value, $fail) {
                $maxMark = $this->defaultMaxMark; // set the max mark value
                $rule = new SecuredMarkRule($maxMark); // create the rule instance with the max mark parameter
                $rule->passes($attribute, $value) ?: $fail($rule->message());
            }],
        ];
    }
}
// SecuredMarkRule.php

class SecuredMarkRule implements Rule
{
    public $MaxMark;

    public function __construct($maxMark)
    {
        $this->MaxMark = $maxMark;
    }

    // ...
}
2 likes

Please or to participate in this conversation.