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

rvp04's avatar
Level 1

Laravel form validation with previous input value

i have two input box which takes numbers as input.

textbox1: Number students appeared for test (textbox name : text1appeared) textbox2: number of student passed (textbox name : text1passed)

i need to add validation that passed should not be greater than appeared.

Please help me to do the validation.

$validator = Validator::make($request->all(), [
                'test1appeared' => 'required|numeric|between:0,100',
                'test1passed' => 'required|numeric|between:0,'test1appeared',
            ]);
0 likes
8 replies
Vilfago's avatar
Vilfago
Best Answer
Level 20
$test1appeared = $request->input('test1appeared');

$validator = Validator::make($request->all(), [
                'test1appeared' => 'required|numeric|between:0,100',
                'test1passed' => 'required|numeric|between:0,'.$test1appeared',
            ]);
2 likes
rvp04's avatar
Level 1

@Vilfago how can i use the same thing if i use Request

For example

php artisan make:request FormValidation.

I have defined all the validation. But how to the above validation.

please help me bro.

Cronix's avatar

You should be able to do the same thing in your rules() using the request() helper (which is available globally).

'test1passed' => 'required|numeric|between:0,'. request('test1appeared'),
rvp04's avatar
Level 1

@Cronix I have form request for add and update method.

Errors showing correctly in when validation fails in add method, but in update method errors are not showing.

Please help me.

Edit function call to show data for edit
public function edit_Academic(Request $request, $id = null)
    {
        $Academic = PartA_Academic::where(['id' => $id])->firstOrFail();
        $AcademicYearMapping = AcademicYearMapping::where(['id' => $Academic->academicyear_mapping_id])->firstOrFail();

        if (Gate::allows('update-record', $AcademicYearMapping)) {
            $semesters = Semester::where(['status_id' => 1])->get();
            $AcademicData = PartA_Academic::where(['id' => $id])->first();
            return view('users.faculty.assessmentforms.parta_academic_edit')->with(compact('semesters', 'AcademicData'));
        } else {
            return view('errors.notallowed');
        }
    }

This function to update the edited record.  Here form validation redirect to edit page not with old values

 public function updateAcademicDetails(FormValidation $request, $id = null)
    {
        $Academic = PartA_Academic::where(['id' => $id])->firstOrFail();
        $AcademicYearMapping = AcademicYearMapping::where(['id' => $Academic->academicyear_mapping_id])->firstOrFail();

        if (Gate::allows('update-record', $AcademicYearMapping)) {

            if ($request->isMethod('post')) {
                $data = $request->all();
                PartA_Academic::where(['id' => $id])->update([
                    "semester_id" => $data['semester'],
                    "subjectname" => $data['subjectname'],
                    "test1_appeared" => $data['test1appeared'],
                    "test1_passed" => $data['test1passed'],
                    "test2_appeared" => $data['test2appeared'],
                    "test2_passed" => $data['test2passed'],
                    "model_appeared" => $data['modelappeared'],
                    "model_passed" => $data['modelpassed'],
                    "test_percentage" => $data['modelpassed'],
                    "feedback_percentage" => $data['feedback'],
                    "university_percentage" => $data['university']
                ]);
                return redirect('/users/faculty/assessmentforms/parta_academic')->with('success', 'Academic Data Updated Successfully');
            }
        } else {
            return view('errors.notallowed');
        }
Cronix's avatar

Here, you're not using the FormValidation request object

public function edit_Academic(Request $request, $id = null)

like you are here

public function updateAcademicDetails(FormValidation $request, $id = null)

So it's not even validating anything in the edit_Academic method.

Cronix's avatar

If that's not it, you'd need to post your FormValidation class where the actual validation is taking place.

Please or to participate in this conversation.