更新时验证失败

时间:2014-12-11 20:48:38

标签: php validation laravel-4 laravel-validation

我正在使用laravel 4.2,我有以下问题:

我的验证在Store函数中正常工作,但是当我使用相同的代码验证数据时 在Update函数中,验证器可以正常工作,但不能正确重定向或显示验证错误。更具体;在更新函数中,验证器验证数据,但验证器失败时,而不是重定向到具有验证错误的编辑页面;重定向到索引页面而不保存新数据。这是我的代码:

模型

public static $rules= array(

    'location'=> 'required',
    'price'=> 'required|numeric',
    'info'=> 'required'
);

控制器(商店代码)

public function store()
 {

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

    if ($v->fails())
    {
        return Redirect ::route ('estates.create')
        ->withInput(Input::all())
        ->withErrors($v);
    }
    else
        {

    $userid = Auth::user()->getId();

    $estate = new Estate;
    $estate-> location = Input :: get('location');
    $estate-> price = Input::get('price');
    $estate-> info = Input::get ('info');

    $estate->user_id = $userid;

    $estate->save();

    return Redirect::to('estates');
        }
}

创建视图

 {{Form::Open(array ('url'=>'estates')) }}


    <div class="form-group {{$errors->has('location') ? 'has-error' : ''}}">
      <label for="location">Location:</label>
      <input type="string" name="location" value="{{{ Input::old('location') }}}" class="form-control">
      {{$errors->first('location','<span class="help-block">:message</span>')}}
  </div>

  <div class="form-group {{$errors->has('price') ? 'has-error' : ''}}">
    <label for="price">Price:</label>
    <input type="string" name="price" value="{{{ Input::old('price') }}}" class="form-control">
    {{$errors->first('price','<span class="help-block">:message</span>')}}
  </div>

  <div class="form-group {{$errors->has('info') ? 'has-error' : ''}}">
    <label for="info">Info:</label>
    <textarea input type ="text" class="col-md-12 input-block-level" rows="8" name="info" id="info"  class="from-control">{{{ Request::old('info') }}}</textarea>
    {{$errors->first('info','<span class="help-block">:message</span>')}}
  </div>

</div>

  <div class="container">

    <button type="Submit" class="btn btn-default">Add estate</button>

  </div>

{{Form::close()}}

控制器(更新代码)

public function update($id)
{
    $v=Validator::make(Input::all(), Estate:: $rules);

    if ($v->fails())
    {
        return Redirect ::route ('estates.edit')
        ->withInput(Input::All())
        ->withErrors($v);
    }

    else
     {
        $userid = Auth::user()->getId();

        $estate = Estate::find($id);
        $estate-> location = Input::get('location');
        $estate-> price = Input::get('price');
        $estate-> info = Input::get ('info');

        $estate->user_id = $userid;

        $estate->save();

        return Redirect::to('estates');
     }
}

查看更新

 <div class="container">
{{Form::Open(array ('method'=>'patch' , 'route'=> array ('estates.update', $estate->id)))}}

  <div class="form-group {{$errors->has('location') ? 'has-error' : ''}}">
      <label for="">Location:</label>
      <input type="string" name="location" value="{{$estate->location}}" class="form-control">
      {{$errors->first('location','<span class="help-block">:message</span>')}}
  </div>


  <div class="form-group {{$errors->has('price') ? 'has-error' : ''}}">
    <label for="">Price:</label>
    <input type="string" name="price" value="{{$estate->price}}" class="form-control">
    {{$errors->first('price','<span class="help-block">:message</span>')}}
  </div>

  <div class="form-group {{$errors->has('info') ? 'has-error' : ''}}">
    <label for="">Info:</label>
    <textarea class="col-md-12 " rows="4" input type="text" name="info" id="info" class="form-control" > {{$estate->info}}</textarea>
  </div>
</div>
<div class="container">

  <button type="Submit" class="btn btn-default">Update</button>

  {{Form::close()}}
</div>

1 个答案:

答案 0 :(得分:0)

我认为你已经取代了这个

return Redirect ::route ('estates.edit')

用这个

return Redirect ::route ('estates.edit', ['id' => $id])