Form_Validate working only once.

I have created two modals, one for register, another for user login. However, it seems that the Form_Validate is working only once. I get the field required errors in Register form, but not in the login form. The Login form just doesnt work, no QAjaxAction takes place, and I have to set causes validation to false in order to make it work. Code below:

 

        //for register form

        protected $txtRegUsername;

        protected $txtRegEmail;

        protected $txtRegPassword;

        protected $txtRegPasswordAgain;

        protected $btnRegRegister;

        protected $lblRegRegisterErrors;

        protected $txtLogUsername;

        protected $txtLogPassword;

        protected $btnLogLogin;

        protected function Form_Create(){

            // code for register form

            $this->txtRegUsername = new QTextBox($this);

            $this->txtRegUsername->SetCustomAttribute('placeholder', 'Username');

            $this->txtRegUsername->Required = true;

            $this->txtRegEmail = new QTextBox($this);

            $this->txtRegEmail->SetCustomAttribute('placeholder', 'email@domain.com');

            $this->txtRegEmail->Required = true;

            $this->txtRegPassword = new QTextBox($this);

            $this->txtRegPassword->SetCustomAttribute('placeholder', '****');

            $this->txtRegPassword->TextMode = QTextMode::Password;

            $this->txtRegPassword->Required = true;

            $this->txtRegPasswordAgain = new QTextBox($this);

            $this->txtRegPasswordAgain->SetCustomAttribute('placeholder', '****');

            $this->txtRegPasswordAgain->TextMode = QTextMode::Password;

            $this->txtRegPasswordAgain->Required = true;

            $this->btnRegRegister = new QButton($this);

            $this->btnRegRegister->CssClass = 'btn btn-primary';

            $this->btnRegRegister->Text = "Register";

            $this->btnRegRegister->CausesValidation = true;

            $this->btnRegRegister->AddAction(new QClickEvent(), new QAjaxAction('user_register'));

            $this->lblRegRegisterErrors = new QLabel($this);

            $this->lblRegRegisterErrors->HtmlEntities = false;

            
            //code for login form

            $this->txtLogUsername = new QTextBox($this);

            $this->txtLogUsername->SetCustomAttribute('placeholder', 'Username');

            $this->txtLogUsername->Required = true;

            $this->txtLogPassword = new QTextBox($this);

            $this->txtLogPassword->SetCustomAttribute('placeholder', '****');

            $this->txtLogPassword->TextMode = QTextMode::Password;

            $this->txtLogPassword->Required = true;

            $this->btnLogLogin = new QButton($this);

            $this->btnLogLogin->CssClass = 'btn btn-primary';

            $this->btnLogLogin->Text = "Log In";

            $this->btnLogLogin->AddAction(new QClickEvent(), new QAjaxAction('user_login'));

        }

        protected function Form_Validate(){

            return true;

        }

        protected function Form_Validate_Register(){

            // username validation

            $userValid=false;

            if(Users::LoadByUsername($this->txtRegUsername->Text)!=null){

                $userValid=false;

                $this->lblRegRegisterErrors->Text.="<li>Username already exists.</li>";

            }

            else{

                $userValid=true;

            }

            //email validation

            $emailValid=false;

            if($this->isEmail($this->txtRegEmail->Text)==false){

                $emailValid=false;

                $this->lblRegRegisterErrors->Text.="<li>Enter correct E-mail address.</li>";

            }

            else if(Users::LoadByEmail($this->txtRegEmail->Text)!=null){

                $emailValid=false;

                $this->lblRegRegisterErrors->Text.="<li>Email already exists</li>";

            }

            else{

                $emailValid=true;

            }

            //password validation

            $passValid = false;

            if($this->txtRegPassword->Text == $this->txtRegPasswordAgain->Text){

                $passValid = true;

            }

            else{

                $this->lblRegRegisterErrors->Text.="<li>Passwords do not match.</li>";

            }

            if($userValid == true && $emailValid == true && $passValid==true){

                return true;

            }

            else{

                return false;

            }

        }

        protected function isEmail($strEmail){

            $i=0;

            $isEmailVal=false;

            while($i<strlen($strEmail)){

                if($strEmail{$i}=='@'){

                    $isEmailVal=true;

                    break;

                }

                $i++;

            }

            return $isEmailVal;

        }

        protected function user_register($strFormId, $strControlId, $strParameter){

            $this->btnRegRegister->Text = "Just a second..";

            $this->lblRegRegisterErrors->Text = "<p class='text-error'>The following errors occoured:<ul>";

            $this->lblRegRegisterErrors->Visible = false;

            if($this->Form_Validate_Register()){

                $objUser = new Users();

                $objUser->Username = $this->txtRegUsername->Text;

                $objUser->Email= $this->txtRegEmail->Text;

                $objUser->Password = $this->txtRegPassword->Text;

                $objUser->Save();

                $this->lblRegRegisterErrors->Text="<p class='text-success'>Successfully Registered! Please Log In to continue.";

                $this->lblRegRegisterErrors->Visible = true;

                $this->txtRegUsername->Text = "";

                $this->txtRegEmail->Text = "";

                $this->txtRegPassword->Text = "";

                $this->txtRegPasswordAgain->Text = "";

                $this->btnRegRegister->Text = "Register";

            }

            else{

                $this->lblRegRegisterErrors->Text.="</ul></p>";

                $this->lblRegRegisterErrors->Visible = true;

                $this->txtRegPassword->Text = "";

                $this->txtRegPasswordAgain->Text = "";

                $this->btnRegRegister->Text = "Re-Submit";

            }

        }

        protected function user_login($strFormId, $strControlId, $strParameter){

            $this->btnLogLogin->Text = "Just a second...";

        }

 

Login or Register to comment on this topic.
Re [Form_Validate working only once.]:

First of all - instead of 'SetCustomAttribute', just use the 'Placeholder' property of TextBox. Let me see the rest of the code. 

Permalink
Re [Form_Validate working only once.]:

This is because you have not implemented the Form_Validate function at all! All you are doing is to return true. Your actual validation is being done by the Form_Validate_Register() function which you call inside the user_register()  function!

Anyways, I was not able to understand what you are asking. Still, to make it more clear for you, here is how I see this piece of code working:

When you set a button's 'CausesValidation' property to true, what happens is that when it fires, it will first make the Form_Validate function be fired. If that function does not return true, then the event handler function will not be launched. However in your case it does! So the event handler should fire. But there is a rule with QCubed's QControls when it comes to Validation - the default validation will not be run on controls if you render them using the Render() function. The validation will work only if you use RenderWithError() or RenderWithName() function. Since you have not shown your template file, I cannot comment much, but I suspect you are not using either of those fucntions to render your controls. 

If possible, do show the template file as well. 

Permalink
Login or Register to comment on this topic.