2019-06-10 19:46
浏览 310


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

            // start validatio array

                //'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');
                $add = $product->id;
                $poducten = new productEnModel();
                $poducten->id_product = $add;
                $poducten->name = $request->input('name');
                $poducten->price = $request->input('price');
                $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



        <div class="alert alert-danger">
              @foreach($errors->all() as $error)




    <form role="form"  action="add" method="post" enctype="multipart/form-data">
        <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 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">
        <!-- /.box-body -->
        <div class="box-footer">
            <button type="submit" name="add" class="btn btn-primary">Add</button>


this is my route

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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 -> Using Closures

    $validationarray = $this->validate($request,
                'name'  => [
                    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' => [
    打赏 评论
  • dsogx84602 2019-06-10 20:19

    First way you can throw validation exception manually. Here you can find out how can you figure out.

    Second way (I recommend this one) you can generate a custom validation rule. By the way your controller method will be cleaner.

    打赏 评论

相关推荐 更多相似问题