Cannot catch value from form_hidden on codeigniter? - php

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.

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.

CodeIgniter: Accessing array values using set_value()

New to CodeIgniter, apologies this is simple stuff. I have a controller with an array which holds an array of values and associative fields.
Controller
$tests = array( "ID" => "1", "Fcilty_typ" => "MO");
View
<input type="text" name="Fcilty_typ" value="<?php echo set_value('Fcilty_typ','Fcilty_typ')?>"/>
How can I manipulate the array in the controller so it's key=>values are accessible in the view, within the set_vaue(); function.
<input type="text" name="Fcilty_typ" value="<?php echo set_value('Fcilty_typ',$tests['Fcilty_typ']) ?>"/>
you could also do something like this with ci form helper.
$form_fcilty = array(
'name' => 'Fcilty_typ',
'id' => 'Fcilty_typ',
'value' => $tests['Fcilty_typ'],
'maxlength' => '100',
'size' => '50',
);
echo form_input($form_fcilty);
that way you don't have to mix too much html and php to create the form. note you can also do the form_input array with set_value() if you need to show the submitted form value again after a validation fail.

To add a class in codeigniter form

How to add a class in this codeigniter Pls any one help me to solve issue
I want to add this class in this line class="form-control"
<?php echo form_input('username', 'Username'); ?>
Please try this
echo form_input('username', 'Username',"class='class_name'");
Please read the docs for more info because the answer's there. (http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html).
You can pass form_input with an associative array with the attributes you want in it.
$attrs = array(
'name'=>'username',
'value'=>'Username',
'class'=>'form-control',
);
echo form_input($attrs);
try something like this
<?php echo form_input('username', 'Username','class="class_name"'); ?>
OR
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%',
'class' => 'class_name',
);
echo form_input($data);
// Would produce:
<input type="text" name="username" id="username" class="class_name" value="johndoe" maxlength="100" size="50" style="width:50%" />

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'
)

Codeigniter: Setting value of form for an edit page

I am trying to make an edit page where the form fields are populated from the database. I'm trying to get it working by just setting some random text, but I'm having trouble making it show.
I have the following in my controller:
public function edit($item_id) {
$this->data['title'] = "Edit Item";
$this->data['item_title'] = array(
'name' => 'item_title',
'id' => 'item_title',
'type' => 'text',
'value' => 'a title',
);
$this->data['url_slug'] = array(
'name' => 'url_slug',
'id' => 'url_slug',
'type' => 'text',
'value' => 'some-url-slug',
);
$this->template->build('admin/item/form', $this->data);
}
This is my view:
<?php echo form_open('admin/item/update_item', array('id' => 'item_form')); ?>
<input type="text" name="item_title" value="<?php echo set_value('item_title'); ?>" id="item_title" placeholder="Enter a title..."/>
<input type="text" name="url_slug" value="<?php echo set_value('url_slug'); ?>" id="url_slug" placeholder="url-slug-of-the-item"/>
When I go the /edit/id page, the placeholder is still showing and the value is blank. Why is not setting? It works fine when I use it for form validation.
I'm still new to codeigniter, so forgive my ignorance.
Can't you simply use the following?
<input type="text" name="item_title" value="<?= $item_title['value'] ?>" id="item_title" placeholder="Enter a title..."/>
Note: This would be valid for fuelphp; not 100% sure about codeigniter.
I ended up ditching the array and just doing $this->data['item_title'] = 'some title'; in the controller, and in the the view, set_value actually accepts a 2nd parameter, like this:
<?php echo set_value('item_title', $item_title); ?>
This does the trick, although I do get warnings if the variable doesn't exist, so I need to declare them all.
I ended up ditching the array and just doing $data['fullName'] = 'some title'; in the controller, and in the the view, set_value actually accepts a 2nd parameter, like this:
<?php
if(!isset($fullname)) {
$fullname = set_value('fullName');
}
echo form_open(base_url() . "broker/practice_send.html");
echo form_label("Name", "name");
$data = array(
'name' => 'fullName',
'id' => 'name',
'value' => set_value('fullName', $fullname)
);
echo form_input($data);
?>

Categories