0

How can I check old data is available or not after validation from controller? i used this but it shows error

@if(\Illuminate\Http\Request::old('status')!=null)
    <option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
@endif

also used this one

@if(isset(old('status')))
    <option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
@endif

full form is

<form action="{{route('add-expense-store')}}" method="post" class="new-added-form">
    {{ @csrf_field() }}
    <div class="row">
        <div class="col-xl-8 col-lg-8 col-12 form-group">
            <label>Expense Type</label>
            <select class="select2" name="type">
                <option value="">Please Select</option>
                {{--<option value="Teacher's Salary">Teacher's Salary</option>
                <option value="Staff's Salary">Staff's Salary</option>--}}
                <option value="Transport">Transport</option>
                <option value="Mobile Bill">Mobile Bill</option>
                <option value="Utility Bill">Utility Bill</option>
                <option value="Stationary">Stationary</option>
                <option value="Print & Press">Print & Press</option>
                <option value="Photocopy & Compose">Photocopy & Compose</option>
                <option value="Entertainment & Hospitality">Entertainment & Hospitality</option>
                <option value="Donation">Donation</option>
                <option value="Program">Program</option>
                <option value="Personal or Owner">Personal or Owner</option>
            </select>
        </div>
        <div class="col-xl-4 col-lg-4 col-12 form-group">
            <label>Status</label>
            <select class="select2" name="status">
                @if(isset(old('status')))
                    <option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
                @endif
                <option value="">Please Select</option>
                <option value="Paid">Paid</option>
                <option value="Due">Due</option>
            </select>
        </div>
        <div class="col-xl-6 col-lg-6 col-12 form-group">
            <label>Holder Name</label>
            <input type="text" placeholder="" class="form-control" name="holder_name"
                   value="{{ old('holder_name') }}">
        </div>
        <div class="col-xl-6 col-lg-6 col-12 form-group">
            <label>Phone</label>
            <input type="number" placeholder="" class="form-control" name="phone" {{ old('phone') }}>
        </div>
        <div class="col-12 form-group mg-t-8">
            <button type="submit" class="btn-fill-lg btn-gradient-yellow btn-hover-bluedark float-right">Save
            </button>
        </div>
    </div>
</form>

controller

$this->validate($request, [
      'type' => 'required',
      'status' => 'required'
]);

both are getting error. How can I check old value us available or not in blade?

Now I include my form data. here I wants to hidden if old value is not found. please help with this one. check isset old data in blade.

tariqul anik
  • 314
  • 6
  • 24
  • please add controller action which calls render on this view – num8er May 13 '20 at 19:13
  • after validation I put the old value in blade – tariqul anik May 13 '20 at 19:15
  • how You put it? that's why I ask You to show controller method – num8er May 13 '20 at 19:16
  • I edited my question please check – tariqul anik May 13 '20 at 19:17
  • it's not enough! I asked You whole controller method that at the end calls render on view. If You simply call `return view('something')` it's not enough. So please add full controller action body. – num8er May 13 '20 at 19:18
  • old value is called flash input, which exists once and it uses session. so at least You've to have some kind of `session()->flashInput($request->input()); return view('viewname');` – num8er May 13 '20 at 19:20
  • Look at your validation, all you have is required. So if your data is empty and gives your `field is required` then how can you expect old data ? – Akhtar Munir May 13 '20 at 19:21
  • https://laravel.com/docs/7.x/requests#old-input – num8er May 13 '20 at 19:21
  • I just written this for checking validation. if validation fails. it automatically return back with messages to the form. its laravel default validate method – tariqul anik May 13 '20 at 19:22
  • i have 10 fields. just 2 is required here. – tariqul anik May 13 '20 at 19:23
  • so fix Your method that returns it should be like `return redirect()->back()->withInput(Input::all())` – num8er May 13 '20 at 19:23
  • I know but i think you are not picking up my point. If you haven't input any value and it gives you error that this field is required ? then how you can expect old value ? – Akhtar Munir May 13 '20 at 19:23
  • it returns me old input. I just want to check in blade. it exists or not. – tariqul anik May 13 '20 at 19:25
  • So it's Your fault that You don't have 2 separate method handlers (actions), 1st one should show form, another handler request from form and if it's invalid to redirect back to form with old input. So as @AkhtarMunir said validation will not let You pass if it's empty initially. – num8er May 13 '20 at 19:25
  • you didn't got my point. I need to check in blade Request::old('status) is null or not? – tariqul anik May 13 '20 at 19:26
  • @tariqulanik You cannot check old value without passing it as flash value, so please add more controller method code to see whole picture. Your view code is correct, it's `{{ old('status') }}` – num8er May 13 '20 at 19:27
  • Everything is working. old values are also returns after validation . but for Some of my needs I want to check old() value is null or not – tariqul anik May 13 '20 at 19:28
  • first time old value is none. but isset() is not working – tariqul anik May 13 '20 at 19:29
  • fyi check this: https://stackoverflow.com/questions/31081644/how-to-redirect-back-to-form-with-input-laravel-5 thereis an answer with validation – num8er May 13 '20 at 19:30
  • I think should give you the full form. then you will understand – tariqul anik May 13 '20 at 19:31
  • please check now I include the full form from blade – tariqul anik May 13 '20 at 19:36
  • check answer if may help you. – Akhtar Munir May 13 '20 at 19:39

1 Answers1

2

You can check it in this way. Remember to return it in this way from controller

 $validator = $this->validate($request, [
                'type' => 'required',
                'status' => 'required'
              ]);
if ( $validator->fails() ) {
        return back()->withErrors( $validator )->withInput();
    }

In your view do this

<select class="select2" name="status">
   <option value="">Please Select</option>
   <option value="Paid" {{ old('status') == 'Paid' ? 'selected' : '' }}>Paid</option>
   <option value="Due" {{ old('status') == 'Due' ? 'selected' : '' }}>Due</option>
</select>
Akhtar Munir
  • 1,691
  • 3
  • 11
  • 28