CodepadME / laravel-tricks

The source code for the Laravel Tricks website
http://laravel-tricks.com
MIT License
966 stars 298 forks source link

New Form Fields and Database Table Columns #70

Closed kwameboame closed 9 years ago

kwameboame commented 10 years ago

Hello Guys,

I created new form fields in the settings area (settings.blade.php).

I also added the new data to the UsersTableSeeder.php like so: [ 'username' => 'kwame', 'email' => 'kwame@example.com', 'password' => Hash::make('password'), 'city' => 'London', 'country' => 'UK', 'descriptiom' => 'I am a PHP developer based in London', 'is_admin' => '1' ]

What else do I have to do to make this work properly (and create columns) so that I can seed the new data into it?

What am I missing? I hope someone can help me solve this.

msurguy commented 9 years ago

Sorry it took me a while to come back to this.

First, you'd need the fields to be present in the views: views/user/settings.blade.php

Then, put the additional fields in the migrations for the Users table (...create_users_table.php):

$table->string('city');
$table->string('country');
$table->text('description');

Finally, add validation for the form in the app/Tricks/Services/Forms/SettingsForm.php and also update the UserRepository.php in app/Tricks/Repositories/Eloquent.

Let me know if you have any more questions on this.

kwameboame commented 9 years ago

Hello msurguy,

When I submit the form (after filling out all the inputs, I get this message: "The description field is required."

This is how I set the validation rule:


protected $rules = [
        'username' => 'required|min:4',
        'password' => 'confirmed|min:6',
        'location' => 'required',
        'description' => 'required'
    ];

And this is how I'm getting the data:


public function getInputData()
    {
        return array_only($this->inputData, [
            'username', 'password', 'password_confirmation', 'location', 'description'
        ]);
    }

Here is the markup for the settings form:

      <fieldset>
        <div class="form-group {{Session::get('username_required')? 'has-error': ''}}">
          <label for="username" class="col-lg-4 control-label">{{ trans('user.username') }}</label>
          <div class="col-lg-8">
            {{ Form::text('username', Auth::user()->username, array('class'=>'form-control','placeholder'=>'Username'))}}
            @if(Session::get('username_required'))
              <span class="help-block">{{ trans('user.github_user_already_taken') }}</span>
            @endif
          </div>

        </div>
        <div class="form-group">
          <label for="email" class="col-lg-4 control-label">{{ trans('user.email') }}</label>
          <div class="col-lg-8">
            <input type="email" disabled class="form-control" id="email" placeholder="{{Auth::user()->email}}">
          </div>
        </div>

        <div class="form-group ">
          <label for="location" class="col-lg-4 control-label">Location</label>
          <div class="col-lg-8">
            {{ Form::text('location', Auth::user()->location, array('class'=>'form-control','placeholder'=>'Accra'))}}
          </div>
          </div>

        <div class="form-group">
          <label for="description" class="col-lg-4 control-label">Description</label>
          <div class="col-lg-8">
            <textarea type="description" class="form-control" id="description" placeholder="{{Auth::user()->description}}"></textarea>
          </div>
        </div>

        <div class="form-group">
          <label for="avatar" class="col-lg-4 control-label">{{ trans('user.profile_picture') }}</label>
          <div class="col-lg-8">
            <input type="hidden" id="avatar-hidden" name="avatar" value="">
            <div id="upload-avatar" class="upload-avatar">
              <div class="userpic" style="background-image: url('{{{ Auth::user()->photocss}}}');">
                 <div class="js-preview userpic__preview"></div>
              </div>
              <div class="btn btn-sm btn-primary js-fileapi-wrapper">
                 <div class="js-browse">
                    <span class="btn-txt">{{ trans('user.choose') }}</span>
                    <input type="file" name="filedata">
                 </div>
                 <div class="js-upload" style="display: none;">
                    <div class="progress progress-success"><div class="js-progress bar"></div></div>
                    <span class="btn-txt">{{ trans('user.uploading') }}</span>
                 </div>
              </div>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label for="password" class="col-lg-4 control-label">{{ trans('user.password') }}</label>
          <div class="col-lg-8">
            {{ Form::password('password', array('class'=>'form-control','placeholder'=> trans('user.new_password')))}}
          </div>
        </div>
        <div class="form-group">
          <label for="password_confirmation" class="col-lg-4 control-label">{{ trans('user.confirm_password') }}</label>
          <div class="col-lg-8">
            {{ Form::password('password_confirmation', array('class'=>'form-control','placeholder'=>trans('user.confirm_password')))}}
          </div>
        </div>
        <div class="form-group">
          <div class="col-lg-offset-7 col-lg-12">
            <input class="btn btn-sm" type="reset" value="{{ trans('user.reset_form') }}">
            <input class="btn btn-primary" type="submit" value="{{ trans('user.update') }}">
          </div>
         </div>
      </fieldset>

What am I doing wrong? Thanks.

msurguy commented 9 years ago

I see that you're missing a "name" attribute on the description field. Without the "name" attribute the server does not get this input field. Use this instead (added the name attribute):

<textarea type="description" class="form-control" id="description" placeholder="{{Auth::user()->description}}" name="description"></textarea>

Also you could use Laravel's form helper Form::textarea().