SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quickform Experts - Problem with Validation

    If I use a If then Else statement when using Quickform, my elements are not validating. I also even tried using a switch command.

    Here is an example of how this is set up using IF-Then...

    PHP Code:
        // Add a header to the form
        
    $form->addElement('header''header''User Information');

        
    // Register the COMPARE function
        
    $form->registerRule('compare''function''compare_field');

        
    //Register the UNIQUE E-mail function
       
    $form->registerRule('unique_Email','function','duplicate_Email');            
        
        
    $form->addElement('hidden''user_id');    
                    
    //LET THE USER EDIT THE RECORD
            
    if ($action=='edit') {
                
    // The login field
                
    $form->addElement('text','login','User Name:','class="signupData"');
                
    $form->freeze('login');
            
                
    // The email field
                
    $form->addElement('text','email','Email Address:','class="signupData"');
                
    $form->addRule('email','Please an email address','required',false,'client');
                
    $form->addRule('email','Please enter a valid email address','email',false,'client');
                
    $form->addRule('email','Email cannot be more than 50 characters','maxlength',50,'client');
                
    $form->addRule('email','Email address is already being used','unique_Email');    
                
                
    // The field for confirming the email
                
    $form->addElement('text','confirmEmail','Confirm Email Address:','class="signupData"');
                
    $form->addRule('confirmEmail','Please confirm email address','required',false,'client');
                
    $form->addRule('confirmEmail','Email address must match','compare','email');
            
                
    // The first name field
                
    $form->addElement('text','firstName','First Name:','class="signupData"');
                
    $form->addRule('firstName','Please enter your first name','required',false,'client');
                
    $form->addRule('firstName','First name cannot be more than 50 characters','maxlength',50,'client');
            
                
    // The last name field
                
    $form->addElement('text','lastName','Last Name:','class="signupData"');
                
    $form->addRule('lastName','Please enter your last name','required',false,'client');
                
    $form->addRule('lastName','Last name cannot be more than 50 characters','maxlength',50,'client');
                
                
    // Add a submit button called submit and "Send" as the text for the button
                
    $form->addElement('submit','submit','Update','class="createAccount"');
            
                
    // Specify the "required field" note for the bottom of the form
                
    $form->setRequiredNote('<span class="required">*</span> required');
            
            } else {
                             
    //LET THE USER VIEW THE RECORD    
                // The login field
                
    $form->addElement('text','login','User Name:','class="signupData"');
            
                
    // The email field
                
    $form->addElement('text','email','Email Address:','class="signupData"');
                
                
    // The first name field
                
    $form->addElement('text','firstName','First Name:','class="signupData"');
            
                
    // The last name field
                
    $form->addElement('text','lastName','Last Name:','class="signupData"');
                
                
    $form->freeze();
            }    

        
    // Display Form
        
    $form->setDefaults($userSearch);

        if ( 
    $form->validate() ) {
            
    $display "<p><b>The record has been Updated</b></p>";    
            
    $display.=$form->toHtml();
            break;
            }
        } else {
        
    $display=$form->toHtml();    
        } 
    The form will validate IF I dont use an IF THEN statement. In my example below, if I made it to look like this instead...

    PHP Code:
        // Add a header to the form
        
    $form->addElement('header''header''User Information');

        
    // Register the COMPARE function
        
    $form->registerRule('compare''function''compare_field');

        
    //Register the UNIQUE E-mail function
        
    $form->registerRule('unique_Email','function','duplicate_Email');            
        
        
    $form->addElement('hidden''user_id');    

                
    // The login field
                
    $form->addElement('text','login','User Name:','class="signupData"');
                
    $form->freeze('login');
            
                
    // The email field
                
    $form->addElement('text','email','Email Address:','class="signupData"');
                
    $form->addRule('email','Please an email address','required',false,'client');
                
    $form->addRule('email','Please enter a valid email address','email',false,'client');
                
    $form->addRule('email','Email cannot be more than 50 characters','maxlength',50,'client');
                
    $form->addRule('email','Email address is already being used','unique_Email');    
                
                
    // The field for confirming the email
                
    $form->addElement('text','confirmEmail','Confirm Email Address:','class="signupData"');
                
    $form->addRule('confirmEmail','Please confirm email address','required',false,'client');
                
    $form->addRule('confirmEmail','Email address must match','compare','email');
            
                
    // The first name field
                
    $form->addElement('text','firstName','First Name:','class="signupData"');
                
    $form->addRule('firstName','Please enter your first name','required',false,'client');
                
    $form->addRule('firstName','First name cannot be more than 50 characters','maxlength',50,'client');
            
                
    // The last name field
                
    $form->addElement('text','lastName','Last Name:','class="signupData"');
                
    $form->addRule('lastName','Please enter your last name','required',false,'client');
                
    $form->addRule('lastName','Last name cannot be more than 50 characters','maxlength',50,'client');
                
                
    // Add a submit button called submit and "Send" as the text for the button
                
    $form->addElement('submit','submit','Update','class="createAccount"');
            
                
    // Specify the "required field" note for the bottom of the form
                
    $form->setRequiredNote('<span class="required">*</span> required');

        
    // Display Form
        
    $form->setDefaults($userSearch);

        if ( 
    $form->validate() ) {
            
    $display "<p><b>The record has been Updated</b></p>";    
            
    $display.=$form->toHtml();
        } else {
        
    $display=$form->toHtml();    
        } 
    I would like to use the QUICKFORM so I can let the user VIEW it with the fields frozen or the opportunity to EDIT it. Does QUICKFORM allow IF THEN statements or Switch statement in the setup?

    Thanks for anyone responding.

  2. #2
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not make it easy on yourself and wrap them in a class. Then you could have functions like UserForms::getViewRecordForm($id) and UserForms::getEditRecordForm($id).

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    conditionals with HTML QF

    Yes, you can you if - else and switch - case to set up the form. Does it work without the freeze lines?

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cuberoot View Post
    Why not make it easy on yourself and wrap them in a class. Then you could have functions like UserForms::getViewRecordForm($id) and UserForms::getEditRecordForm($id).

    Im relatively a noob to using Classes. I am following a tutorial and trying to get a grasp of it. I definitely see your way of doing it..unfortunately I will be stumbling through the process of doing your suggestion for now. I will give it a try. I was hoping the if then would work for now so I can move on with my project.

    I appreciate your response.

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Yes, you can you if - else and switch - case to set up the form. Does it work without the freeze lines?
    I commented the freeze lines and still not validating properly. Note: The client javascript validation does work, but the other validation processes blows through it.

    Also, the if-else and switch does work on the form setup...that does work. But the validation is what is not working properly.

    If I take out one of the form setups, the validation works, but if I try to break it up into 2 different forms, that is where it doesnt work.

  6. #6
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey imchaz,

    It would look a little something like...

    Code:
    <?php
    
    class UserForms {
    
        function &getEditRecord($data) {
            $form = new HTML_QuickForm(...);
            $form->addElement(...);
            $form->addRule(...);
            $form->setDefaults($data);
            return $form;
        }
    
        function &getViewRecord($data) {
            $form = new HTML_QuickForm(...);
            $form->addElement(...);
            $form->addRule(...);
            $form->setConstants($data);
            $form->freeze();
            return $form;
        }
    
    }
    
    // you may want to adopt the practice of putting constant terms
    // on the left hand side when doing comparisons
    // this way forgetting an equals sign doesn't turn into a bug hunt
    if ('edit' == $action) {
    
        $form =& UserForms::getEditRecord($userData);
    
        // it only makes since to validate data when it's coming from the user
        if ($form->validate()) {
            $form->process(...);
        }
    
    } else {
    
        $form =& UserForms::getViewRecord($userData);
    
    }
    
    echo $form->toHtml();
    
    ?>

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cuberoot,

    Thankyou, Thankyou, Thankyou, Thankyou!!!!

    I will look over this and fill in the blanks. You gave me a great start/idea on how to accomplish this.

    Some of the coding is still strange to me

    i.e.
    1. the "&" symbol before the function (What does this do for me?)
    2. The $userData variable...what does it contain?

    If you can give me a quick and dirty answer, that would be great. But, no need to, I guess I can try to figure it out. YOu already went beyond the call for me.

    BTW, I am using PHP 4.3. Yes I know there are other later versions out there but I have to code in PHP4 because my Company website provider is still using PHP4 module and our company have no plans of changing provider. I assume this format is still fine.

    I really appreciate this. I will PM you if I am successful.

    Thanks again.

  8. #8
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad I could help.

    To answer your questions --

    1) The & symbol is telling PHP to return a reference to the object rather than a copy. Returning a copy can mess things up and slow things down. It's actually a bit more complicated than that, but I don't have much time.

    You can find out more here -- http://php.net/references

    2) $userData is just an associative array of field names and values. Supposing you've added a field named 'username', you can assign a default value with $form->setDefaults(array('username' => 'cuberoot')).

    And PHP 4.3 shouldn't pose a problem.

    Best wishes,
    Denny

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Denny,
    I appreciate you taking the time to respond. I shall look at the info as suggested.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •