繁体   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