CodeIgniter Forums

Full Version: Single Responsibility Principal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]xtremer360[/eluser]
This is what I am currently using for my submit function but it is doing way to much and I'm wanting to work with it so that I can apply the Single Responsibility Principle as well as I can see my code is suffering from Arrow Anti-Pattern and was looking for some ideas on how this could be improved. I had to include it in a link because the function was too big.

http://pastebin.com/j2wnck6C

El Forum

[eluser]xtremer360[/eluser]
Any ideas?

El Forum

[eluser]Otemu[/eluser]
Hi,

You could make better use of the Codeigniter Validation class, for instance you have a lot of

Code:
if (strtotime($user_data->lock_date) > $current_time)
                                {
                                        // User is still locked out
                                        $output_status = 'Error';
                                        $output_title = 'Account Locked';
                                        $output_message = 'This user account is current locked!';
                                        $x++;
                                }

Quote:The validation system supports callbacks to your own validation functions. This permits you to extend the validation class to meet your needs. For example, if you need to run a database query to see if the user is choosing a unique username, you can create a callback function that does that

So for instance to check status you could run a callback:

Code:
$this->form_validation->set_rules('username', 'Username', 'required|check_status');
public function check_status($str)
{
  if (//your check)
  {
                        //your code
   $this->form_validation->set_message('check_status', 'status message"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}

Quite a few of your if else statements can also be changed to just switch statements. Then just use set_message for each one.
You can also save sets of rules in a config file Form Validation

Also check out Flattening Arrow Code and The Single Responsibility Principle

El Forum

[eluser]xtremer360[/eluser]
I should mention this form is submitted with jQuery ajax function.

El Forum

[eluser]xtremer360[/eluser]
Well with the user status that would display under the username field and I just want it to be a part of what is returned as the json output array.

Here's what I have so far:

Code:
public function form_is_valid()
{
  $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|regex_match[/[a-z0-9]/]');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|regex_match[/[a-z0-9]/]');

  return $this->form_validation->run();
}

public function submit()
{
  $output_status = 'Notice';
  $output_title = 'Not Processed';
     $output_message = 'The request was unprocessed!';
    
     if ($this->form_is_valid())
     {
      
      $output_status = 'Success';
      $output_title = 'Form Submitted Successfully';
      $output_message = 'The form did validate successfully!';
     }
     else
     {
      $output_status = 'Error';
      $output_title = 'Form Not Validated';
      $output_message = 'The form did not validate successfully!';
     }
  
  echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message, 'error_messages' => $this->form_validation->get_error_array()));
}

[quote author="Otemu" date="1358157348"]Hi,

You could make better use of the Codeigniter Validation class, for instance you have a lot of

Code:
if (strtotime($user_data->lock_date) > $current_time)
                                {
                                        // User is still locked out
                                        $output_status = 'Error';
                                        $output_title = 'Account Locked';
                                        $output_message = 'This user account is current locked!';
                                        $x++;
                                }

Quote:The validation system supports callbacks to your own validation functions. This permits you to extend the validation class to meet your needs. For example, if you need to run a database query to see if the user is choosing a unique username, you can create a callback function that does that

So for instance to check status you could run a callback:

Code:
$this->form_validation->set_rules('username', 'Username', 'required|check_status');
public function check_status($str)
{
  if (//your check)
  {
                        //your code
   $this->form_validation->set_message('check_status', 'status message"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}

Quite a few of your if else statements can also be changed to just switch statements. Then just use set_message for each one.
You can also save sets of rules in a config file Form Validation

Also check out Flattening Arrow Code and The Single Responsibility Principle[/quote]

El Forum

[eluser]xtremer360[/eluser]
Any additional ideas?

El Forum

[eluser]xtremer360[/eluser]
*bump*