Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2009-12-15 22:52:26

nikz2
New member
Registered: 2009-12-15
Posts: 5

Simple trouble.

I need to create some selects that depends one on the other. I need one select(dropdown list) for cities, for example, and other for streets. So my application has to see on client side change of selected city and look (at the server-side) for data related to the city and send it back to other select. How would be better to do it?

Last edited by nikz2 (2009-12-15 22:53:19)

Offline

#2 2009-12-16 02:25:09

ryan.horn
Member
From: Buffalo, NY
Registered: 2009-09-08
Posts: 41
Website

Re: Simple trouble.

First, break down what your end user's will end up doing into steps. If you're using the MVC components of Zend, then you'll want to create controller actions for the intermediate steps, each of which accepts data and returns the appropriate information for the next step via AJAX.

With the case you provided, the initial step is selecting a city. Once the city is selected, your front end needs to make a request to a controller action that looks up and returns the addresses associated with the city to the front end. With that information, your front end can build the control needed for the next step.

If you give more information on what you're using, such as client side libraries (jQuery, etc.) I can provide a more concrete example.

Offline

#3 2009-12-17 15:53:17

nikz2
New member
Registered: 2009-12-15
Posts: 5

Re: Simple trouble.

Well.. Look. I already made it with jQuery. It works great, but i have another trouble. When i select city and than make async call by jquery and return values all looks great. Than i try to insert this data to my database doing a postback. When i do it (i do it same way like in zendcasts), my form returns valid, but options for select box injected by ajax disappear. So.. How can i register options on the form element of zend by jQuery? Or is there another way to do it?

Offline

#4 2009-12-17 16:20:01

nikz2
New member
Registered: 2009-12-15
Posts: 5

Re: Simple trouble.

Cool.. It all works. I inserted at my newone controller request to db for selected values and inserted them and it all works. If someone wants i can post my code. Thanks for help smile.

P.S. i don't know if my solution is the best (in terms of speed). But i don't know how make it better.

Last edited by nikz2 (2009-12-17 16:21:48)

Offline

#5 2009-12-17 16:56:09

ryan.horn
Member
From: Buffalo, NY
Registered: 2009-09-08
Posts: 41
Website

Re: Simple trouble.

If you have particular questions about how your existing code performs, then we'll need to see your existing code smile

Offline

#6 2009-12-17 18:24:36

nikz2
New member
Registered: 2009-12-15
Posts: 5

Re: Simple trouble.

[code=php]
public function newoneAction()
    {
        $this->PreAccess();
        $edos_civiles = $this->dbservice->getEdos_Civiles();
        $colonias = $this->dbservice->getColonias();
        $municipios = $this->dbservice->getMunicipiosByColony($this->_getParam('colonia_id'));
        $cp = $this->dbservice->getCodigos_PostalesByMunicipio($this->_getParam('municipio_id'));
        if ($cp!=null) $cp=$cp->toArray();
        if ($municipios!=null) $municipios=$municipios->toArray();
        //$codigos_postales->toArray(),$municipios->toArray()
        $this->view->form1 = new App_forms_newoneForm($edos_civiles->toArray(),$colonias->toArray(),$municipios,$cp);
        $this->doPostBackInsert();
        ....
}

    private function doPostBackInsert()
    {
        if ($this->getRequest()->isPost())
            {
                $paramArray = array();
                $paramArray = $this->getRequest()->getParams();
                unset($paramArray['controller']);
                unset($paramArray['module']);
                unset($paramArray['action']);
               
                //var_dump($paramArray);
                 if ($this->view->form1->isValid($paramArray))
                 {
                     unset($paramArray['Guardar']);
                     $this->view->message = $this->dbservice->insertData($paramArray,'Default_Model_AdolescenteTable');
                    
                 }
                 else
                 {
                     $this->view->message = 'Hubo un error. Datos no fueron registrados.';
                     $this->view->errorElements = $this->view->form1->getMessages();
                     //var_dump($this->view->errorElements);
                 }
            }
    }

[/code]
well.. I would be gratefull to hear any comment. What could i modify, do better, why, etc.

P.S. as you can see i'm new to zend and php smile and to web programming. That's why i supose i could do better a lot of things smile.

Last edited by nikz2 (2009-12-17 18:32:48)

Offline

#7 2009-12-18 22:16:00

Manuel
Member
Registered: 2009-12-09
Posts: 14

Re: Simple trouble.

$this->PreAccess();

For what is this ? Maybe you can use the zend-given methods like preDispatch() ? Over that, you should camelcase it. It's preAccess(), not PreAccess() :-)

I would try to get a more consistent code layout and change the indent of the = in a 'block-context' so that it looks more structured. But that's a personal thing.. You can take a look at the code conventions of zend framework-code, it's avaiable in the manual.

$paramArray = $form->getValues() would fit better.

Private methods are named _methodName().

That are the points I would modify. I hope it helps :-) Keep your head up! If you are new to zend and php it's really good code. But OOP seems to be familiar to you, so I bet you are coming from a desktop programming-background ?

Merry Christmas

Last edited by Manuel (2009-12-18 22:16:54)

Offline

#8 2009-12-18 23:13:04

ryan.horn
Member
From: Buffalo, NY
Registered: 2009-09-08
Posts: 41
Website

Re: Simple trouble.

I'll echo Manuel's comments on the naming conventions. Take a look at http://framework.zend.com/manual/en/cod … tions.html It's not the biggest deal in the world, but it will make your code more readable to others.

I also second using getValues() via the form object. Together, isValid() and getValues() serve as a "black box" for your controller to pass an array of input data through. Upon calling getValues(), your form will have applied any required manipulations (filters, etc.) and will have removed any indexes that the form does not contain an element for. This means that you would not need to call "unset()" on the request parameters which are irrelevant to your form.

Ex:

[code=php]
...
// Assume $requestParams contains data in "controller", "action", "module", "a", "b", "c", "d" and "e"
$requestParams = $this->getRequest()->getParams();

// assume your form has elements named "a", "b", and "c"
if ($form->isValid($requestParams)) {

    // $validatedFormData will only contain indexes for "a", "b" and "c". If there were any filtering objects attached to any elements, they will have manipulated the data as per their rules
    $validatedFormData = $form->getValues();
}
...
[/code]


I also personally like to keep all controller action logic encapsulated under one method in the controller, rather than spreading it out across multiple methods. It makes for "bigger" methods, but I find it's more organized and emphasizes the simplicity of MVC...which reminds me to look at ways of cutting down on controller responsibility when it starts to get too big. That's just me though.

Try to make all variable names self descriptive and meaningful. Example: instead of naming your form "form1", name it something like "newUserForm", "userLoginForm" or something that signifies what its responsibility is.

Last edited by ryan.horn (2009-12-18 23:45:27)

Offline

#9 2009-12-20 02:29:51

nikz2
New member
Registered: 2009-12-15
Posts: 5

Re: Simple trouble.

Well... Big thanks for answers. Just like i said, i just started to program in php and didn't put enough atention to the names, thanks for pointing at it. I use preAccess because i need it on many different forms but not on all of them. $validatedFormData = $form->getValues()  didn't knew. It's helpful, thanks. And yes. I did some works on c++ and MFC. I don't like very much web development, but now i live in little city in here i can't find good developer job. Also i was making some programs at asp .net, but there is a lot of trouble with license, so now i'm studing php smile Big thanks for help.

Offline

Board footer

Powered by FluxBB