shrooq
shrooq

Reputation: 109

Make validate to be Only integer input in laravel

I need to make validate the (request_number) input is the Only integer and show a Message if the student writes a string as an example. the Message if the user write the existing number is (the number of your order exists) and the message if the user writes the non-integer value is (the input should be the Only Number) Now I want to make double validation on (request_number).

this is my store in my controller

     public function store(Request $request)
     {
        $excuse = new Student();
        $excuse->request_number = $request->input('request_number');
        $validatedData = $request->validate([
            'request_number' => Rule::unique('students')->where(function ($query)
            {
              return  $query->whereIn('status_id',[2,4,6,5]);
            })]);

        $excuse->student_id = Auth::guard('web')->user()->id;
        $excuse->status_id = 1;
        $excuse->college_id = Auth::guard('web')->user()->college_id;
        $excuse->save();
        return redirect('/students');
     }

and this is my form to make a request

<form method="post" action="{{url('/student')}}" enctype="multipart/form-data">
@csrf

  <h1> Make order FORM</h1>

  <div class="form-group">
    <label for="requestnumber">   write the Request Number </label><br>
    <input type="text" id="request_number" name="request_number"class="form-control" minlength="5" style="width:50%" required >
  </div>
  @if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li> the number of request is exists </li>
            @endforeach
        </ul>
    </div>
@endif
  <br>
     <button type="submit" class="btn btn-primary">send</button><br>
      </form>

Upvotes: 0

Views: 3957

Answers (3)

Tanvir Ahmed
Tanvir Ahmed

Reputation: 1019

Try replacing your code with this

$status = Student ::whereIn('status_id', [2,3,4,5])->pluck 
('status_id') ;
$validatedData = $request->validate([
            'request_number ' => 
            'required|integer ',
            Rule::unique('students')->where(function ($query) 
    use ($status) {
                return $query->whereIn('status_id', $status);
            })
            ]) ;

and let me know either it is the thing you want or not

Upvotes: 1

lagbox
lagbox

Reputation: 50481

You can have many rules for a single field by using an array of rules:

'request_number' => [
    'integer',
    Rule::unique('students')->where(function ($query) {
        return  $query->whereIn('status_id', [2, 4, 6, 5]);
    }),
]

Upvotes: 0

Mehedi Hassan
Mehedi Hassan

Reputation: 396

$validatedData = $request->validate([
                    'request_number ' => [
                        'required', 'integer', function($attr, $val, $fail) {
                             if (YourTargetModel::where('id', [1,2,3])->count() > 0) {
                                 return $fail("The request number field should be 
                                              unique");
                             }
                    }]
                ]);

Upvotes: 0

Related Questions