Laravel:未定义的偏移量:0

5

我正在尝试在数据库中选择的字段重复时显示错误消息。为此,我使用了 Laravel 验证规则 required unique。但是我遇到了重定向问题。以下是存储控制器的代码:

 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('/');
        }
    }

这里是路由

Route::get('insur_docs/create', array('as' => 'insur_docs.create','uses' => 'Insur_DocController@create'));

创建控制器

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

<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

可能是由于唯一部分引起的。您正在将汽车设置为insur_docs和car_id表中的唯一项。我猜这两个不是实际的表,而只是表中的数据。因此,如果您有一张汽车表,您将把汽车值设置为汽车表的唯一项。例如:'car' => array('unique:cars') - CheckeredMichael
但是你的创建方法是什么样子的? - sidneydobber
@CheckeredMichael 我有一个名为“insur_docs”的表,其中有一个名为car_id的字段。我做错了什么? - user3592644
我刚刚查看了Laravel文档,唯一部分实际上是正确的,对此感到抱歉。如果错误甚至在您提交数据之前就出现了,那么您需要查看create方法,错误应该就在那里。 - CheckeredMichael
不要在提交后返回翻译文本。 - user3592644
3个回答

4
可能是您正在使用get,尝试使用post。另外,您混合了modelcontroller代码。最好将它们分开,例如重定向应该在控制器内完成而不是在模型中。
以下链接可以帮助您了解有关Laravel验证的更多信息: 建议在$validator->passes()上执行操作,然后使用else返回错误。
控制器:
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下可用。只需在您的blade模板中执行{{var_dump($errors)}}以验证它们是否存在。

视图

@if($errors->count() > 0)
  <p>The following errors have occurred:</p>

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

你忘记了验证样式:/ - user3592644
我创建了一个函数:public static function macro($field, $errors)。如果$errors中包含$field的错误信息,那么会返回以下格式的内容:<span style="color:red" class="error">{错误信息}</span>,其中错误信息将以红色字体展示。如果没有错误信息,则返回空字符串。 - user3592644
但这并不添加任何样式 :/ 只是红色。 - user3592644
返回以下編程相關內容的中文翻譯:或者這樣 public static function macro($field, $errors) { if ($errors->has($field)) { $msg = $errors->first($field); return "<div class="col-sm-5">" . "<span class="help-block">" . "<span class="glyphicon glyphicon-warning-sign">" . "</span> $msg</span>" . "</div>"; } return ''; } 但是沒有紅色邊框 :/ - user3592644
请查看此链接:http://stackoverflow.com/questions/23568538/filled-fields-are-going-empty-after-validation - user3592644
显示剩余10条评论

1
我认为这个参考文献提供了一个更好的答案: Laravel 4, ->withInput(); = Undefined offset: 0 withInput()并不像你想象的那样工作。它只是Redirect的一个函数,而不是View。
在View上调用withInput($data)会产生完全不同的效果;它将以下键值对传递给您的view:'input'= > $data(您会因为没有向函数传递任何数据而出现错误)。
要获得您想要的效果,请在制作视图之前调用Input::flash(),而不是调用withInput()。这应该允许您在视图中使用Input::old()函数来访问数据。
或者,您可以简单地将Input::all()传递给您的视图,并在视图中使用数组。
View::make(...)->withInput(Input::all());

翻译为

View::make(...)->with('input', Input::all());

关于您的评论,我建议这样做:

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

0

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

例如:- Route :: get('hasith','Order \ OrderController @ hasith'); -----&gt;     此参数将重定向到此函数

   public function show($id, Request $request) {
            //code
   }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接