Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [Zend Framework] Convalidare campi

    Dovrei convalidare dei campi.
    Nel senso che se si inserisce qualcosa di sbagliato su di un campo deve poi comparire un messaggiodi errore del tipo email inserita non valita. Avrei pensato a Zend_Validator, anche se ho guardato la guida, non ho capito il funzionamento.

    Ho questo codice nel file authcontroller

    codice:
    <?php
    
    class AuthController extends Zend_Controller_Action
    {
    
        public function init()
        {
            /* Initialize action controller here */
        }
    
        public function indexAction()
        {
            // action body
                    $form = new Application_Form_Login();
                    $request = $this->getRequest();
                    if ($request->isPost())
                    {
                        if ($form->isValid($request->getPost()))
                        {
                            if (Zend_Validate_EmailAddress::INVALID_FORMAT('email'))
                            {
                                echo 'email non iserita correttamente';
                            }
                         /*   if ($this->_process($form->getValues('password')))
                            {
                                $this->_helper->redirector('index', 'index');
                            }*/
                         }
                     }
                 $this->view->form = $form;
        }
    
        protected function _process($values)
        {
            $adapter=$this->_getAuthAdapter();
                    $adapter->setIdentity($values['username']);
                    $adapter->setCredential($values['password']);
                    
                    $auth = Zend_Auth::getInstance();
                    $result = $auth->authenticate($adapter);
                    if ($result->isValid())
                    {
                        $user =$adapter->getResultRowObject();
                        $auth->getStorage()->write($user);
                        return  true;
                    }
                    return false;
        }
    
        protected function _getAuthAdapter()
        {
            $dbAdapter = Zend_Db_Table::getDefaultAdapter();
                    $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
                    
                    $authAdapter->setTableName('utenti')
                                ->setIdentityColumn('username')
                                ->setCredentialColumn('password');
                 
                    return $authAdapter;
        }
    
           
        public function registrationAction()
        {
            // action body
            $form = new Application_Form_Login();
            $form->submit->setLabel('Registration');
            $this->view->form = $form;
            
            if ($this->getRequest()->isPost()) 
            {
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)) 
                {
                    $nome = $form->getValue('nome');
                    $cognome = $form->getValue('cognome');
                    $username = $form->getValue('username');
                    $password = $form->getValue('password');
                    $email = $form->getValue('email');
                    $ruolo = $form->getValue('ruolo');
                    $login= new Application_Model_DbTable_Utenti();
                    $login->updateUtenti($id, $nome, $cognome, $username, $password, $email, $ruolo);
                    
                    $this->_helper->redirector('index');
                }
                else 
                {    $form->populate($formData);    }
            }
            else 
            {
                $id = $this->_getParam('id', 0);
                if ($id > 0) 
                {
                    $utenti = new Application_Model_DbTable_Utenti();
                    $form->populate($utenti->getUtenti($id));
                }
            }        
        }
    }
    e questo nel file login.phph nella form
    codice:
    $this->addElement('text','nome', array(
            'filters'=>array('StringTrim','StringToLower'),
            'messaqes'=>'Non valido'
            'validators'=>array(
            array('StringLength', false, array(0, 50)),),
            'required'=>true, 
            'label'=>'email',));

  2. #2
    In authcontroller aggiungo
    codice:
    if ($form->isValid($request->getPost()))
                        {
                            if (Zend_Validate_EmailAddress::INVALID_FORMAT('email'))
                            {
                                echo 'email non iserita correttamente';
                            }
    ma non succede nulla.

  3. #3
    codice:
    $this->addElement('text','cognome', array(
            'filters'=>array(
            'StringTrim','StringToLower'),
            'validators'=>array(array(
            'StringLength', false, array(5, 70)),),
            'Between', array('min'=>2, 'max'=>50),
            'Alpha'=>true,
            'required'=>true, 
            'label'=>'Cognome:',));
    per convalidare la lunghezza del campo sono riuscito tramite Between, per dire che deve essere solo alfabetico devo usare Alpha, ma sbaglio qualcosa. dove????

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.