Cakephp - Checkbox names - php

Currently, i have succeeded in creating the checkbox.
The array which i have setup is as below:
$emailName = $this->User->find('list', array(
'fields' => array('User.username', 'User.email')
));
The output is as follows:
array
'admin' => string 'asd#asd.asd' (length=11)
'test' => string 'test#test.test' (length=14)
'Floo' => string 'XXXX#gmail.com' (length=16)
I'm trying to make the checkbox shows the username instead of the user email in view.ctp.
I have tried using the following code in view.ctp
<?php echo $this->Form->input('Address_list.['.$emailName['username'].']', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $emailName['email']
)); ?>
However, it seems that this doesn't work. Any ideas?

You are not formatting your form field for the checkbox list correctly. Try changing it to this:
echo $this->Form->input('Address_list', array(
'multiple' => 'checkbox',
'options' => $emailName,
));
However, this will return the value of Username based on the selection of Email the user chooses. It creates the form like this:
<label for="Address_list">Address List</label>
<input type="hidden" id="Address_list" value="" name="data[Address_list]"/>
<div class="checkbox"><input type="checkbox" id="AddressListAdmin" value="admin"
name="data[Address_list][]"/><label for="AddressListAdmin">asd#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListTest" value="test"
name="data[Address_list][]"/><label for="AddressListTest">test#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListFloo" value="Floo"
name="data[Address_list][]"/><label for="AddressListFloo">XXX#example.com</label></div>

Related

Validation is not working in codeigniter

Validation is not working. All the form fields are coming dynamically. It depends on the user how many fields he chooses.If he chooses 2 and it will display 2 fields in the view. If select 3 then it will display 3 fields and so on.I have more than 30 fields
I set 3 arrays(for testing purpose I set only 3. It will be total no of fields ) in my form validation page. If I remove the last array than validation is working because I am getting only 2 fields in the view. I am not able to use the more than 2 array in my form validation page.
Is it mandatory to require the number of fields in view is equal to a number of sets of rules array in form validation?
View
This is my dynamic view page
<?php
echo form_open('formbuilder_control/enxample_from_view');
foreach ($data as $key) {// I am getting output
$exp_fields_name=$key->fields_name;
$exp_fields_type=$key->fields_type;
$exp_form_elements=$key->form_elements;
$abc=explode(',',$exp_form_elements);
foreach ($abc as $value) {
if ($exp_fields_name == $value) {?>
<div class="form-group row label-capitals">
<label class="col-sm-5 col-form-label"><?php echo $exp_fields_name;?></label>
<div class="col-sm-7">
<input type="<?php echo $exp_fields_type;?>" name="<?php echo $exp_fields_name;?>" placeholder="<?php echo $value;?>" class="form-control" />
<?php echo form_error($exp_fields_name); ?>
</div>
</div>
<?php
}}}?>
<div class="form-buttons-w btn_strip">
<input type="submit" name="submit" value="Save" class="btn btn-primary margin-10">
</div>
<?php echo form_close(); ?>
Form_validation.php
$config = array(
'test'=>array(
array('field' =>'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array('field' => 'lastname',
'label' => 'lastname',
'rules' => 'required'
),
array('field' => 'middlename',
'label' => 'middlename',
'rules' => 'required'
)
),
);
Controller
public function test()
{
if($this->form_validation->run('test') == TRUE)
{
echo "working";
}
$this->load->view('test1');
}
Of course it will fail. Your validation rules define a 'middlename' field as required, and that field doesn't even exist in the form.
A missing field cannot satisfy a required rule.
It is possible to have flexible sets of rules with a minimum of code. Consider this form_validation.php example
<?php
$first_last = array(
array('field' => 'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array('field' => 'lastname',
'label' => 'lastname',
'rules' => 'required'
),
);
$middle_name = array(
array('field' => 'middlename',
'label' => 'middlename',
'rules' => 'required'
)
);
$config = array(
'fullname' => array_merge($first_last, $middle_name), //first, middle and last name fields
'shortname' => $first_last, //only first and last name fields
);
This provides two different sets of fields for use with form_validation->run().
For example: On a form using first, middle and last name fields
if($this->form_validation->run('fullname'))
{
...
Or, when the form contains only the first and last name fields
if($this->form_validation->run('shortname'))
{
...
As others have mentioned, you are requiring a field that does not exist. You either need to:
Add a field in your view with the name middlename
Remove the validation rule that requires a field middlename to exist
Not contributing to your issue, but in a usability aspect, you likely want to change the label in your lastname and middlename rules to make them more user friendly.
$config = array('test'=>array(
array('field' =>'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array('field' => 'lastname',
'label' => 'lastname',
'rules' => 'required'
),
array('field' => 'middlename',
'label' => 'middlename',
'rules' => 'required'
)
),
);
Also, the documentation has other helpful tips for different custom rules if you're not trying to require the middlename but want to sanitize or validate its format prior to insertion into a database.
https://www.codeigniter.com/userguide3/libraries/form_validation.html#form-validation-tutorial
Well, as you want to have one set of validation rules and you have two different forms to validate, you can do it using very nice option with Codeigniter's validation callback function.
I am going to post a simple example here:
Your view file:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php echo form_open('formbuilder_control/test');?>
<input type="text" name="firstname">
<input type="hidden" name="which_form" value="first_form">
<?php echo form_error('firstname'); ?>
<input type="text" name="lastname">
<?php echo form_error('lastname'); ?>
<input type="submit" name="submit" value="submit">
<?php echo form_close(); ?>
<?php echo form_open('formbuilder_control/test');?>
<input type="text" name="firstname">
<input type="hidden" name="which_form" value="second_form">
<?php echo form_error('firstname'); ?>
<input type="text" name="lastname">
<?php echo form_error('lastname'); ?>
<input type="text" name="middlename">
<?php echo form_error('middlename'); ?>
<input type="submit" name="submit" value="submit">
<?php echo form_close(); ?>
</body>
</html>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class YourController extends CI_Controller {
public function save()
{
//.... Your controller method called on submit
$this->load->library('form_validation');
// Build validation rules array
$validation_rules = array(
array(
'field' => 'A',
'label' => 'Field A',
'rules' => 'trim|xss_clean'
),
array(
'field' => 'B',
'label' => 'Field B',
'rules' => 'trim|xss_clean'
),
array(
'field' => 'C',
'label' => 'Field C',
'rules' => 'trim|xss_clean|callback_required_inputs'
)
);
$this->form_validation->set_rules($validation_rules);
$valid = $this->form_validation->run();
// Handle $valid success (true) or failure (false)
}
public function required_inputs()
{
if( $this->input->post('which_form') == "second_form" && !$this->input->post('middlename'))
{
$this->form_validation->set_message('middlename', 'Middle name is required!');
return FALSE;
}
return TRUE;
}
}
From above example you can see required_inputs function is just like a normal function, where you can write any php code.
So what I would advice is, have one hidden field in both of the forms, jus to check which form got submitted, and then set callback function for middlename validation rule and in that function, check which form is submitted by user, and based on it, return TRUE or FALSE.
I hope this will get you the whole idea what you can do.
You just need to add a hidden field with different value in each the forms and check the value in callback function to decide whether you should apply the third validation rule or not.

How to use check a checkbox in CodeIgniter?

I have checkbox which is created as below in CI:
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1'
));
I also have other fields such as Name which have a required field validation applied. So, when I leave Name as blank and tick the active checkbox, then it shows error for name. But it removes the check on active checkbox. I want to keep the active checkbox as checked.
I know about a set_checkbox method in CI, but not sure how to use this in the above. In case of form_input we simply use set_value and all is ok. But with set_checkbox it returns full string checked="checked". So when I use the set_checkbox and form_checkbox as combined as below, then it doesn't work.
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1',
'checked' => set_checkbox('active', '1')
));
Any idea how to fix this?
You can set this as
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1',
'checked' => ($this->input->post('active') && $this->input->post('active') == 1 )
));
IF you Do not use ($this->input->post('active') && ....) in your condition it will throw an error if you do not check this check-box and submit the form.
The checked attribute on form_checkbox accepts TRUE/FALSE value. A simple comparison to see if the value is set will work.
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1',
'checked' => ( $this->input->post('active') == 1 )
));
The set_checkbox method was not made to be used in this style. It's intended to be use inside existing HTML as per this example from the CI User Guide
<input type="checkbox" name="mycheck" value="1" <?php echo set_checkbox('mycheck', '1'); ?> />
<input type="checkbox" name="mycheck" value="2" <?php echo set_checkbox('mycheck', '2'); ?> />

Cakephp Radio input within table through foreach

I want radio button within table, without label and one radio button for one table row, while all grouped in one single group, so user can select single table row by selecting one radio button from the group ...
In pure html it looks like this:
<div class="radio">
<label>
<input type="radio" name="memberPaymentsRadio" value="mp<?php echo $key;?>" aria-label="...">
</label>
</div>
What would be the code in cakephp to get the same html code?
I tried like this:
foreach($payments as $key => $payment){
...
echo $form->input('memberPaymentsRadio',
array(
'div' => array('class' => 'radio'),
'label' => false,
'type' => 'radio',
'aria-label' => '...',
'options' => array('mp'.$key),
'before' => '<label>',
'after' => '</label>'
)
);
}
but I'm getting radio buttons, one per table row, and all within specific table column, but with labels 'mp0', 'mp1', which is not what I was looking for...
You can use like this
$this->Form->radio('memberPaymentsRadio', array('mp'.$key => ""),array('class' => 'radio', 'before' => '<label>','after' => '</label>','label' => false));

Cannot catch value from form_hidden on codeigniter?

I try to create Update Form with form helper CI. everything Work if on form_input. But, when id on form_hidden, it return NULL. this the script
on View
$hidden = array('name'=>'id_hidden','value'=>$datacompany[0]->id);
echo form_hidden($hidden); //I have edited
On Controller
function edit_company()
{
if(isset($_POST['EDIT']))
{
print_r($_POST);//return All value
$isi = array(
'id' =>$this->input->post('id_hidden'),//return null
'nip' =>$this->input->post('nip'),//return value
'nama' =>$this->input->post('nama'), //return value
'golongan' =>$this->input->post('golongan') //return value
);
echo $isi['id']; //the result id is null
}//end if
}//end Function
That Id I need for using on model. How Can I Fix That?
How to Catch ID from form_hidden?
I'm very appreciated Your Answer
thanks
While using my first comment will let you debug it in 3 seconds, Here's the answer :)
You're using the form_hidden in the wrong way.
An array in the form_hidden turns to this (from the docs)
$data = array(
'name' => 'John Doe',
'email' => 'john#example.com',
'url' => 'http://example.com'
);
echo form_hidden($data);
// Would produce:
<input type="hidden" name="name" value="John Doe" />
<input type="hidden" name="email" value="john#example.com" />
<input type="hidden" name="url" value="http://example.com" />
As you see, the 'keys' of the array turn to the names of the fields.
The value is the 'value' of the array. so in your example, you're making two hidden fields.
<input type="hidden" name="name" value="id_hidden">
<input type="hidden" name="value" value="$datacompany[0]->id">
You need to define like this a hidden field in CI:
$hidden = array('id_hidden',$datacompany[0]->id); // a name and value pair for a single instance.
echo form_hidden($hidden);
$hidden = array('id_hidden' => $datacompany[0]->id);
echo form_hidden($hidden);
I think this will fulfill your need.
Or if you want other attributes... Try with this..
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'type' => 'hidden',
'size' => '50',
'style' => 'width:50%',
);
echo form_input($data);
Depend on your need.

cakephp generates wrong label for radio input id

When i create a form on CakePHP with radio inputs, the label generated not match with the id of the radio input, the label "for" duplicates the name of the form. Here is my code:
echo $this->Form->create(
'test',
array(
'action' => 'index',
'type' => 'post',
'class' => 'fill-up',
'inputDefaults' => array('div' => 'input')));
$options = array('option1' => '1',
'option2' => '2');
$attributes = array('legend' = > false);
echo $this->Form->radio('Type', $options, $attributes);
echo $this->Form->end(
array(
'label' = > 'end',
'class' = > 'button',
'div' = > false));
and the generated HTML is something like:
<input type="hidden" name="data[test][options]" id="testOptions_" value="">
<input type="radio" name="data[test][options]" id="TestOptionsOption1" value="option1">
<label for="testTestOptionsOption1">1</label>
<input type="radio" name="data[test][options]" id="TestOptionsOption2" value="option2">
<label for="testTestOptionsOption2">2</label>
as you can see, cake duplicate the form name "test" on the label. How I can fix this? I try with the exact code of the documentations and still have the same issue
hope you can help me, thx very much
I auto-answer my question. That was a bug of cakephp, solved on the last version:
https://github.com/cakephp/cakephp/releases/tag/2.4.2
Try using
'label' => array(
'class' => 'thingy',
'text' => 'The User Alias'
)

Categories