Welcome Guest, Not a member yet? Register   Sign In
Yet another validation question
#1

[eluser]yello[/eluser]
Hi!

I am building some kind of social networking website right now and I am having this dilema:

my urls look like this:

domain.com/browse/state/city/username

each username has a form on their page so people can send them message... i'm trying to set simple validations on each of the profile's pages...

Code:
function send()
    {
        $this->load->library('validation');
        
        $rules['name']    = "required";
        $rules['email']        = "required";
        
        $this->validation->set_rules($rules);
        if ($this->validation->run() == FALSE)
        {
            *What do I put here to make them redirect to the user profile page?*
        }
        else
        {
            echo("success");
        }
    }

Since it is not a 'static' form with a static url/view, I don't know how to redirect the user on the correct user profile if their is an error in the validation process.

Can someone help me on this one?
Thanks!
#2

[eluser]danoph[/eluser]
replace

Code:
if ($this->validation->run() == FALSE)
        {
            *What do I put here to make them redirect to the user profile page?*
        }

with

Code:
if ($this->validation->run() == FALSE)
        {
            $this->correct_user_profile_function();
        }
#3

[eluser]charlieD[/eluser]
If you include the URL helper, you can use the following to redirect a user within your application.

Code:
//Load URL helper; this includes the redirect() function
$this->load->helper('url');

redirect('browse/state/city/username'); //Relative to your domain
#4

[eluser]yello[/eluser]
charlieD, I have used your method but it seems that the "<?=$this->validation->error_string; ?>" does not work... any idea why?

Code:
<? $this->load->view('includes/header'); ?>
<?# var_dump($installer);?>
<div class="left_articles">
    <h2 id="profile_header">&lt;?=ucfirst($installer->username);?&gt;</h2>
    <p>Location: &lt;?=ucfirst(trim($installer->city));?&gt; &lt;? if (!empty($installer->state)) {echo ", $installer->state";} ?&gt;
    <p>Registred on &lt;?=$installer->date;?&gt;</p>
    </p>
    
    <h3>Details</h3>
    <p>&lt;?=$installer->details;?&gt;</p>
    <h3>Contact &lt;?=ucfirst($installer->username);?&gt;</h3>
    &lt;?=$this->validation->error_string; ?&gt;
    &lt;? echo form_open('email/send');?&gt;
    Name: &lt;? echo form_input('name', '');?&gt; <br>
    Email: &lt;? echo form_input('email', '');?&gt; <br>
    Phone number: &lt;? echo form_input('phone', '');?&gt; <br>
    Message: &lt;? echo form_textarea('message', '');?&gt;
    &lt;? echo form_hidden('username', $installer->username_uri);?&gt;
    &lt;? echo form_submit('mysubmit', 'Send Message!'); ?&gt;
    &lt;? echo form_close();?&gt;
    </div>
&lt;? $this->load->view('includes/footer'); ?&gt;
#5

[eluser]danoph[/eluser]
You should only redirect the user if all of the information is correct. If there is an error when a user is filling out a form and you redirect them, none of the validation data will be saved.

Here is an example of a controller function that handles a form submission (login form):

Code:
function login($data = array()) {
            // set required fields for validation
            $rules['username'] = "required";
            $this->validation->set_rules($rules);
            $fields['username'] = "Username";
            $this->validation->set_fields($fields);
            
            // if post variables are set, run validation
            if ($this->validation->run()) {
                $login_ok = $this->ezauth->login();    // $login_ok is true or false depending on user login information
                if ($login_ok['authorize'] == true) {
                    $this->ezauth->remember_user();        // store username in cookie
                    redirect('mystore');            // if user logs in successfully, redirect to main page
                } else {
                    $data['error_string'] = $login_ok['error'];
                }

            }
            $this->load->view('login_view',$data);
    }

This is an example from my EzAuth model I released about a week ago.
#6

[eluser]yello[/eluser]
The thing is that I use a view which used by tens of profiles... how to send back the user to the profile page with the validation errors activated?

It is not a static view like a login view, it is a form that is used to send the profile's owner an email on his profile page.
#7

[eluser]danoph[/eluser]
That's fine, just load the view or call the function inside the controller that loads the view, for example:

Code:
$this->load->view('profile_view');

or

Code:
$this->profile();

Both will save the validation data and display the error string, validation field data and messages, etc. If you use a redirect, they are all erased!
#8

[eluser]yello[/eluser]
The controller that sends the email and the controller that outputs the profile view are diffrent... is that a problem?

Profile Controller:
Code:
function profile()
    {
        $this->load->helper('form');
        $this->load->library('validation');
        
        $country_uri = $this->uri->segment(2);
        $state_uri = $this->uri->segment(3);
        $city_uri = $this->uri->segment(4);
        $username_uri = $this->uri->segment(5);
        $valid = $this->queries->is_valid_username($country_uri, $state_uri, $city_uri, $username_uri);

        if ($valid !== false)
        {
            $data['installer'] = $valid;
            $this->load->view('profile/profile', $data);
        }
    }

Email controller
Code:
function send()
    {
        $this->load->library('validation');
        $this->load->helper('url');
        
        $rules['name']    = "required";
        $rules['email']        = "required";
        
        $this->validation->set_rules($rules);
        if ($this->validation->run() == FALSE)
        {
            redirect('browse/united-states/bitch-state/biiiiitch/misstest'); //Relative to your domain
        }
        else
        {
            echo("success");
        }
    }
#9

[eluser]danoph[/eluser]
based on your email controller, you are redirecting to the browse function if there is an error? You should just load that view or call that function in the controller. Then it will save all of the validation data.
#10

[eluser]yello[/eluser]
I understand what you mean... but they are two diffrent controllers!

I can't just do $this->profile() since #1: It will not find the profile() function, #2: It won't keep the correct URI and without that, it won't send the user back to the correct profile page




Theme © iAndrew 2016 - Forum software by © MyBB