doubingjiu3199 2019-06-10 19:46
浏览 314
已采纳

使用laravel框架提交数据后清除表单输入

I have multiple data base connection when I validate name of product I send message product name is exist before to view and here problem is appeared.

Message appeared in view but all form inputs is cleared. How I recover this problem taking in consideration if product name not exist. validation executing correctly and if found error in validation it appeared normally and form input not cleared. this my controller code.

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
            $query = dBHelper::isExist('mysql2','products','`status`=? AND `deleted` =? AND `name`=?',array(1,1,$validationarray['name']));
            if(!$query) {

                $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 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

this is my route

Route::get('/products/add',"produtController@add");
Route::post('/products/add',"produtController@add");
  • 写回答

2条回答 默认 最新

  • dps57553 2019-06-10 20:38
    关注

    You can create your own custom validate function like below. I guess this should help you.

    Found it from https://laravel.com/docs/5.8/validation#custom-validation-rules -> Using Closures

    $validationarray = $this->validate($request,
            [
                'name'  => [
                    'required',
                    'alpha',
                    function ($attribute, $value, $fail) { 
                        //$attribute->input name, $value for that value.
                        //or your own way to collect data. then check.
                        //make your own condition.
                        if(true !=dBHelper::isExist('mysql2','products','`status`=? AND `deleted` =? AND `name`=?',array(1,1,$value))) {                        
                               $fail($attribute.' is failed custom rule. There have these named product.');
                        }
                    },          
                ],
                'price' => [
                    'required',
                    'numeric',
                ]        
            ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?