duanpu8830 2019-06-10 12:11
浏览 218

如何在laravel中重定向并显示错误验证

good day, I new in laravel Framework and I face this two problems : -
first one
I want to redirect to my page after 2 seconds automatically.
the second one
I make custom function call (is exist ) if this function returns true data I want to print "name exist before " but the problem here is form was rested when this function returns true and print message. how to prevent form resetting from inputs value? here is my code

controller code

enter code here
public function add(Request $request)

{
        // start add
    if($request->isMethod('post'))
    {
        if(isset($_POST['add']))
        {
            // start validatio array
        $validationarray=$this->validate($request,[
            //'name'  =>'required|max:25|min:1|unique:mysql2.products,name|alpha',
            'name'  =>'required|alpha',
            'price' =>'required|numeric',

            ]);
                // check name is exist

            if(true !=dBHelper::isExist('mysql2','products','`status`=? AND `deleted` =? AND `name`=?',array(1,1,$validationarray['name'])))
            {

                $product=new productModel();
                // start add
                $product->name=$request->input('name');
                $product->save();
                $add=$product->id;
                $poducten=new productEnModel();
                $poducten->id_product=$add;
                $poducten->name=$request->input('name');
                $poducten->price=$request->input('price');
                $poducten->save();
                $dataview['message']='data addes';




            }else{
                $dataview['message']='name is exist before';
            }

        }
    }
    $dataview['pagetitle']="add product geka";
    return view('productss.add',$dataview);
}

this is my routes

Route::get('/products/add',"produtController@add");
    Route::post('/products/add',"produtController@add");

this is my view

@extends('layout.header')
@section('content')
    @if(isset($message))
        {{$message}}
    @endif

    @if(count($errors)>0)
        <div class="alert alert-danger">
            <ul>
              @foreach($errors->all() as $error)
                  <li>{{$error}}</li>

                  @endforeach

            </ul>

        </div>
        @endif

    <form role="form"  action="add" method="post" enctype="multipart/form-data">
        {{csrf_field()}}
        <div class="box-body">
            <div class="form-group{{$errors->has('name')?'has-error':''}}">
                <label for="exampleInputEmail1">Employee Name</label>
                <input type="text" name="name" value="{{Request::old('name')}}" class="form-control" id="" placeholder="Enter Employee Name">
            </div>

            <div class="form-group">
                <label for="exampleInputEmail1">Email Address</label>
                <input type="text" name="price" value="{{Request::old('price')}}" class="form-control" id="" placeholder="Enter Employee Email Address">
            </div>
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <button type="submit" name="add" class="btn btn-primary">Add</button>
        </div>
    </form>

@endsection
  • 写回答

3条回答 默认 最新

  • duanbogan5878 2019-06-10 12:20
    关注

    I hope that I understood your question. Instead of using {{ Request::old('price') }} use {{ old('price') }} This should retrieve the form data after page was reloaded.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?