Solved

Help with a Kohana ORM Validation problem

Posted on 2014-09-15
4
486 Views
Last Modified: 2014-10-19
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
0
Comment
Question by:William-B
  • 3
4 Comments
 
LVL 15

Expert Comment

by:Insoftservice
ID: 40325367
Please do let me know the version of kohana you are using.
0
 

Author Comment

by:William-B
ID: 40325393
Right now I am using 3.3.1.
0
 

Accepted Solution

by:
William-B earned 0 total points
ID: 40381487
Found it.  Turns out User_Auth has 3 fields to validate: email, username, and password.  So those have to be used.
0
 

Author Closing Comment

by:William-B
ID: 40389839
I looked through the UserAuth.php file along with the other ORM files.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question