Adding and Removing Fields Generated via CakePHP

3

I have a <select> that is fed by data from the database. I need to manipulate this select and multiply it if the user wants to enter more different data.

I also need to change the "name" attribute of each <select> so I can get the values correctly in $this->request->data because I implode the data.

I have a function that runs in pure php, but now I'm trying to redo it in CakePHP . I've never done anything with Javascript or Jquery in CakePHP , so I'm kind of lost in it. My main question is how to use Javascript with Cake. I have a script.js file that I use in the application with pure php. Now I want to use this with Cakephp, but I do not know how. I already put the file in the webroot / js folder, but I do not know how to do it from there.

    
asked by anonymous 12.02.2015 / 15:45

1 answer

0

Cakephp follows a pattern for creating form fields, if you are using Cake Form Helper

The default is always this:

$this->Form->input('model.id'); // <input type="text" name="data[model][id]" />

Based on this return, you could add the fields normally by jQuery, but setting the name of the element to Cakephp's default. See a simple example

$(function(){
   var $form =  $('#meu-formulario');

   var $baseSelect = $('<select></select>');


     $('.algum-evento').click(function(){
         var $newSelect = $baseSelect.clone();

          $newSelect.attr({name: 'data[model][novo_campo]'});

         $form.append($newSelect);
     });

});
    
12.02.2015 / 15:54