doushuichong2589 2014-12-11 20:48
浏览 35
已采纳

更新时验证失败

I am using laravel 4.2 and I have the following issue:

My validation works properly at the Store function but when i am using the same code to validate the data at Update function the validator works but doesn't redirect correctly or showing the validation errors. to be more specific; at the update function the validator validates the data but when the validator fails instead of redirecting to the edit page with the validation errors ; redirects to the index page without saving the new data .Here is my code:

Model

public static $rules= array(

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

Controller (code for store)

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

View for Create

 {{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()}}

Controller (code for Update)

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

View for Update

 <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条回答 默认 最新

  • dongqie8661 2014-12-11 21:29
    关注

    I think you have replace this

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

    with this

    return Redirect ::route ('estates.edit', ['id' => $id])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比