Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Sample Profile Controller/View
  • I created an example profile controller/view that seems to work well. Does it makes sense to you? controller/profile.php
    class Controller_Profile extends Controller_Common {
    
     public function action_index()
     {         
      if (Input::method() === 'POST')
      {
       // Setup validation
       $val = Validation::factory();
      
       // Set validation rules
       $val->add_field('first_name', 'First Name', 'trim|min_length[2]|max_length[32]');
       $val->add_field('last_name', 'Last Name', 'trim|min_length[2]|max_length[32]');
       $val->add_field('email', 'Email', 'trim|required|valid_email|min_length[2]|max_length[255]');
       
       // Determin if user wants to change password
       if (!empty($_POST['old_password']) and !empty($_POST['password']))
       {
        $val->add_field('old_password', 'Old Password', 'trim|required|min_length[2]|max_length[255]');
        $val->add_field('password', 'Password', 'trim|required|min_length[2]|max_length[255]');
       }
       
       if ($val->run())
       {      
        // Update user
        if (Auth::instance()->update_user($val->validated()))
        { 
         Session::set_flash('success', 'Profile updated successfully!');
         Response::redirect('profile');
        }
        else
        {
         Session::set_flash('notice', 'Profile was not updated.');
         Response::redirect('profile');
         //throw new Exception('An unexpected error occurred. Please try again.');
        }
       }
       else
       {
        $val->set_message('valid_email', 'The field :label is not a valid email address.'); 
       }
       
       View::set_global('val', $val, false);
      }
        
      // Load User Data
      $user = Model_User::find($this->user_id);
       
      $this->template->set_global('user', $user, false);
      $this->template->set_global('profile_fields', Auth::instance()->get_profile_fields(), false);
      
      $this->template->title = 'Profile';
      $this->template->content = View::factory('profile/index');
     }
    
    }
    

    views/profile/index.php
    <div class="row"><br>
    <div class="column grid_12"><br>
    <h1>Profile Information</h1><br>
    <?php if (isset($val)) echo $val->show_errors(); ?><br>
    <br>
    <?php echo Form::open(); ?><br>
    <br>
    <p>Username: <?=Auth::instance()->get_screen_name();?></p><br>
    <p><label for="form_first_name">First Name</label> <?php echo Form::input('first_name', Input::post('first_name', isset($profile_fields) ? $profile_fields['first_name'] : '')); ?> </p><br>
    <p><label for="form_last_name">Last Name</label> <?php echo Form::input('last_name',  Input::post('last_name', isset($profile_fields) ? $profile_fields['last_name'] : '')); ?> </p><br>
    <p><label for="form_email">Email</label>   <?php echo Form::input('email',   Input::post('email', isset($user) ? $user->email : '')); ?> </p><br>
    <br>
    <hr><br>
    <p><label for="form_password">Old Password</label>   <?php echo Form::password('old_password',   Input::post('old_password', '')); ?> </p><br>
    <p><label for="form_password">New Password</label>   <?php echo Form::password('password',   Input::post('password', '')); ?> </p><br>
    <br>
    <div class="actions"> <?php echo Form::submit(); ?> </div><br>
    <?php echo Form::close(); ?><br>
    </div><br>
    </div>
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!