?
Solved

Help with a Kohana ORM Validation problem

Posted on 2014-09-15
4
Medium Priority
?
550 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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).
Suggested Courses

765 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