Link to home
Start Free TrialLog in
Avatar of William-B
William-B

asked on

Help with a Kohana ORM Validation problem

Hello,
I am currently developing a user registration system.  Unfortunately I am running into a problem in the validation process.  I can initially create a user account, but when I add additional information, re-check it, and then save it displays an error.  I actually have two different models, one a user and one an account.  In order not to have accounts needlessly created I am checking to see if an email address is associated with an account already.  If it is, an error is shown, if it isn't the registration continues.  I'm also checking all the form inputted values before creating any of the models, again to keep unnecessary creations to a minimum.  Currently the email is checked, the form values are run through the validation and everything works up until then.  The problem arises when I try to save the user object.  Kohana gives me a "Failed to Validate Array" error with (it appears) 'username' not meeting the validation criteria.  This is odd since I don't have 'username' in my controller, model, or anywhere else.  The only 'username' usage I am aware of is the 'login' function, but in this instance I am using an email address to log in, so it should be fine.  Anyone have any ideas on what could be causing this?  I'm including some of my code along with the error messages below.  I'm also using MySQL as my database and it doesn't have any username columns.  My database connection is working alright since an account entry appears each time I run the page.  If anyone needs any additional code or output let me know.

Thanks

if( $this->request->method() === Request::POST )
        {
          
            $account = new Model_Account;
            $user = new Model_User;
            $post = $this->request->post();


            if( $user->where( 'email', '=', $post[ 'Email' ] )->count_all() === 0 )
            {
                $date = date( 'Y-m-d H:i:s', time() );
                $params = array( 'type' => 'user', 'registered_date' => $date, 'active' => true );
                $userparams = array();
                $userparams[ 'first_name' ] = $post[ 'FirstName' ];
                $userparams[ 'last_name' ] = $post[ 'LastName' ];
                $userparams[ 'gender' ] = $post[ 'Gender' ];
                $bd = $post[ 'Birthyear' ] . "-" . $post[ 'Birthmonth' ] . "-" . $post[ 'Birthday' ]; 
                $userparams[ 'birthdate' ] = $bd;
                $userparams[ 'email' ] = $post[ 'Email' ];
                $userparams[ 'emailconf' ] = $post[ 'Emailconf' ]; 
                $userparams[ 'password' ] = $post[ 'Password' ];
                $userparams[ 'passconf' ] = $post[ 'Passconf' ];
                $userparams[ 'account_id' ] = 0;
                //$userparams[ 'TOS' ] = $post[ 'TOS' ];
                $userparams[ 'zip_code' ] = $post[ 'ZipCode' ];
                $userparams[ 'auto_learn' ] = 1;

                $usercheck = $user->validate_create( $userparams );

                try
                {
                    $usercheck->check();
                    $user->values( $userparams );
                    $account->CreateAccount( $params );
                    echo 'account create ok ';
                    $addparams[ 'account_id' ] = $account->pk();
                    $addparams[ 'banned' ] = 0;
                    echo Debug::vars( $addparams );
                    $user->save();
                    echo '1st save ok ';
                    $user->values( $addparams );                   
                    echo 'before user->save ';
                    $user->save();
                    echo 'after user->save ';
                    $user->add( 'roles', ORM::factory( 'role' )->find(1) );
                    Auth::instance()->login( $this->request->post( 'Email' ), $this->request->post( 'Password' ) );
                    $this->redirect( 'userhome' );
                }
                catch( ORM_Validation_Exception $e )
                {   
                    echo 'in 1st catch ';
                    $errors = $e->errors();
                    echo Debug::vars( $errors );
                    $msg = $e->getMessage();
                    echo $msg;
                    $file = $e->getFile();
                    echo '          File: ' . $file . '          ';
                    $ln = $e->getLine();
                    echo 'Line: ' . $ln;
                }
            }
            else // Email exists
            {
                echo json_encode( $user->validation()->errors() );
            }

Open in new window


The output:

account create ok

array(2) (
    "account_id" => integer 118
    "banned" => integer 0
)

in 1st catch

array(1) (
    "username" => array(2) (
        0 => string(9) "not_empty"
        1 => array(1) (
            0 => NULL
        )
    )
)

Failed to validate array File: \modules\orm\classes\Kohana\ORM.php Line: 1275
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

Please do let me know the version of kohana you are using.
Avatar of William-B
William-B

ASKER

Right now I am using 3.3.1.
ASKER CERTIFIED SOLUTION
Avatar of William-B
William-B

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I looked through the UserAuth.php file along with the other ORM files.