Validate fields not in the database
flag
4 messages - Collapse all
/groups/adfetch?adid=9oMBixIAAAAsLS8JFcfPWuEESvlpmbi7xyxpo7bMRVgpn9ZMKisPfQ
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  Devo  
View profile  
 More options Jul 25 2006, 8:11 pm
From: "Devo" <Devin.Scott.H...@gmail.com>
Date: Wed, 26 Jul 2006 00:11:47 -0000
Local: Tues, Jul 25 2006 8:11 pm
Subject: Validate fields not in the database
I'm working my way toward Advanced Validation Errors, but am struggling
with simple validation for fields that do no exist in my database.  My
Cake instance first checks to see if a user has access to the site by
checking the database for the user (passwords are not kept here),
thereafter checking LDAP to verify their username/password credentials.
 The database and LDAP calls work fine.  Right now I'm just trying to
get it to recognize VALID_NOT_EMPTY on the username and password fields
in my login page.

--------------------------------------------------------------------------- --------------------------

class User extends AppModel
{
        var $name = 'User';
        var $validate   = array (
                'username' => VALID_NOT_EMPTY,
                'password' => VALID_NOT_EMPTY
        );

}

--------------------------------------------------------------------------- --------------------------

class UserController extends AppController
{
        function login ()
        {
                // Page was loaded for the first time
                if ( empty ( $this -> params['data'] ) )
                {
                        $this -> render();
                }
                // It's a submit
                else
                {
                        $this->set ( 'data', $this->params['data'] );

                        // Get parameters from the form
                        $username = ( $this -> data['user']['username'] );
                        $password = ( $this -> data['user']['password'] );

                        if ( empty ( $username ) )
                        {
                                $this -> User -> invalidate ( 'username' );
                        }
                        if ( empty ( $password ) )
                        {
                                $this -> User -> invalidate ( 'password' );
                        }

                        // Everything's valid
                        if ( $this -> User -> validate ( 'username' ) == 0 )
                        {
                                // Query DB for username
                                $user = $this -> User -> findByUsername ( $username );

                                // If user was in DB, authenticate with LDAP
                                if ( !empty ( $user ) )
                                {
                                        $ldap = new LdapUser();
                                        if ( $ldap -> auth ( $username, $password ) )
                                        {
                                                $this -> Session -> write ( 'valid_user', 1 );
                                                $this -> redirect ( '/' );
                                        }
                                        else
                                        {
                                                // Custom error to come
                                        }
                                }
                                // Set error and redirect for login
                                else
                                {
                                        // Custom error to come
                                }
                        }
                }
        }

        function logout ()
        {
                $this -> Session -> write ( 'valid_user', 0 );
                $this -> redirect ( '/user/login' );
        }

}

--------------------------------------------------------------------------- --------------------------

<?php echo $html->input ( 'user/username', array ( 'size' => '40' ) );
?>
<?php echo $html->tagErrorMsg ( 'user/username', 'Username is required'
); ?>

<?php echo $html->password ('user/password', array ( 'size' => '40' )
); ?>
<?php echo $html->tagErrorMsg ( 'user/password', 'Password is required'
); ?>

--------------------------------------------------------------------------- --------------------------
I guess I'm misunderstanding how the validate method works:

e.g. if ( $this -> User -> validate ( 'username' ) == 0 )

Since I'm not calling save(), I believed that validate() would do the
same validation that save() does.  In my controller above, validate()
is always returning true and none of the error tags are being
populated.  (I read that calling invalidate() on each applicable field
would manually enable the tags.)  Is there something that I'm missing?
Or if I'm misunderstanding the logic/flow, could someone please
explain.

Thanks,

Devo


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
2.  Devo  
View profile  
 More options Jul 26 2006, 12:12 pm
From: "Devo" <Devin.Scott.H...@gmail.com>
Date: Wed, 26 Jul 2006 16:12:50 -0000
Local: Wed, Jul 26 2006 12:12 pm
Subject: Re: Validate fields not in the database
Ok, I realized that the validate() method should really be the
following:

if ( $this -> User -> validates ( $this -> data ) )

Program execution is entering the if statements as it should, but the
tagErrorMsg's are still not being displayed.  

Thanks,

Devo


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
3.  Devo  
View profile  
 More options Jul 26 2006, 12:17 pm
From: "Devo" <Devin.Scott.H...@gmail.com>
Date: Wed, 26 Jul 2006 16:17:16 -0000
Local: Wed, Jul 26 2006 12:17 pm
Subject: Re: Validate fields not in the database
Also, I realized that I was missing an else:

if ( $this -> User -> validates ( $this -> data ) )
{
        // OK, good to go

}

else
{
        $this->set( 'data', $this->params['data'] );
        $this->validateErrors( $this->User );
        $this->render();

}

But still no tagErrorMsg's.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
4.  Devo  
View profile  
 More options Jul 26 2006, 1:22 pm
From: "Devo" <Devin.Scott.H...@gmail.com>
Date: Wed, 26 Jul 2006 17:22:22 -0000
Local: Wed, Jul 26 2006 1:22 pm
Subject: Re: Validate fields not in the database
Sheeesh, the problem was that I had lower-case model names in my
tagErrorMsg's.  Changed to upper case and simple validation is working
great.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google