10

To pre-populate form field, we can add 'value' to form field in create.blade.php:

{{ Form::text('title', 'Some default title') }}

Is there a way to do that task elsewhere (maybe in model or controller?). I'd like to have code for form fields identical in create & edit view. Thanks!

4
  • Why not put just form code into separate partial view and then include it in both, create & edit views? Commented Jul 7, 2013 at 10:25
  • Hi Andreyco! I intend to try that - section or variable for form fields, with an if statement (if mode is create, use default value, or use null if mode is edit). I'd like also to set it elsewhere for cases like: setting category for new item (to be the same as 'current' category). Commented Jul 7, 2013 at 13:04
  • You may want to look into the "pattern" of using a ViewModel. An explanation is here note: Read that for the concept, don't try to use FuelPHP in your Laravel project. Here's an example in Zend. Commented Jul 7, 2013 at 16:08
  • fideloper, thank you for links. I admit often things work and I don't understand why. I'm new, but I can also blame the 'Magic' which hides the complexity and at the same time things important for understanding. :) Commented Jul 7, 2013 at 19:05

7 Answers 7

17

Okay, so here we are... I used Laravel's form model binding in the example. (I work with User model/db table). If this topic is not clear for you, take a look at this http://laravel.com/docs/html#form-model-binding

// Controller

class UsersController extends BaseController
{

    ...

    // Method to show 'create' form & initialize 'blank' user's object
    public function create()
    {
        $user = new User;
        return View::make('users.form', compact('user'));
    }

    // This method should store data sent form form (for new user)
    public function store()
    {
        print_r(Input::all());
    }

    // Retrieve user's data from DB by given ID & show 'edit' form   
    public function edit($id)
    {
        $user = User::find($id);
        return View::make('users.form', compact('user'));
    }

    // Here you should process form data for user that exists already.
    // Modify/convert some input data maybe, save it then...
    public function update($id)
    {
        $user = User::find($id);
        print_r($user->toArray());
    }

    ...

}

And here come the view file served by controller.

// The view file - self describing I think
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    @if(!$user->id)
    {{ Form::model($user, ['route' => 'admin.users.store']) }}
    @else
    {{ Form::model($user, ['route' => ['admin.users.update', $user->id], 'method' => 'put']) }}
    @endif
        {{ Form::text('firstName') }}

        {{ Form::text('lastName') }}

        {{ Form::submit() }}
    {{ Form::close() }}
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

A single view file is much better, easier to maintain, than what I wanted to try (two view files and a common formfields file :) ); GREAT!!! Thanks! The only change I needed was in if statement ---> @if(! isset($user->id))
5

Yes, let's consider the following example:

View:

{{ Form::text('title', $title) }}

Controller:

$title = 'Some default title';
if($article) {
    $title = $article->title;
}
return View::make('user.login')->with('title', $title)

Then you will have a text-input with either Some default title or the title from $article, if $article isn't equal to false

2 Comments

Thanks, Victor! Adding $title variable to view, and then appending 'with' part(s): ->with('title', 'Some default ttle') worked. If I have identical view code for editing, it will complain about missing title variable, so I add: ->with('title', null)
Somehow this was deselected as accepted answer when I selected Andreyco's answer. Both provide useful answer to my "Is there a way" question :) Thanks, all!
3

All you need to do is include a conditional in your blade template.

Lets assume your database table has a field myfield, which you want to default to mydefault.

Just include the following in a partial view which is called by the create and edit views:

@if(isset($myfield))
{{ Form::input('text','myfield') }}
@else
{{ Form::input('text','myfield','mydefault') }}
@endif

You don't have to anything else.

Comments

2

if you mean placeholder you can do this

{{ Form::password('password', array('placeholder' => 'password'))}}

1 Comment

Thanks mahami, it's not about placeholder.
1

Probably easier (Laravel 5):

{{ Form::text('title', isset($yourModel) ? null : 'Some default title') }}

That is to assume you are using the form as a partial. It should populate the value if the model for the form exists (you are editing or patching record) else it should show you the default you wish.

Comments

0

When you are using the Schema builder (in Migrations or somewhere else):

Schema::create( 
      'posts', function($table) {
          $table->string('title', 30)->default('New post'); 
      }
);

1 Comment

Hi Ivan, that works for database defaults, it won't change anything in model or other modules.
0

If you want to do this conditionally, an alternative method of solving this could be to perform a check instead. If this check passes, set the default (as is done below with $nom as an example). Otherwise, leave it empty by setting it to null explicitly.

{{ Form::text('title', (isset($nom)) ? $nom : null) }}

1 Comment

While code only answers are allowed, please consider editing your answer to include a description. This helps the answer apply to more situations and helps others learn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.