1

The controller function:

function signup()
        {

            $bool1 = $this->form_validation->run('username');
            $bool2 = $this->form_validation->run('email');
            $bool3 = $this->form_validation->run('pass');

            var_dump($bool1);
            var_dump($bool2);
            var_dump($bool3);

            $this->load->view('myform');

        }

The view code:

<?php $this->load->view('includes/header'); ?>


<?php

    $data_username = array(

        'name' => 'username',
        'onBlur' => 'username_val()',
        'maxlength' => 50,

    );

    $data_email = array(

        'name' => 'email',
        'onBlur' => 'email_val()',
        'maxlength' => 50

    );

    $data_pass = array(

        'name' => 'pass',
        'onBlur' => 'pass_val()',
        'maxlength' => 50,
        'autocomplete' => 'off'

    );

?>

    <div id="body">

        <div id="bodycontent">

            <table class="login_table">

            <?php $attr = array( 'onsubmit' => 'return final_val()' ); echo form_open('signup',$attr); ?>

            <tr><td>Username: </td><td><?php echo form_input($data_username); ?><span name="username_error" >&nbsp;&nbsp;<?php echo form_error('username'); ?></span></td></tr>

            <tr><td>Email: </td><td><?php echo form_input($data_email); ?><span name="email_error">&nbsp;&nbsp;<?php echo form_error('email'); ?></span></td></tr>


            <tr><td>Password: </td><td><?php echo form_password($data_pass); ?><span name="pass_error">&nbsp;&nbsp;<?php echo form_error('pass'); ?></span></td></tr>

            <tr><td><input type="submit" value="SIGN UP!" name="signup"/></td></tr>

                <?php echo form_close(); ?>

            </table>


        </div>

    </div>

<?php $this->load->view('includes/footer'); ?>

Form Validation Config:

<?php
$config = array(
                 'username' => array(

                                    array(
                                            'field'   => 'username',
                                            'label' => 'nama',
                                            'rules' => 'required|min_length[1]|max_length[50]|strip_tags|encode_php_tags|htmlspecialchars'
                                         )

                                    ),
                 'email' => array(
                                    array(
                                            'field'   => 'email',
                                            'label' => 'EMAIL',
                                            'rules' => 'required|min_length[1]|max_length[50]|valid_email|strip_tags|encode_php_tags|htmlspecialchars'
                                         )
                                    ),     

                'pass' => array(
                        array(
                                'field'   => 'pass',
                                'label' => 'PASSWORD',
                                'rules' => 'required|min_length[6]|max_length[256]|strip_tags|encode_php_tags|htmlspecialchars'
                        )
                ),/*

                'passconf' => array(
                        array(
                                'field'   => 'passconf',
                                'label' => 'PASS_CONF',
                                'rules' => 'required|min_length[6]|max_length[32]|matches[pass]|strip_tags|encode_php_tags|htmlspecialchars'
                        )
                ),*/

    'title' => array(

        array(
            'field'   => 'title',
            'label' => 'Title field',
            'rules' => 'required|min_length[1]|max_length[60]|strip_tags|encode_php_tags|htmlspecialchars'
        )

    )

               );



?>

Javascript is turned off.

The problem: Once the username field is submitted with any value, all returns true. And if all is given value except for username field, all returns false.

What's wrong with the code? Thanks.

UPDATE:

Thanks @TheSwiftExchange for pointing this out: You dont run each variable through the validation - you run the WHOLE form through the validation ONCE.

So, I changed the config file to:

$config = array(
                 'signup' => array(

                                    array(
                                            'field'   => 'username',
                                            'label' => 'nama',
                                            'rules' => 'required|min_length[1]|max_length[50]|strip_tags|encode_php_tags|htmlspecialchars'
                                         ),

                                     array(
                                         'field'   => 'email',
                                         'label' => 'EMAIL',
                                         'rules' => 'required|min_length[1]|max_length[50]|valid_email|strip_tags|encode_php_tags|htmlspecialchars'
                                     ),


                                     array(
                                         'field'   => 'pass',
                                         'label' => 'PASSWORD',
                                         'rules' => 'required|min_length[6]|max_length[256]|strip_tags|encode_php_tags|htmlspecialchars'
                                     )

                                  )
)

And it works nicely now. ;)

2
  • if ($this->form_validation->run() == true) Commented Jun 18, 2012 at 6:07
  • I've tried that before and it didn't work. I used == FALSE though, but I guess that won't make any difference. Commented Jun 18, 2012 at 6:09

1 Answer 1

1

You dont run each variable through the validation - you run the WHOLE form through the validation ONCE.

function signup()
    {

        if ($this->form_validation->run())
        {
            echo "ok";
        }
        else
        {
            echo validation_errors();
        }

        $this->load->view('myform');

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.