noblemfd
1 month ago

Validating Scores in Laravel

Posted 1 month ago by noblemfd

I am developing a goal-setting application using Laravel-5.8. I have a table called goal_types as shown below:

GoalType

protected $fillable = [
          'goal_type',
          'score'
      ];

GoalTypeController

public function create()
{
   return view('goal_types.create');
}

 public function store(StoreGoalTypeRequest $request)

   $goaltype = GoalType::create([
      'goal_type'              => $request->goal_type,
      'score'                  => $request->score,
  ]);
   Session::flash('success', 'Goal Type is created successfully');
   return redirect()->route('goal_types.index');
}

create.blade.php

   <form  action="{{route('goal_types.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}
                    <div class="form-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3">Goal Type<span style="color:red;">*</span></label>
                                    <div class="col-md-9 controls">
                                        <input  type="text" name="goal_type" placeholder="Enter goal type here" class="form-control" value="{{old('goal_type')}}">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3">Score<span style="color:red;">*</span></label>
                                    <div class="col-md-9 controls">
                                        <input  type="number" name="max_score" placeholder="Enter score here" class="form-control" value="{{old('score')}}" step="0.01">
                                    </div>
                                </div>
                            </div>
                            <!--/span-->

                        </div>
                    </div>
      
        <div>
            &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
            <button type="button" onclick="window.location.href='{{route('goal_types.index')}}'" class="btn btn-default">Cancel</button>
        </div>
    </form>

The field goal_type will consist of these five parameters (Plan1, Plan2, Plan3, Dev, Job), and should be entered one after the other.

For the field score, this is what I want to achieve:

The sum of Plan1, Plan2 and Plan3 should not be more than 75
Dev should be exactly 15 while Job should be exactly 10. So that everything is 100%

How do I validate my code to achieve this?

Thank you.

Please sign in or create an account to participate in this conversation.