I am in the progress of making a form to edit an existing entry in the database. I am using the Form::model approach to do this, however it doesn't seem to work. The fields just stay empty.

ServerController.php

/**
* Editing servers
*/
public function edit($name)
{
  $server = Server::find($name);
  $keywords = ($server->getKeywords()) ? $server->getKeywords() : array();
  $countries = $this->getCountries();
  return View::make('server/edit', array('server' => $server, 'countries' => $countries));
}

public function update($name) 
{
  $server = Server::find($name);
  // Did it succeed?
  if($server->save()) {
    Session::flash('success', 'You server was edited!');
    return Redirect::route('server.view', array($name));
  }

  // Did not validate
  if(Input::get('keywords')) {
    $keywords = Input::get('keywords');
    Session::flash('keywords', $keywords);
  }
  Session::flash('danger', "<b>Oops! There were some problems processing your update</b><br/>" . implode("<br/>", $server->errors()->all()));
  return Redirect::route('server.edit', array($name))->withInput()->withErrors($server->errors());
}

The Form

{{ Form::model($server, array('route' => array('server.update', $server->name), 'class' => 'form-horizontal', 'role' => 'form', 'files' => true)) }}
  <div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
    {{ Form::label('email', 'Email', array('class' => 'control-label col-md-4')) }}
    <div class="col-md-4">
      {{ Form::text('email', '', array('class' => 'form-control')) }}
      {{ $errors->first('email', '<br/><div class="alert alert-danger">:message</div>') }}
    </div>
  </div>
  (some more fields)
{{ Form::close() }}
有帮助吗?

解决方案

The problem here is that you're passing in an empty string as the default field value. As the documentation states here, any explicitly passed values will overrule the model attribute data. Try using null instead of '':

{{ Form::text('email', null, array('class' => 'form-control')) }}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top