验证后填充的字段将变空

时间:2014-05-09 15:32:13

标签: validation laravel

我已经创建了一个带有验证的laravel表单,这里的问题是,当我离开空字段时,它会显示错误消息(这没关系)但是如果我填写一些字段而不是其他字段则刷新然后现在填充的字段为空,并显示该字段的错误消息。

public function store() {
        $data = [
            "errors" => null
        ];
        $rules = array(
            "car" => "required|unique:insur_docs,car_id", 
            "ownership_cert" => "required",
            "authoriz" => "required",            
            "drive_permis" => "required",
            "sgs" => "required",
            "tpl" => "required",
            "kasko" => "required",
            "inter_permis" => "required",
        );
        $validation = Validator::make(Input::all(), $rules);
        $cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');  
        if ($validation->passes()) {
            $doc = new InsurDoc();
            $doc->ownership_cert = Input::get('ownership_cert');
            $doc->authoriz = Input::get('authoriz');
            $doc->drive_permis = Input::get('drive_permis');
            $doc->sgs = Input::get('sgs');
            $doc->tpl = Input::get('tpl');
            $doc->kasko = Input::get('kasko');
            $doc->inter_permis = Input::get('inter_permis');
            $doc->car_id = Input::get('car');
            $doc->save();
            return Redirect::to('/');
        } else {
            $data['errors'] = $validation->errors();
            $data['cars'] = $cars;
                return View::make('pages.insur_docs_create', $data);
        }
    }

视图

@if($errors->count() > 0)
                    <div class="alert alert-danger">
                        <p>The following errors have occurred:</p>

                        <ul>
                            @foreach($errors->all() as $message)
                            <li>{{ Helpers\Helper::macro($message) }}</li>
                            @endforeach
                        </ul>
                    </div>
                    @endif

宏功能

public static function macro($errors) {
            return   "<span class=\"glyphicon glyphicon-warning-sign\">"
                    . "</span> $errors";

    }

那里出了什么问题?

1 个答案:

答案 0 :(得分:0)

在您的控制器中添加Input::flash(),并在Input::old('field_name')的视图中使用它。

            Input::flash(); // Add this
            $data['errors'] = $validation->errors();
            $data['cars'] = $cars;
                return View::make('pages.insur_docs_create', $data);
        }
    }