简体   繁体   English

使用Laravel Fram工作提交数据后清除表单输入

[英]form input cleared after submit data using laravel fram work

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. 当我确认要发送的消息的product name is exist before时,我具有多个数据库连接。查看product name is exist before ,并且出现问题。

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

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. 顺便说一下,您的控制器方法将更干净。

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 https://laravel.com/docs/5.8/validation#custom-validation-rules- >使用闭包找到它

$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',
            ]        
        ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM