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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2008, 04:57 PM
Junior Member
 
Join Date: May 2008
Posts: 4
Default Zend_Validate concerns

Hi,
Can anyone tell me if I can make use of Zend_Validate classes if I am not using Zend_Form to handle the forms? It would be nice if you could give some examples too...

Moreover, if I am not using Zend_Form in mvc how do I handle the forms (where smarty template is used)?

Please help..

~Nwim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-09-2008, 05:10 PM
Junior Member
 
Join Date: Mar 2007
Posts: 26
Default

Hi,
yes, you can use Zend_Validate without Zend_Form.

Zend_Validate
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-09-2008, 07:44 PM
Junior Member
 
Join Date: May 2008
Posts: 4
Default

Hi Martin,

Thanks for the reply. Can you give me an example of how to implement zend_validate with smarty?

Anyone else have any idea on how to do it? Please help..

~Nwim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-10-2008, 07:46 AM
Junior Member
 
Join Date: Mar 2007
Posts: 26
Default

Hi, I've never used Smarty and ZF together, so I have no idea how to do it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-10-2008, 08:24 PM
Junior Member
 
Join Date: Mar 2008
Posts: 12
Default

hi nwim,

form handling doesn't need to be acomplished by Zend_Form and it doesn't matter what templating system you're using (either Zend_View or Smarty, etc.).

all you need to do is eg.:

PHP Code:
class IndexController
{
   public function 
registerAction
   
{
      
$request $this->getRequest();
      if (
$request->isPost()) {
         
//fragment of code eclosed by this if clause will execute only if there is any post data send by a form
         
$errors = array();
         
//if u want to retrieve form field do as follows ('username' is example field name)
         //it is prefered way to $username = $_POST['username']
         
$username $request->getPost('username');
         
//now if you want to validate for 'username' length
         
$validator = new Zend_Validate_StringLength(8);
         if (
$validator->isValid($username)) {
            
// user name appears to be valid
         
} else {
            
// user name is invalid; save the reason
            
foreach ($validator->getMessages() as $messageId => $message) {
               
$errors['username'] = "Validation failure '$messageId': $message";
            }
         }
      
// make errors array available to your view
      
$this->view->errors $errors;
      }
   }

I hope this example gave you a little bit of understanding on form validating, however there is much more to know, like validator chains

Read more on zend framework's manual on official site

Last edited by kuba : 05-11-2008 at 11:57 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-12-2008, 06:10 PM
Junior Member
 
Join Date: May 2008
Posts: 4
Default

Hi Kuba,
Thanks a lot for the info and the sample code. It was great help. I used your sample code for validating username filed in my registration form. I just added the following line
$this->view->title = "Submit Success"; just before the
$request = $this->getRequest();....rest of the codes are same as the sample code.

But, the validation did not occur. Whether I enter or do not enter any data in the username field, it does not give any error and takes me to the Add Success page. I think I am missing something here...Could you please help me with this?

~Nwim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-13-2008, 02:26 PM
Junior Member
 
Join Date: Mar 2008
Posts: 12
Default

it works fine for me :P

index.phtml:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<?=$this->errors['username']; ?>
<form action="" method="post">
<input type="text" name="username" />
</form>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-13-2008, 03:56 PM
Junior Member
 
Join Date: May 2008
Posts: 4
Default

Thanks a lot Kuba,
It's working now. I was so stupid to forget method=post...ha!

Thanks for all the help..

~Nwim
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 10:02 AM.