cakephp form not submitted correctly after JS added a new field

651 Views Asked by At

The View

<?php echo $this->Form->create('Practice');
echo $this->Form->input('title',array('value'=>'test value'));
echo $this->Js->submit('Ajax Submit', array(
'update' => '#left',
 'url'=>'/practices/content',
 'async' => true,
 'method' => 'post',
 'dataExpression'=>true,
 'data'=> $this->Js->serializeForm(array(
     'isForm' => true,
     'inline' => true 

  ))
    ));
?> <?php echo $this->Form->end(); ?>

the form works fine before the Ajax submit. the ajax submit actually return a new form field of the same model. below is the view of which the form is being sent firstly for ajax

echo $this->Form->input('Practice.options',array('value'=>'regex value'));

the new field value is not being submit with the form

2

There are 2 best solutions below

1
On

Make sure that your put at the end of your form

<?php echo $this->Form->end(); ?>

and you unlock the current action for the SecurityComponent

0
On

I was doing two mistakes first the new ajax returned fields were not inside form tag second I needed to bypass the cakephp security check

function beforeFilter() {
        parent::beforeFilter();
        $this->Security->blackHoleCallback = 'blackhole';
        $this->Security->csrfCheck = false;
        $this->Security->validatePost = false;
        $this->set('contentLayout', 'admin_dashboard');
    }