Define default values for Laravel form fields

前端 未结 7 2146
感动是毒
感动是毒 2021-02-20 12:45

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

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

Is ther

相关标签:
7条回答
  • 2021-02-20 13:06

    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

    0 讨论(0)
  • 2021-02-20 13:14

    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>
    
    0 讨论(0)
  • 2021-02-20 13:15

    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.

    0 讨论(0)
  • 2021-02-20 13:18

    if you mean placeholder you can do this

    {{ Form::password('password', array('placeholder' => 'password'))}}
    
    0 讨论(0)
  • 2021-02-20 13:18

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

    Schema::create( 
          'posts', function($table) {
              $table->string('title', 30)->default('New post'); 
          }
    );
    
    0 讨论(0)
  • 2021-02-20 13:28

    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) }}
    
    0 讨论(0)
提交回复
热议问题