Solved

Help with a Kohana ORM Validation problem

Posted on 2014-09-15
4
525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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.

717 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