Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should try the following code:</p> <pre><code>&lt;?php class FieldsController extends AppController{ public $uses = array('Template', 'Field'); function add(){ $this-&gt;set('title_for_layout', 'Please Enter Your Invoice Headings'); $this-&gt;set('stylesheet_used', 'style'); $this-&gt;set('image_used', 'eBOXLogo.jpg'); $this-&gt;Session-&gt;setFlash("Please create your required fields."); $templates = $this-&gt;Template-&gt;find('list', array('fields' =&gt; array('Template.id, Template.template_name' ); $this-&gt;set('templates', $templates); //$current_template = $this-&gt;request-&gt;data['Field']['template_id']; // right way to do it, but Template is undefined, and says undefined var //comment: You should check the request data with in if condition //$template = $this-&gt;request-&gt;data['Field']['template_id']; // makes sense with the find, no errors, but still doesnt print in form, says undefined var //$current_template = $this-&gt;request-&gt;data($template['Field']['template_id']); if($this-&gt;request-&gt;is('post')) { $this-&gt;Field-&gt;create(); if ($this-&gt;Field-&gt;save($this-&gt;request-&gt;data)) { if($this-&gt;request-&gt;data['submit'] == "type_1") { $this-&gt;Session-&gt;setFlash('The field has been saved'); $this-&gt;redirect( array('controller' =&gt; 'fields','action' =&gt; 'add')); } if($this-&gt;request-&gt;data['submit'] == "type_2") { $this-&gt;Session-&gt;setFlash('The template has been saved'); $this-&gt;redirect( array('controller' =&gt; 'templates','action' =&gt; 'index')); } } else { $this-&gt;Session-&gt;setFlash('The field could not be saved. Please, try again.'); } } } } </code></pre> <p>You view should looks like:</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('Field', array('action'=&gt;'add')); echo $this-&gt;Form-&gt;input('name', array('label'=&gt;'Name: ')); echo $this-&gt;Form-&gt;input('description', array('label'=&gt;'Description: ')); echo $this-&gt;Form-&gt;input('template_id',array('label'=&gt;'Template ID: ', 'options' =&gt; $templates)); //echo $this-&gt;Form-&gt;input('template_id',array('label'=&gt;'Template ID: ', 'type' =&gt; 'text', 'default'=&gt; $templates)); //echo $this-&gt;Form-&gt;input('templates_id', array('label'=&gt;'Template ID: ', 'type' =&gt; 'text', 'default' =&gt; $current_template['templates_id']));//this would be the conventional fk fieldname echo $this-&gt;Form-&gt;button('Continue adding fields', array('name' =&gt; 'submit', 'value' =&gt; 'type_1')); echo $this-&gt;Form-&gt;button('Finish adding fields', array('name' =&gt; 'submit', 'value' =&gt; 'type_2')); echo $this-&gt;Form-&gt;end(); ?&gt; </code></pre> <p>Kindly check and verify if it is working for you or not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload