vendredi 8 juin 2018

auto populate laravel form

I am currently rendering and populating a form in laravel using the laravelcollective plugin. this is working as expected:

{!! Form::model($user, ['action' => 'user@updateUser']) !!}

<div class="form-group">
    {!! Form::label('user_name', 'Name') !!}
    {!! Form::text('user_name') !!}
</div>

<button class="btn btn-success" type="submit">Update</button>

{!! Form::close() !!}

The above code generates the form and populates the input field with the user's name.

If I want to add a class attribute to the form input like so:

{!! Form::text('user_name', '', ['class' => 'form-control']) !!}

It does not populate the input value because in theory I've set the default value (second parameter) to ''.

Is there a way of populating the value and adding a class without explicitly doing so like this:

{!! Form::text('user_name', $user->user_name, ['class' => 'form-control']) !!}

By doing the above it defeats the object of rendering the form via a model {!! Form::model($user, ['action' => 'user@updateUser']) !!} as I may as well parse the $user as a variable onto the template, which I don't want to do.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire