Add and remove fields generated via Cakephp

Asked

Viewed 440 times

3

I have a <select> which is powered by database data. I need to manipulate this select and multiply it in case the user wants to enter more different data.

I also need to change the "name" attribute of each <select> to be able to pick up the values correctly in the $this->request->data because I implode the data.

I have a function that runs in pure php, but now I’m trying to redo on Cakephp. I’ve never done anything with Javascript or Jquery in the 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 don’t know how to do it. I’ve already put the file in the webroot/js folder, but I don’t know how to do it from there.

1 answer

0

Cakephp follows a standard for creating form fields, if you’re using Cake Form Helper

The pattern is always like 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 according to the Cakephp standard. 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);
     });

});
  • This question was the closest I could think of to my problem: http://answall.com/questions/43655/howto add and remove the generated fields-via-cakephp-dynamicamente/50471?noredirect=1#comment102498_50471

  • can you explain me better? I’m having a tremendous difficulty understanding how it works. What goes in the controller, if you need to change something in the model, as I call the function on the button. The only thing I did was put a script I already had inside the webroot/js folder. I’m finishing a great project and it took me several months, just missing this part even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.