Welcome Guest, Not a member yet? Register   Sign In
I have few questions...
#1

[eluser]mTuran[/eluser]
Hi everone. i am developing my first MVC pattern app and have few questions about ci.

1-) i do validation like below but i didn't understand what for trim or xss clean validation options. Normally they are used for trim string or clean string against xss codes but "$this->form_validation->set_rules('i_fullname', 'lang:fullname', 'trim|required|xss_clean');" this line only set rule, don't manupulate i_fullname input with trim or xss clean also this line doesn't return any manupulated value.

Code:
// Collectiong User Input
            $fullname = ucwords($this->input->post('i_fullname'));
            $birthdate= convert_datetime($this->input->post('i_birthdate'), 'mysql');
            $country= ucwords($this->input->post('i_country'));
            $city = ucwords($this->input->post('i_city'));
            $email = $this->input->post('i_email');
            $username = $this->input->post('i_username2');
            $password = $this->input->post('i_password2');
            $password_confirm = $this->input->post('i_password2_confirm');
            $captcha = strtoupper($this->input->post('i_captcha'));
    
            // Validation
            $this->load->helper('form');    
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<span class="info error">', '</span>');
            $this->form_validation->set_rules('i_fullname', 'lang:fullname', 'trim|required|xss_clean');
            $this->form_validation->set_rules('i_birthdate', 'lang:birthdate', 'trim|required|xss_clean');
            $this->form_validation->set_rules('i_country', 'lang:country', 'trim|required|xss_clean');

2-) I wrote example of my controller, my code logic is like that. am i doing MVC pattern with proper way ? Can you suggest better coding patterns ?

Code:
function email_confirmation($membership_type)
    {
        // Collectiong User Input
        $email = $this->input->post('i_email');
        
        // Validation
        $this->load->helper('form');    
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('i_email', 'lang:email', 'trim|required|valid_email|xss_clean|callback__email_check');
        
        if (!$this->form_validation->run())
        {
            $data['content'] =  $this->load->view('hesap/register_email_confirmation_view.php', '', TRUE);            
        }
        else
        {
            // Sending confirmation e-mail
            $this->my_user->send_confirmation_email($email, $membership_type);
            
            $content_data = array(
                'email' => $email
            );
            $data['content'] =  $this->load->view('hesap/register_email_confirmation_sended_view.php', $content_data, TRUE);            
        }
        
        // Page data
        $data['page_title'] = 'My Page';
        $data['page_description'] = 'example description';
        $data['js'] = '$(function() { hesap = new exampleJsClass(); });';
        $this->load->vars($data);
        $this->load->view('layout/default');
    }

3-) I want to use validation callbacks global. For example i have callback like below i want to use that in many controllers, i don't want rewrite same code again. What is proper way of done that ?

Code:
function _username_check($username)
        {
            if (!$this->user->is_username_available($username))
            {
                $this->form_validation->set_message('_username_check', $this->lang->line('not_available_resource'));
                return FALSE;
            }
            
            if(preg_match('/[^A-Za-z0-9_$]/', $username))
            {
                $this->form_validation->set_message('_username_check', $this->lang->line('valid_username'));
                return FALSE;
            }
            
            return TRUE;
        }

Thank you very much
#2

[eluser]TheFuzzy0ne[/eluser]
1) You need to collect the data after you've run the validation.
Code:
// Validation
$this->load->helper('form');    
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span class="info error">', '</span>');
$this->form_validation->set_rules('i_fullname', 'lang:fullname', 'trim|required|xss_clean');
$this->form_validation->set_rules('i_birthdate', 'lang:birthdate', 'trim|required|xss_clean');
$this->form_validation->set_rules('i_country', 'lang:country', 'trim|required|xss_clean');

// Collectiong User Input
$fullname = ucwords($this->input->post('i_fullname'));
$birthdate= convert_datetime($this->input->post('i_birthdate'), 'mysql');
$country= ucwords($this->input->post('i_country'));
$city = ucwords($this->input->post('i_city'));
$email = $this->input->post('i_email');
$username = $this->input->post('i_username2');
$password = $this->input->post('i_password2');
$password_confirm = $this->input->post('i_password2_confirm');
$captcha = strtoupper($this->input->post('i_captcha'));

All of the functions above can be called from within you're validation, so all you need to do is access the data using $this->input->post('field_name');

2) Again, you're accessing the post array before it's been validated and sanitized. You should either access it when validation is complete, or if you absolutely must use it, it's wise to sanitise it first by passing TRUE in as the second parameter to $this->input->post();
Code:
$email = $this->input->post('i_email', TRUE);

3) Validation callbacks can be in their own helper, or even model if you wish. You can also store them in a config file (Please see the [url="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#savingtoconfig"]user guide[/url] for further information), or extend the validation helper.
#3

[eluser]mTuran[/eluser]
Thanks for your valuble post. I understand answers of my question 1 and 2. Can you explain more question 3 ? "Validation callbacks can be in their own helper, or even model if you wish." is this mean i have to write same validation callbacks for every different controller again and again ?
#4

[eluser]TheFuzzy0ne[/eluser]
No, the whole point of this is that you can re-use the validation rules over and over again if you want.
#5

[eluser]mTuran[/eluser]
[quote author="TheFuzzy0ne" date="1245602187"]No, the whole point of this is that you can re-use the validation rules over and over again if you want.[/quote]

i think you misunderstood me. I know i can re-use validation RULES again and again by saving $config variable. My question is can i re-use validation CALLBACKS in different controllers like username callback. Thank you
#6

[eluser]TheFuzzy0ne[/eluser]
Yes, although it's not quite as easy. Just put your callback into a helper, model, library or whereever else you want, and then you can simply reference that call back from your controller with an alias.

Code:
function _my_callback($str="")
{
    return $this->my_validation_model->my_callback($str);
}

I think you can also access it in a model/library directly by defining you're rule something like this:
Code:
...
'rules' => 'callback_my_model->some_method',
...
#7

[eluser]mTuran[/eluser]
Thanks i will try as soon as possible.




Theme © iAndrew 2016 - Forum software by © MyBB