Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
SimpleAuth Update Profile Information
  • First, I apologize if this is the wrong place to post this. Below is the code I am using, it seems to be working fine, just want to make sure this is the best way to be doing this. This takes info from the form and updates just the profile_fields in the simpleauth database. The \Notify is a class I wrote that just makes it simple to display jquery notices for messages.
    public function action_profile()
     {  
      if (\Input::method() === 'POST')
      {
       // create instance of validation
       $val = \Validation::factory();
       
       // add fields to validate
       $val->add_field('profile_fields.first_name', 'First Name', 'trim|required|min_length[2]|max_length[32]');
       $val->add_field('profile_fields.last_name', 'Last Name', 'trim|required|min_length[2]|max_length[32]'); 
       
       if ($val->run())
       { 
        $update = \Auth::instance()->update_user($_POST['profile_fields']);
        
        if ($update)
        {
         // send success notification to browser
         \Notify::add(\Lang::line('settings.profile_updated'), 'success');
         
         // unset $_POST so we are using the freshest profile data
         $_POST = NULL;
        }
        else
        {
         \Notify::add(\Lang::line('settings.profile_error'), 'error', 8000);
        }
       }
      }
      
      // decide what to prefill form values
      if (empty($_POST))
      {
       $this->template->set_global('profile_fields', \Auth::instance()->get_profile_fields(), false);
      }
      else
      {
       $this->template->set_global('profile_fields' , $_POST, false);
      }
      
      // set the default page title
      $this->template->set('page_title' , 'Dashboard :: Settings + Trucero');
      
      // set the content view
      $this->template->set('content' , 'settings/user_settings'); 
     }
    

    Thank you in advance for any input.
  • Very weel, I have only one thing to add to your code.
    You do that:
    $update = \Auth::instance()->update_user($_POST['profile_fields']);
    

    But this isn't correct: you run the validation but you update profile information using $_POST variables.
    You can use validated field instead of Post field:
    $user_data['email'] = $val->validated('email');
    $user_data['old_password'] = $val->validated('old_password');
    $user_data['password'] = $val->validated('new_password');
       
    if(Auth::instance()->update_user($user_data))
    {
        // To something
    }
    

    Bye bye
  • Thank you for the response.... I am not sure why I used $_POST there rather than the "validated"... Thanks again! Hope someone else finds this useful as well.
  • Great example. Can you post the Form that you used for this?

Howdy, Stranger!

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

In this Discussion