Yii Framework Forum: Testing for Login button click in CUserIdentity - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Testing for Login button click in CUserIdentity When enableAjaxValidation is set to true, CUserIdentity logic calls DB Rate Topic: -----

#1 User is offline   Scott Deagan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 04-February 10

Posted 21 October 2010 - 05:56 AM

Hi,

When the 'enableAjaxValidation' is set to true for the CActiveForm widget in the auto-generated login.php view, the logic in CUserIdentity::authenticate() is called whenever ajax validation is performed within the login.php page (view). For example, if you want to perform ajax validation of an email field and your CUserIdentity::authenticate() method contains logic to validate user entered credentials against a database, then the database will be hit with a query when the username field on the login form loses focus.

What I want to do is retain the ajax validation of the username (in my case an email address) but only run the authentication logic in UserIdentity::authenticate() if the "Login" button has been pressed (and not when the email ajax validation is executed). For example:


  public function authenticate()
  {
    if (LOGIN_BUTTON_WAS_PRESSED)
    {
      QUERY_DB;
      AUTHENTICATE_USER;
    }
  }


I added a print_r($_POST) in the authenticate method, and get notice there's a [yt0] => Login entry. Should I just check the value of "yt0" to see if the Login button was pressed?

Thanks in advance.
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 21 October 2010 - 01:50 PM

yt0 is an ID that Yii framework generates automatically... it would be safer if you give to the submit button an ID that you can test afterwards...
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Scott Deagan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 04-February 10

Posted 22 October 2010 - 04:06 AM

Thanks for your reply. I have changed the name of the button in the view using:

<?php echo CHtml::submitButton('Login', array('name' => 'btnLogin')); ?>


In my UserIdentity component I'm now using:

  public function authenticate()
  {
    if (isset($_POST['btnLogin']))
    {
      if ($_POST['btnLogin'] == 'Login')
      {
        // Perform authentication here, set $this->errorCode accordingly...
        return !$this->errorCode;
      }
    }
    // Always return TRUE here since login button was NOT pressed.
    // This prevents the ajax validation failing when the textbox loses focus.
    return 1;
  }


Using the above I achieve what I wanted to - ajax validation without the authentication code being executed whenever textboxes lose focus.

Is there a better/cleaner way of achieving this?
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 22 October 2010 - 06:27 AM

I still don't really understand what you want/don't want to do. But it sounds like scenarios could help you. You can define different sets of attributes to validate in each scenario. Then all you do is set different scenarios for ajax requests and standard form submission.

Scenarios are explained in more detail here:
http://www.yiiframew...1/en/form.model
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users