0

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>
3
  • the docs will show you have to use multiple rules for a single field Commented Nov 15, 2019 at 19:22
  • as what? how can I do it? Commented Nov 15, 2019 at 19:24
  • laravel.com/docs/6.x/… the part about "Alternatively, validation rules may be specified as arrays of rules instead of a single | delimited string" should be what you want in this particular case Commented Nov 15, 2019 at 19:26

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

14 Comments

I need to add this validate in this $validatedData = $request->validate([ 'request_number' => Rule::unique('students')->where(function ($query) { return $query->whereIn('status_id',[2,4,6,5]); })]); HOW?
I don't want all the numbers will be unique .. only if the status is 2 or 4 or 6 or 5..
what about the message? for non-integer input?
If you wat pass a custom message then you can write in 2nd parameter
I get an error message(syntax error, unexpected ')', expecting ']')
|
0
$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");
                             }
                    }]
                ]);

Comments

0

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]);
    }),
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.