Solved

Help with a Kohana ORM Validation problem

Posted on 2014-09-15
4
468 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now