emreakay / CodeIgniter-Aauth

Authorization, Authentication and User Management library for Codeigniter 2.x and 3.x to make easy user management and permission operations
http://emreakay.com
GNU Lesser General Public License v3.0
393 stars 235 forks source link

Display error message #120

Closed Firefly16 closed 8 years ago

Firefly16 commented 8 years ago

Hi again !

I don't really understand how to display errors. For example, I have a sign up system, when the username or email is already taken it doesn't go on the DB, but i have to do errors message system myself, and i want to use your because it seems simplier.

What i tried is: echo $data['errors'] = $this->aauth->print_errors($divider = '<br />'); but no success :/

REJack commented 8 years ago

i've used the print_errors like this

if($this->aauth->create_user($email, $password $name)){
  //success
  redirect ($another_page);
}else{
  //failure on creation
  echo $this->aauth->print_errors();
}

the $divider = '<br />' is default.

if you write me more of your code, then i can better help you :smiley:

REJack commented 8 years ago

idk if this work echo $data['errors'] = $this->aauth->print_errors($divider = '<br />'); try it without $divider = i think this is your problem.

if you want to change the divider only then write it like this echo $data['errors'] = $this->aauth->print_errors('<hr />'); example with a <hr /> as divider.

Firefly16 commented 8 years ago
public function create_user(){

        $this->form_validation->set_rules('nickname', 'Nickname', 'trim|required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
        $this->form_validation->set_rules('address', 'Address', 'trim|min_length[2]|alpha_numeric_spaces');

        if($this->form_validation->run()){
            //  Le formulaire est valide
            $data['nickname'] = $this->input->post('nickname');
            $data['password'] = $this->input->post('password');
            $data['address'] = $this->input->post('address');
            $data['email'] = $this->input->post('email');
            $query = $this->db->get('aauth_users');
            foreach ($query->result() as $row){

                if($row->email == $data['email']){
                    $exist = 1;
                }
            }

            if(isset($exist) && $exist == 1){ // a faire avec les erros de aauth
                $data['exist'] = $exist;
                echo $this->aauth->print_errors();
                $this->load->view('inscription_view',$data); 
            }else{
                $this->aauth->create_user($data['email'], $data['password'], $data['nickname']);

                if(isset($data['address']) && $data['address'] != ''){ // test d'ajout de l'addresse
                   $this->aauth->set_user_var('address', $data['address']);
                }

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

        }else{
            //  Le formulaire est invalide
            $data['signup'] = 1;
            $this->load->view('inscription_view',$data); //inscription_view

        }
    }

I tried your echo $this->aauth->print_errors(); doesn't work too :s

REJack commented 8 years ago
public function create_user(){

        $this->form_validation->set_rules('nickname', 'Nickname', 'trim|required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
        $this->form_validation->set_rules('address', 'Address', 'trim|min_length[2]|alpha_numeric_spaces');

        if($this->form_validation->run()){
            //  Le formulaire est valide
            $data['nickname'] = $this->input->post('nickname');
            $data['password'] = $this->input->post('password');
            $data['address'] = $this->input->post('address');
            $data['email'] = $this->input->post('email');

            if($this->aauth->create_user($data['email'], $data['password'], $data['nickname'])){ 
                if(isset($data['address']) && $data['address'] != ''){ // test d'ajout de l'addresse
                   $this->aauth->set_user_var('address', $data['address']);
                }
                $this->load->view('confirmation_view',$data);
            }else{
                echo $this->aauth->print_errors();
                $this->load->view('inscription_view',$data); 
            }

        }else{
            //  Le formulaire est invalide
            $data['signup'] = 1;
            $this->load->view('inscription_view',$data); //inscription_view

        }
    }

try this your extra check for existing mail is not needed

REJack commented 8 years ago

the create_user() creates the error messages. you skipped it trough your extra check.

an other way are to create your own error message instead of Aauth's

REJack commented 8 years ago

the other way :smile:

public function create_user(){

        $this->form_validation->set_rules('nickname', 'Nickname', 'trim|required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
        $this->form_validation->set_rules('address', 'Address', 'trim|min_length[2]|alpha_numeric_spaces');

        if($this->form_validation->run()){
            //  Le formulaire est valide
            $data['nickname'] = $this->input->post('nickname');
            $data['password'] = $this->input->post('password');
            $data['address'] = $this->input->post('address');
            $data['email'] = $this->input->post('email');
            $query = $this->db->get('aauth_users');
            foreach ($query->result() as $row){

                if($row->email == $data['email']){
                    $exist = 1;
                }
            }

            if(isset($exist) && $exist == 1){ // a faire avec les erros de aauth
                $data['exist'] = $exist;
                $this->aauth->error('<YOUR ERROR MESSAGE>');
                echo $this->aauth->print_errors();
                $this->load->view('inscription_view',$data); 
            }else{
                $this->aauth->create_user($data['email'], $data['password'], $data['nickname']);

                if(isset($data['address']) && $data['address'] != ''){ // test d'ajout de l'addresse
                   $this->aauth->set_user_var('address', $data['address']);
                }

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

        }else{
            //  Le formulaire est invalide
            $data['signup'] = 1;
            $this->load->view('inscription_view',$data); //inscription_view

        }
    }
Firefly16 commented 8 years ago

In fact my extra check was to display a php block on the view because i wasn't able to display the aauth message error, but now it work, thx you :)

And, as long as you are here, the set_user_var dont give errors but i cant find my "address" row in all aauth table :s

Firefly16 commented 8 years ago

(I read in an other post that create the address row before was not usefull)

REJack commented 8 years ago

which version you use without the extra check or with the extra check?

Firefly16 commented 8 years ago

without, your first solution :)

REJack commented 8 years ago

ok

public function create_user(){

        $this->form_validation->set_rules('nickname', 'Nickname', 'trim|required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[60]|alpha_numeric_spaces');
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
        $this->form_validation->set_rules('address', 'Address', 'trim|min_length[2]|alpha_numeric_spaces');

        if($this->form_validation->run()){
            //  Le formulaire est valide
            $data['nickname'] = $this->input->post('nickname');
            $data['password'] = $this->input->post('password');
            $data['address'] = $this->input->post('address');
            $data['email'] = $this->input->post('email');
            $user_id = $this->aauth->create_user($data['email'], $data['password'], $data['nickname'])
            if($user_id){ 
                if(isset($data['address']) && $data['address'] != ''){ // test d'ajout de l'addresse
                   $this->aauth->set_user_var('address', $data['address'], $user_id);
                }
                $this->load->view('confirmation_view',$data);
            }else{
                echo $this->aauth->print_errors();
                $this->load->view('inscription_view',$data); 
            }

        }else{
            //  Le formulaire est invalide
            $data['signup'] = 1;
            $this->load->view('inscription_view',$data); //inscription_view

        }
    }

set_user_var works only without an user_id if you logged in.

Firefly16 commented 8 years ago

I will give you a feedback tomorrow morning, i cant anymore today :s but i didnt noticed than the function create_user return an id, so i guess its ok if you want me to close the issue now :)

Firefly16 commented 8 years ago

Hi, i have errors and the address go on my DB :)

But I have a cosmetic probleme, and i think it's due to thetype of the variable.

if($user_id){ 
                if(isset($address) && $address != ''){ // test d'ajout de l'addresse
                   $this->aauth->set_user_var('address', $address, $user_id);
                }
                $this->load->view('confirmation_view',$data);
            }else{
                $data['errors'] = $this->aauth->print_errors();
                $this->load->view('inscription_view',$data); 
            }

View:

<?php
    if(isset($errors)){
?>
                    <div class="inscription_error">
                        Errors: <?php echo $errors; ?>
                    </div>
<?php 
    }
?>

1) if(isset($errors)) my

isn't displayed, but i have the errors messages, that's weird. 2) the errors messages go to the top left of the page even when i dont but a condition. So, when the div bloc is displayed with only this code:

