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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to