Codeigniter form helper setting input class on edit form - php

I am using ion auth to edit some user data.This is the code
<h1><?php echo lang('edit_user_heading');?></h1>
<p><?php echo lang('edit_user_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open(uri_string());?>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php
$data = array(
'name' => 'first_name',
'value' => set_value('first_name', $first_name),
'class' => 'form-control'
);
echo form_input($data);?>
</p>
This produces the input plus the value but without the input class
echo form_input($first_name);?>
My first code snippet is an attempt at adding input class and populating the input field. This gives me a conversion error Message: Array to string conversion
How should i add form class inside the edit form field?

The error is not coming from trying to add a class to your input field, but from set_value(), another CI helper function. The variable $first_name is sent as an array to the view, hence the error message.
As we don't know, which array value you need, you could try to access the first one with set_value('first_name', $first_name[0]), but this depends how your array (or array of objects) is structured, a echo '<pre>';print_r($first_name); die(); helps you to debug this.
CI set_value() is defined as described here:
set_value($field[, $default = ''[, $html_escape = TRUE]])
Parameters:
$field (string) – Field name
$default (string) – Default value
$html_escape (bool) – Whether to turn off HTML escaping of the value

Related

Codeigniter - How to set name property from database

I'm using CodeIgniter and I have 2 questions:
How do I call result from model to name property in any form inputs?
How do I get the dynamic name property posted in Controller?
This is what I tried in my HTML:
<input type="text" name="saa_<?php echo $row1['Tube_No']; ?>" value="<?= $row1['Pin_No']; ?>">
And this is what I tried in my controller:
$data = array(
'SA_No' => $this->input->post('saa_.$row1'),
);
How do i get my form input name property get posted to controller.
First you need to get the value of $row1['Tube_No']
$valueDefinedWhenRenderingTheView = $row1['Tube_No'];
then you need to access it like this:
$data = array(
'SA_No' => $this->input->post('saa_' . $valueDefinedWhenRenderingTheView)
);
because, when your defining the input name to be the concatenation of "saa_" and the value of $row1['Tube_No'].

how to get data from Input Form Cakephp 3

I am very new to this Cakephp framework, trying to follow and understand how to upload files using cakephp. I am using upload plugin provided by josediazgonzalez. In the view file, using formhelper i have:
<?= $this->Form->create($user, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add User') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('username');
echo $this->Form->control('password');
echo $this->Form->control('role');
echo $this->Form->input('photo', ['type' => 'file']);
echo $this->Form->control('dir');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
I want to print again the value that i submitted in my controller, what should i write? Something like:
$this->request->data;
$this->request->data is an array of data passed to your scirpt. You can use it to get field that you are interested in with, eg:
$data = $this->request->data;
$someVariable = $data["name"];
Or, you can access any field directly, by using data() accessor:
$someVariable = $this->request->data("name");
From this point, you can do anything you want with this variable.
One more thing - as $this->request->data and $this->request->data() are currently in deprecated state and will be removed in next version, I suggest to use $this->request->getData() instead. Usage is similar:
$this->request->getData(); //will return array of data passed
$this->request->getData("field_name"); //access to specific field

CakePHP Array Checkbox, Black hole error

I am new to CakePHP i tried to submit a form, which has an array input field.
Below is the code i used.
<?php echo $this->Form->checkbox('statusid.', array('value'=>$o['Order']['id'])); ?>
When i tried to submit the form i got a blackhole security error.
How can i fix it ?
I noticed that when i submitted the code below it works.
<?php echo $this->Form->checkbox('statusid', array('value'=>$o['Order']['id']));?>
Is there a way to make an array checkbox in CakePHP please guide me.
You can use the FormHelper and make a checkbox array named statusid this way.
<?php
echo $this->Form->create();
echo $this->Form->input('statusid.0', array('type' => 'checkbox', 'value' => $o['Order']['id']));
echo $this->Form->end();
?>
The issue with blackhole appears when 'hiddenField' is set to false. Although it's set to true by default, it's possible that OP has it defined somewhere globally for entire Form object.
<?php echo $this->Form->checkbox('statusid.', array(
'value' => $o['Order']['id'],
'hiddenField' => true)); ?>

Function that creates a input name from a string in CakePHP

I would like know if there is a function in CakePHP that transforms Mymodel.Mycolumn into data[Mymodel][Mycolumn].
I know how to do this with PHP only, but I would like to know if there is a built-in CakePHP function for it.
EDIT:
I don't need the input, only the name.
See the pretty complete CakePHP Cookbook. To use the current model:
echo $this->Form->input('Mycolumn');
Or to specify the model:
echo $this->Form->input('Mymodel.Mycolumn');
Creates:
<input type="text" id="MymodelMycolumn" name="data[Mymodel][Mycolumn]">
if you're sending, you can do this:
<?php
echo $this->Form->create('Mymodel');
echo $this->Form->input('Mymodel.Mycolumn', array('label' => 'add data for Mycolumn'));
echo $this->Form->submit('submit', array('class' => 'form-submit', 'title' => 'Enter'));
?>

CakePHP: posted file data not included in request->data

I'm trying to upload a file to 3rd party endpoint, but I can't post the file directly from my form because the API requires an api_key which I can't expose to the end user. Therefore, my plan was to point the form to a controller/action and post the data from there. However, when I debug($this->request->data) from inside the controller, the file data is missing.
The form on the view:
echo $this->Form->create('Media', array('type'=>"file", 'url'=>array('controller'=>'media', 'action'=>'upload') ) );
echo $this->Form->input('name', array("name"=>"name") );
echo $this->Form->input('file', array('type'=>'file', "name"=>"file") );
echo $this->Form->input('project_id', array('type'=>'hidden', "name"=>"project_id", "value"=>$project["Project"]['hashed_id']) );
//THIS CANNOT BE HERE: echo $this->Form->input('api_password', array('type'=>'hidden', "name"=>"api_password", "value"=>'xxxxxxx') );
echo $this->Form->end("Submit");
Here's what what I see when I debug() the request data from the controller:
array(
'name' => 'Some Name',
'project_id' => 'dylh360omu',
)
What's going on here?
File upload data can only be found in CakeRequest::$data in case the input element name is passed in an array named data (which is the default when not defining a specific name manually), ie:
<input type='file' name='data[file]'>
In your case however, the element will look like this:
<input type='file' name='file'>
which will cause the files data to be put in CakeRequest::$params[form].
https://github.com/cakephp/cakephp/blob/2.4.0/lib/Cake/Network/CakeRequest.php#L346
So either change the name in the form accordingly:
$this->Form->input('file', array('type' => 'file', 'name' => 'data[file]'));
Or access the file data via CakeRequest::$params[form]:
debug($this->request->params['form']);
Nunser was right (as always)! The problem resulted from customizing the name of the input. When I remove the 'name'=>'...' from the options array, the file shows up as expected. This seems like a bug, but if someone has a better explanation I'd love to hear it.

Categories