1

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() }}
Moeflon
  • 23
  • 5
  • IF you do `return (var_dump($server));` at the end of edit() function (before the existing `return` call), does it return a server object or null? How about `$name`, is it an ID number or name string? – Simo A. Mar 19 '14 at 15:03
  • Yes it returns the whole server object, $name is a name string but I have changed the primary key so that shouldn't be a problem. I can even use the $server variable in the view and read out its content. – Moeflon Mar 19 '14 at 15:12

1 Answers1

7

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')) }}
Stidges
  • 141
  • 3