簡體   English   中英

如何在laravel中重定向並顯示錯誤驗證

[英]how to redirect and show error validateion in laravel

美好的一天,我是laravel Framework的新人,我面對這兩個問題: -
第一
我想在2秒后自動重定向到我的頁面。
第二個
我做自定義函數調用(存在)如果這個函數返回真正的數據我想打印“名稱存在之前”但這里的問題是表格被休息時此函數返回true和打印消息。 如何防止表單重置輸入值? 這是我的代碼

控制器代碼

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

這是我的路線

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

這是我的看法

@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

我希望我理解你的問題。 而不是使用{{Request :: old('price')}}使用{{old('price')}}這應該在重新加載頁面后檢索表單數據。

在視圖頁面中嘗試以下代碼以顯示錯誤

   $validator = Validator::make($params, $req_params);
    if ($validator->fails()) {
      $errors = $validator->errors()->toArray();
      return Redirect::to($web_view_path)->with('errors', $errors);
    }

您想要自動重定向到另一個頁面使用ajax提交表單並在settimeout menthod下面使用。

setTimeout(function(){  // Here mentioned the redirect query }, 3000);
//use $request instead of $_POST
if($request->isMethod('post'))
    {
        if(isset($request['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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM