Welcome Guest, Not a member yet? Register   Sign In
Single Responsibility Principal
#5

[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]


Messages In This Thread
Single Responsibility Principal - by El Forum - 01-12-2013, 05:04 PM
Single Responsibility Principal - by El Forum - 01-13-2013, 07:49 PM
Single Responsibility Principal - by El Forum - 01-14-2013, 02:55 AM
Single Responsibility Principal - by El Forum - 01-14-2013, 11:51 AM
Single Responsibility Principal - by El Forum - 01-24-2013, 07:53 PM
Single Responsibility Principal - by El Forum - 01-25-2013, 12:31 PM
Single Responsibility Principal - by El Forum - 01-26-2013, 12:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB