CodeIgniter: Controller structure for forms with many inputs - forms

I'm developing a site using CodeIgniter and am trying to adhere to the "Fat Model / Skinny Controller" paradigm, but am running into some problems when it comes to pages containing forms with a number of inputs. The code below is what I'm using to define the inputs for the address fields
Part of my Controller (where I'm defining the form inputs and their attributes):
$this->data['address1'] = array(
'name' => 'address1',
'id' => 'address1',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '10',
'value' => $this->form_validation->set_value('address1'),
'placeholder' => 'Street Address'
);
$this->data['address2'] = array(
'name' => 'address2',
'id' => 'address2',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '11',
'value' => $this->form_validation->set_value('address2'),
'placeholder' => 'Address Line 2',
);
$this->data['city'] = array(
'name' => 'city',
'id' => 'city',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '12',
'value' => $this->form_validation->set_value('city'),
'placeholder' => 'City'
);
$this->data['state'] = array(
'name' => 'state',
'id' => 'state',
'class' => 'field addr',
'tabindex' => '13',
'value' => $this->form_validation->set_value('state'),
'label' => array('class' => 'desc')
);
$this->data['zip'] = array(
'name' => 'zip',
'id' => 'zip',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '14',
'maxlength' => '20',
'value' => $this->form_validation->set_value('zip'),
'placeholder' => 'Zip / Postal Code'
);
$this->data['country'] = array(
'name' => 'country',
'id' => 'country',
'class' => 'field addr',
'tabindex' => '15',
'value' => $this->form_validation->set_value('country')
);
Part of my View (minus all the HTML to position the form inputs):
<?php
echo form_open("address/add");
echo form_input($address1);
echo form_input($address2);
echo form_input($city);
$options = array();
$options[''] = 'State / Province / Region';
foreach($province_options AS $prov)
{
$options[$prov->id] = $prov->province;
}
echo form_dropdown('state',$options,'',$state);
echo form_input($zip);
$options = array();
$options[''] = 'Country';
foreach($country_options AS $cnt)
{
$options[$cnt->id] = $cnt->country;
}
echo form_dropdown('country',$options,'',$country);
echo form_submit('submit', 'Submit & Continue');
echo form_close();
?>
I feel like my Controller is overly verbose, but I can't think of what the alternative would be for how to organize the information necessary to represent my form if I'm planning on using the Form Helper to generate the form inputs in my view. Is this the right way to be doing things, or is there a better approach?

Just because Codeigniter provides all of these helpers does not mean you have to use them!
All you need is form_open() because this adds the CRSF token (if used).
Raw HTML is much cleaner and I suspect much faster than waiting for PHP to render markup.
Edit: I would like to add, the reason its cleaner is because you have control over the output, where as CI might adere to certain specifications.
I don't see a problem in your question.
This is just silly
$options = array();
$options[''] = 'State / Province / Region';

There is a little bit of logic that can be moved to the controller or even model layer:
$options = array();
$options[''] = 'Country';
foreach($country_options AS $cnt)
{
$options[$cnt->id] = $cnt->country;
}
echo form_dropdown('country',$options,'',$country);
Could probably look like:
echo form_dropdown('country', $countries, '', $country);
...if you move the options to the controller or view.
This is a problem I run into all the time trying to keep things DRY as possible, where to define the form data? I think sometimes we forget the power of the "V" in "MVC". You could define all the view logic in the view instead.
Things like id, tabindex and placeholder are only necessary and useful in the view. Things like form validation rules and data checking/prepping belong in the Controller/Model layer.
The form helper functions are useful, but sometimes raw HTML is better. For example:
// Controller
$this->data['address1'] = array(
'name' => 'address1',
'id' => 'address1',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '10',
'value' => $this->form_validation->set_value('address1'),
'placeholder' => 'Street Address'
);
// View
echo form_input($address1);
Or simply:
<input name="address1" id="address1" tabindex="10" type="text" placeholder="Street Address" value="<?php echo set_value('address1'); ?>" class="field text addr">
I wrote a bunch of applications last year where I defined all this stuff in the Model, and now I'm regretting it as I've been going back to do maintenance on them and all the view logic is obscured away in the Model or Controller. Editing a Controller or Model to change a class attribute is just silly.

Related

Form Data Not Being Sent From View To Controller

So I'm trying to send a few pieces of data from a form in cake's form builder but for some reason none of the data being submitted is showing up when I print out $this->data or $this->request->data
I've tried creating the form myself and using cake's Form Builder and I need to send this data via POST. The data shows up if I send it as a GET parameter.
view.ctp
echo $this->Form->create(null, array('action' => 'downloadbgc', 'type' => 'get'));
echo $this->Form->input('userId', ['type' => 'text' , 'id' => 'userId', 'name' => 'userId', 'value' => $user->id]);
echo $this->Form->input('product_id', ['type' => 'hidden' , 'id' => 'product_id', 'name' => 'product_id', 'value' => $user->product_id]);
echo $this->Form->submit('Download PDF', array('class' => 'btn btn-icon btn-primary', 'title' => 'Download'));
echo $this->Form->end();
controller.php
...
public function downloadbgc() {
$this->autoRender = false;
print_r("Data: ");
print_r($this->data); die();
}
}
...
When printing this out I get Data: Array() instead of Array('userId' => X, 'product_id' => Y) And I'm positive that these values aren't null since they print to the console.
Use the FormHelper's create() in a correct way:
echo $this->Form->create(false, ['url' => 'downloadbgc']);
echo $this->Form->input('userId', ['type' => 'text' , 'id' => 'userId', 'name' => 'userId', 'value' => 123]);
echo $this->Form->input('product_id', ['type' => 'hidden' , 'id' => 'product_id', 'name' => 'product_id', 'value' => 4456]);
echo $this->Form->submit('Download PDF', array('class' => 'btn btn-icon btn-primary', 'title' => 'Download'));
echo $this->Form->end();
Like the Docs point out the create() method can take the Model as first parameter or false, if you want to access the data without Model. Also use the url option as action is deprecated
In your Controller you can then use $this->request->data to access your data.

Create a SelectBox using Form Helper in CakePHP3

I am trying to make a combo box for an edit page.
echo $this->Form->select('status',
['empty' => 'Select Status'],
['class' => 'form-control', 'required']
);
Here I want to add 2 things :
$options = array('0' => 'Inactive',
'1' => 'Active',
);
and selected value. suppose that is $status;
I tried with different options but sometime it do not add classes and sometime it shows options in tag
It will be great if somebody give clue.
Thanks
<?= $this->Form->input('status', [
'type' => 'select',
'options' => ['0' => __('Inactive') , '1' => __('Active')],
'empty' => __('Select Status'),
'class' => 'form-control',
'required' => true, 'label' => __('Type')
])
?>

Cakephp 3.0 form dropdown how to show level

I am trying to add a form field for gender below is my code :
$options = ['m' => 'Male', 'f' => 'Female'];
echo $this->Form->select('gender', $options);
But in my view file I am unable to see lebel gender is there any other code which can help me suggest .
From->select doesn't give a proper label. Just do:
echo $this->Form->input('gender', array(
'options' => $options,
'type' => 'select',
'empty' => 'Select the gender',
'label' => 'Gender'
)
);
<?= $this->Form->select('gender', ['m' => 'Male', 'f' => 'Female'], ['class' => 'form-control', 'required' => true])?>

how to set readonly property in zend form add element

Hi I am new in zend framework.
I want to set ready only property on input box in zend Form.
example as we do in html
<input type ="text" readonly="readonly" />
this is my zend code:
$this->addElement('text', 'name', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:338px'),
'autocomplete' => 'off',
'decorators'=>Array(
'ViewHelper',
'Errors',
),
help mee
Try this
$this->getElement('text')->setAttrib('readonly', 'readonly');
Try something like this:
$this->addElement('text','text_field',array('attribs' => array('readonly' => 'true')));
In ZF2 you can create a form by extending Zend\Form and then add form element in the constructor. there you can set the attributes as follows.
use Zend\Form\Form;
class MyForm extends Form {
public function __construct() {
$this->add(array(
'name' => 'name',
'type' => 'Text',
'attributes' => array(
'id' => 'name',
'class' => 'form-control',
'readonly' => TRUE,
),
'options' => array(
'label' => 'Name : '
)
));
}
}

Display boolean as radio button in CakePHP form

Does anyone know how to display two radio buttons in a form in CakePHP and have them save to a boolean field in the DB?
I can get my boolean value to display as a checkbox (default behaviour) but I need to display two radio buttons, however when I do this the changes don't get saved to the DB.
I feel like it's something very simple. Here's my code:
<h1>Edit Content</h1>
<?php
$thisVal = $this->data['LessonContent']['is_exercise'] ? "1" : "0";
$options = array(
'0' => 'Learning Material',
'1' => 'Exercise'
);
echo $this->Form->create('LessonContent', array('action'=>'edit'));
echo $this->Form->input('name', array('label' => 'Name'));
echo $this->Form->input('description', array('label' => 'Description'));
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'name' => 'Type',
'options' => $options,
'value' => $thisVal
));
echo $this->Form->input('id', array('type'=>'hidden'));
echo $this->Form->input('lesson_id', array('type'=>'hidden'));
echo $this->Form->end('Save Content');
echo $this->Html->link('Cancel', array('controller'=>'Lessons', 'action'=>'view', $this->data['Lesson']['id']));
?>
Thanks
You're overriding the name of the input, therefore the value for is_excercise will be sent as Type.
Remove the name option;
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'options' => $options,
'value' => $thisVal
));
note after making this change, you probably don't even have to manually set the value of the radio-button.
debugging
In situations like this, always check/debug the posted form-data, either via FireBug or by debugging it in CakePHP, by putting this in your controller;
debug($this->request);
Even better, install the CakePHP DebugKit Plugin this plugin shows all the information (request data, queries, session variables etc.) without having to add debug-lines