首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义偏移量:0

未定义偏移量:0
EN

Stack Overflow用户
提问于 2014-05-08 09:38:32
回答 3查看 86.5K关注 0票数 5

我正在尝试播放一个错误的介面,万一选择的字段是复制在db.For,这是我使用的拉拉验证所需的独特。我在重定向方面有问题,这里是商店管理员。

代码语言:javascript
复制
 public function store() {
        $rules = array(
            'car' => array('required', 'unique:insur_docs,car_id'),
        );

        $validation = Validator::make(Input::all(), $rules);

        if ($validation->fails()) {
            // Validation has failed.
            return Redirect::to('insur_docs/create')->with_input()->with_errors($validation);
        } else {
            $data = new InsurDoc();
            $data->ownership_cert = Input::get('ownership_cert');
            $data->authoriz = Input::get('authoriz');
            $data->drive_permis = Input::get('drive_permis');
            $data->sgs = Input::get('sgs');
            $data->tpl = Input::get('tpl');
            $data->kasko = Input::get('kasko');
            $data->inter_permis = Input::get('inter_permis');
            $data->car_id = Input::get('car');
            $data->save();
            // redirect
            return Redirect::to('/');
        }
    }

这是路线

代码语言:javascript
复制
Route::get('insur_docs/create', array('as' => 'insur_docs.create','uses' => 'Insur_DocController@create'));

创建控制器

代码语言:javascript
复制
public function create() {


     $cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');
        return View::make('pages.insur_docs_create', array(
                    'cars' => $cars
        ));

}

insur_docs_create.blade.php

代码语言:javascript
复制
<div id="div-1" class="body">
            {{ Form::open(array('url' => 'insur_docs/store', 'class'=>'form-horizontal','id'=>'inline-validate')) }} 
            <div class="form-group">
                {{ Form::label('ownership_cert', 'Ownership Certificate', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('ownership_cert', array('' => '', '1' => 'Yes', '0' => 'No'), '',  array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid ownership certificate',
                                'class' => 'form-control'))
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('authoriz', 'Authorization', array('class'=>'control-label col-lg-4')) }}            
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('authoriz', '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid authorization date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('drive_permis', 'Drive Permission', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('drive_permis', array('' => '', '1' => 'Active', '0' => 'Not active'),  '',  array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid drive permission',
                                'class' => 'form-control'))
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('sgs', 'SGS', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('sgs', '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid sgs date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>  
            <div class="form-group">
                {{ Form::label('tpl', 'TPL', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('tpl', isset($v->sgs) ? $v->sgs : '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid tpl date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('kasko', 'Kasko', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('kasko', isset($v->kasko) ? $v->kasko : '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid kasko date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('inter_permis', 'International Permission', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('inter_permis', '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid international permission date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('car', 'Car', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('car', $cars, Input::old('class'), array(
                                'data-validation' => 'required',  
                                'data-validation-error-msg' => 'You did not enter a valid car',
                                'class' => 'form-control')) 
                    }}
                    {{ $errors->first('car') }}
                </div>
            </div>
            {{ Form::submit('Save', array('class' => 'btn btn-success btn-line')) }}
            <input type="button" value="Back" class="btn btn-danger btn-line" onClick="history.go(-1);
                    return true;">
            <div>
                @foreach($errors as $error)
                <li>{{$error}}</li>
                @endforeach
            </div>
            {{ Form::close() }}

显示此错误:

未定义偏移量:0

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-08 10:00:45

您可能使用的是get,使用post可能会有所帮助。除此之外,您还需要混合modelcontroller代码。把它们分开总是个好主意。例如,您的重定向应该在控制器内完成,而不是在模型中完成。

  • http://laravel.com/docs/validation
  • http://laravelbook.com/laravel-input-validation/
  • http://culttt.com/2013/07/29/creating-laravel-4-validation-services/

$validator->passes()上做一些事情也更好,然后else返回错误。

控制器

代码语言:javascript
复制
public function store() {

    $data = [
       "errors" => null
    ];

    $rules = array(
        'car' => array('required', 'unique:insur_docs,car_id')
    );

    $validation = Validator::make(Input::all(), $rules);

    if($validation->passes()) {
        $data = new InsurDoc();
        $data->ownership_cert = Input::get('ownership_cert');
        $data->authoriz = Input::get('authoriz');
        $data->drive_permis = Input::get('drive_permis');
        $data->sgs = Input::get('sgs');
        $data->tpl = Input::get('tpl');
        $data->kasko = Input::get('kasko');
        $data->inter_permis = Input::get('inter_permis');
        $data->car_id = Input::get('car');
        $data->save();
        return Redirect::to('/');
    } else {
        $data['errors'] = $validation->errors();
        return View::make('pages.insur_docs_create', $data);
    }

}

$errors下,您的错误将在您的视图中可用。只需在您的{{var_dump($errors)}}模板中执行一个blade,以验证它们是否存在。

视图

代码语言:javascript
复制
@if($errors->count() > 0)
  <p>The following errors have occurred:</p>

  <ul>
    @foreach($errors->all() as $message)
      <li>{{$message}}</li>
    @endforeach
  </ul>
@endif
票数 4
EN

Stack Overflow用户

发布于 2014-06-02 10:32:19

我认为这是一个更好的答案从这个参考Laravel 4, ->withInput(); = Undefined offset: 0

withInput()不像你想的那样工作。这只是一个重定向的功能,而不是视图。

调用withInput($data) on View有一个完全不同的效果;它将以下键值对传递给您的view: 'input' => $data (您得到一个错误,因为您没有将任何数据传递给该函数)

要获得所需的效果,请在创建视图之前调用Input::flash(),而不是调用withInput()。这应该允许您在视图中使用Input::old()函数来访问数据。

或者,只需将Input::all()传递给视图,并在视图中使用input[]数组:

代码语言:javascript
复制
View::make(...)->withInput(Input::all());

它被翻译成

代码语言:javascript
复制
View::make(...)->with('input', Input::all());

至于你的评论,我建议这样做:

代码语言:javascript
复制
$position_options = DB::table('jposition')->lists('friendly','id');
$category_options = DB::table('jcategory')->lists('friendly','id');
$location_options = DB::table('jlocation')->lists('friendly','id');
$category = Input::get('category');
$location = Input::get('location');
$type = Input:: get('type'); 

$data = compact('position_options', 'category_options', 'location_options', 'category', 'type', 'location');

return View::make('jobsearch.search', $data);
票数 1
EN

Stack Overflow用户

发布于 2019-03-28 06:20:04

同时也考虑到了拉拉资源控制器。因为当我们调用无参数get方法时,它会重定向到show方法,而忽略了我们的函数名。

路径::get(‘hasith’,‘Order\OrderController@hasith’);-->此参数与此函数相对应

代码语言:javascript
复制
   public function show($id, Request $request) {
            //code
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23538053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档