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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题