<div class="inscription_error">
     Errors: <?php echo $errors; ?>
</div>
REJack commented 8 years ago

try to change if(isset($errors)){ with if(isset($errors) && !empty($errors)){

Firefly16 commented 8 years ago

doesn't work :s

Firefly16 commented 8 years ago

I can't show you my view, it bugs, but even if we found how to display the div block, the error message will be in the top left of the page, is there any better way to show error to the user?

EDIT:

I changed the error text and putted a <div class=""> on it, it work for the design :D I just only don't know why if(isset($errors)){ with if(isset($errors) && !empty($errors)){ dont work but i can do an other way for this problem :)

REJack commented 8 years ago

you try a fail creation right? does it transfer the errors to the view?

Firefly16 commented 8 years ago

Yep, i tried a fail connection (already exist name & email), and i "echo" the error only on the view, and i see them (on the top left of the page in place of in my div ^^ ).

EDIT:

Screen of what happend. The "errors" bloc under "signup" is display because i deleted the condition, else he didn't come. http://imgur.com/ZRidDPW

REJack commented 8 years ago

your div should work normal if you took it around the error.

REJack commented 8 years ago

you can use less <?php ?>-tags too like this

<?php
    if(isset($errors)){
         echo '<div class="inscription_error">';
         echo 'Errors: '.$errors;
         echo '</div>';
    }
?>
Firefly16 commented 8 years ago

Ok, but still the problem.

Are you sure the print_errors can be designed? Perhaps he is like an echo and will always go to the top left of the page.

REJack commented 8 years ago

u can place a echo any where, if you echo a div around its inside a div. if its stuck on the top left check your css, for problems like this you can inspect really good it with a Browser Dev tool like firebug or check the html source code trough your browser

Firefly16 commented 8 years ago

That's why i already do with chrome, and the div are juste under the body

REJack commented 8 years ago

what around the body? can you send me your view file? i think u have a failure in it.

Firefly16 commented 8 years ago

How can i? When i put here, it bugs ^^'

REJack commented 8 years ago

zip it ^^

REJack commented 8 years ago

or change the extension from php to phps

Firefly16 commented 8 years ago

it's zip but anyway: Unfortunately, we don’t support that file type. Sélect. fichiers Try again with a PNG, GIF, JPG, DOCX, PPTX, XLSX, TXT, PDF, or ZIP. ........

REJack commented 8 years ago

ok then put the view content in a ``` ^^

Firefly16 commented 8 years ago

`<!DOCTYPE html>

```

Sign Up


``` '; echo 'Errors: '.$errors; echo '
'; } ?>

```


* Required Field
``` `
Firefly16 commented 8 years ago

it breaks

REJack commented 8 years ago

take it in a Quoting code ```

` is only for inline :D

Firefly16 commented 8 years ago

i'm not very good at english so i don't really understand :D

REJack commented 8 years ago

https://help.github.com/articles/basic-writing-and-formatting-syntax/#quoting-code

Firefly16 commented 8 years ago
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="Une description de la page" />
    <meta name="keywords" content="a definir !!!!!!!!!!!!" />
    <title></title>

    <!-- Bootstrap -->
    <link href="<?php echo base_url('bootstrap/css/bootstrap.min.css'); ?>" rel="stylesheet">
    <link href="<?php echo base_url('bootstrap/css/main.css'); ?>" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    </head>
    <body>
        <div class="row">
            <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                <div class="inscription_container">
                    <h3>Sign Up</h3>
                    <hr>

<?php
    if(isset($again) && $again == 1){
         echo '<div class="inscription_error">';
         echo 'Errors: '.$errors;
         echo '</div>';
    }
?>
                    <form action="http://localhost/codeigniter-3.0.6/index.php/login/create_user" method="post">
                        <?php echo form_error('nickname'); ?>
                        <span class="inscription_label"><label for="nickname">Nickname:*</label></span>
                        <span class="inscription_input"><input type="text" name="nickname" value="<?php echo set_value('pseudo'); ?>"></span><br>

                        <?php echo form_error('password'); ?>
                        <span class="inscription_label"><label for="password">Password:*</label></span>
                        <span class="inscription_input"><input type="password" name="password" value="<?php echo set_value('pseudo'); ?>"></span><br>

                        <?php echo form_error('email'); ?>
                        <span class="inscription_label"><label for="email">E-mail:*</label></span>
                        <span class="inscription_input"><input type="text" name="email"></span><br>

                        <?php echo form_error('address'); ?>
                        <span class="inscription_label"><label for="address">Country:</label></span>
                        <span class="inscription_input"><input type="text" name="address"></span><br>

                        <span class="inscription_required">* Required Field</span>

                        <input type="submit" value="Send" value="<?php echo set_value('pseudo'); ?>">
                    </form>
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
            </div>
        </div>

           <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="<?php echo base_url('bootstrap/js/bootstrap.min.js'); ?>"></script>
    </body>
</html>
REJack commented 8 years ago

in this view is the div around your body?

Firefly16 commented 8 years ago

You have all the view, so error is in <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4"> who is in <div class="inscription_container"> who is in <div class="row"> who is in <body> .

REJack commented 8 years ago

yes and the div goes where?

Firefly16 commented 8 years ago

just under the <body>

REJack commented 8 years ago

under the <body>?

Firefly16 commented 8 years ago

yep

REJack commented 8 years ago

curious it should placed around the error

Firefly16 commented 8 years ago

ho, i just saw that, meta are under error Oo

REJack commented 8 years ago

really strange ^^

Firefly16 commented 8 years ago

http://imgur.com/iQY1pVm

REJack commented 8 years ago

which php version you use?

Firefly16 commented 8 years ago

5.5.34

Firefly16 commented 8 years ago

Ho, i think i have a start of answer:

$data['errors'] = $this->aauth->print_errors(); this print automaticaly the errors, even if we just wanted to put the errors in the $data ><

EDIT:

But so, i don't know how to put errors in variable without printing them in the controller :(

REJack commented 8 years ago

ah yeah that i haven't seen it ^^

you could use the print_errors() without an echo because it echo's from it self (inside the function).

a other way is get_errors_array() in controller instead print_errors(), but then you need a foreach in your view

REJack commented 8 years ago

that was my failure :sweat_smile: ^^

i doesn't use the CI View files, i use a real template engine (twig) codeigniter-twiggy. its a lot easier to change the layout (not need to change every view :D)