Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-27-2007, 08:57 AM
Administrator
 
Join Date: Jan 2007
Posts: 12
Default Zend_Input_Filter problems and best practice question

Hi,

I'm having some issue with Zend_Filter_Input and wondered if anyone could help me? All I'm trying to do is have a form submitted, filter/validate it and then if it's not valid render the form again with the filtered data populated in the form elements, but I must be doing something stupid which is probably blindingly obvious to everyone except to me! :-)

My action looks like:

Code:
    public function registerAction()
   {
       if ($this->_request->isPost()) {
           $filters = array(
               '*' => array('StringTrim', 'StripTags', new Component_View_Filter_StripSlashes())
           );
           $validators = array(
               'email' => array(
                   'EmailAddress',
                   'allowEmpty' => false
               ),
               'password' => array(
                    array('StringLength', 6),
                   'allowEmpty' => false
               ),
               'confirm_password' => array(
                   'StringEquals',
                   'fields' => array('confirm_password', 'password'),
                   'allowEmpty' => false
               ),
               'username' => array(
                   'Alnum',
                   array('StringLength', 6, 32),
                   'allowEmpty' => false
               )
           );
           $input = new Zend_Filter_Input($filters, $validators, $this->_request->getPost());
           $input->addNamespace('Component_Validate');
           if ($input->isValid()) {
               echo "Form OK<br />\n";
           } else {
               $messages = $input->getMessages();
               $this->view->invalid = $messages;
           }

Zend_Debug::dump($this->_request->getPost());
Zend_Debug::dump($input);
Zend_Debug::dump($input->address2);

           $this->view->post = $input;
       }
       $this->view->countries = $this->_countries;
   }
The above debug output shows me that the form is being submitted OK, and the filters are being applied. But when I try to access the input value with the getter, I just get a null value:

Code:
array(14) {
 ["username"] => string(0) ""
 ["email"] => string(0) ""
 ["password"] => string(0) ""
 ["confirm_password"] => string(0) ""
 ["firstname"] => string(7) "  foo  "
 ["lastname"] => string(15) "bar            "
 ["address1"] => string(21) "<script>ack!</script>"
 ["address2"] => string(0) ""
 ["city"] => string(22) "          it\'s a test"
 ["county"] => string(0) ""
 ["postalcode"] => string(0) ""
 ["country"] => string(2) "GB"
 ["site_contact"] => string(1) "1"
 ["partner_contact"] => string(1) "1"
}

object(Zend_Filter_Input)#56 (13) {
 ["_data:protected"] => array(10) {
   ["firstname"] => string(3) "foo"
   ["lastname"] => string(3) "bar"
   ["address1"] => string(4) "ack!"
   ["address2"] => string(0) ""
   ["city"] => string(11) "it's a test"
   ["county"] => string(0) ""
   ["postalcode"] => string(0) ""
   ["country"] => string(2) "GB"
   ["site_contact"] => string(1) "1"
   ["partner_contact"] => string(1) "1"
 }
 ...
}

NULL
Anyone know why this might be? I've also tried to use $input->getEscaped() but without any luck.

Do the filtered values only get returned if the form is valid?

Also, in my view I have something like this:

Code:
<?php echo $this->formText('address2', $this->post->address2, array('id' => 'frm_address2', 'maxlength' => 48)); ?>
Which produces an empty element on the submit (because of the above issue), and an error prior to form submission because the post view variable doesn't exist yet. Obviously I could wrap the $this->post->address2 in a ternary statement, or something, to default the value, but I wondered what best practise on this was? Remembering that I want to display the filtered values again and not the raw ones that I could with $_POST.

Any help or advice would be truly appreciated!!

Thanks,

Andy

PS: I posted instead of Andy amnuts because he had some problems posting the code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-29-2007, 11:02 AM
Junior Member
 
Join Date: Oct 2007
Posts: 2
Default

The ever-helpful naneau has just pointed out on #zftalk that a validator has to exist for all form elements.

So by simply adding this to my $validators:

'*' => array()

I default all elements to use an empty validator. Problem solved!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-11-2008, 03:39 AM
Junior Member
 
Join Date: Mar 2008
Posts: 2
Default

I noticed in your code you are using the "StringEquals" validator. Would you mind sharing how you implemented this (eg where your class file is and how it's loaded/extended to work in your controller). I'd really appreciate it. Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 01:22 AM.