how to get data from Input Form Cakephp 3 - php

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

Related

Codeigniter form helper setting input class on edit form

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

Displaying yii2 checkboxlist vertically

I have an active form which currently displays checkboxlists horizontally but I would like it to display them vertically. I have set the form layout to vertical but it still displays them horizontally:
This is what I have tried:
//generates an array of permissions
$options = Permission::value_list(
Permission::findWhere()->select('name')->andWhere(['not', ['name' => $name]])->all(),
['name']
);
This is the form
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'item_children')
->checkboxList($options)->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>
What do I need to add: currently they are displayed in this way.
I would like the displayed vertically.
You could use the separator option mentioned here: http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#activeCheckboxList()-detail
Your call would then look like this for example:
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'item_children')
->checkboxList($options, ['separator'=>'<br/>'])
->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>
Or whatever you want to use as the separator in your specific case.
Hope that helps...
The label to use Note that this will NOT be encoded.
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'country')
->inline(true)
->checkboxList($options)->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>

Take data from another field in yii2 form

I'm making a form with yii2, now I have two field:
<?php echo $form->field($model, 'Protocol')->textInput(['maxlength' => true])->dropDownList(
array("rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"), // Flat array ('id'=>'label')
['prompt'=>'Select'] // options
); ?>
<?php echo $form->field($model, 'url')->textInput(['maxlength' => true]); ?>
They look like this:
How can I take the selection from Protocol's drop down list and automatically add it to the below URL field? Like this: I type the http:// in the field manually, is there anyway I can make it automatic?
Add onchange event in your 'protocol' dropdownList. show below code
<?= $form->field($model, 'Protocol')->dropdownList(["rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"], [
'onchange'=>'$( "#'.Html::getInputId($model, 'url').'").val($(this).val());'
]) ?>
<?= $form->field($model, 'url')->textInput(['maxlength' => true]);
?>

Cakephp none add action forms

I have an index action in one of my controllers.
In this index action i wish to create a "search" form that calls another action within my controller with a post request.
All the documentation i could find on form creation in cakephp is about creating new elements (i.e insert data into a database ) and not actually sending data to another action / function.
here is an example:
<?php echo $this->Form->create('Product'); ?>
<fieldset>
<legend><?php echo __('Søg Produkt'); ?></legend>
<?php
echo $this->Form->input('Search field');
?>
</fieldset>
<?php echo $this->Form->end(__('Søg')); ?>
How would i send the value of my search field to another action? (so it redirects to that action and sends data to it)
Did you try to read the documentation?
$this->Form->create('Product', array('url' => array('action' => 'search');

Basic Hidden field in yii

I'm trying to place data in hidden text in yii, but I don't know how.
I need a similar code to a regular php syntax:
<input type="hidden" name="field_name" value="a"/>
It's supposed to be a field with static value of a. I just need it to go with my $_POST variables for error checking.
Is it possible to avoid modifying the models and controllers just to put the field in?I cant use gii cause I only have snippets of code with me.Sorry as well as I have little understanding of yii so I have no clue if what I'm saying about the last 2 sentences is correct.
in views
hidden field with model and form:
<?php echo $form->hiddenField($model, 'name'); ?>
or without model
<?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?>
Yii hidden input :
<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>
In Yii2 this has changed too:
<?= Html::activeHiddenInput($model, 'name') ;?>
References:
http://www.yiiframework.com/forum/index.php/topic/49225-activeform-how-do-you-call-label-input-and-errors-individually/
https://github.com/yiisoft/yii2/issues/735
if data from database and value or size field:
echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>
Yii 1
<?php echo $form->hiddenField($model, 'name'); ?>
Yii2
<?= Html::activeHiddenInput($model, 'attribute', ['value' => 'Some Value']) ?>
Also, worth noting for Yii2, the array parameter works different to a normal form field.
E.G. A normal input would look more like this.
<?= $form->field($model, 'attribute', ['inputOptions' => ['placeholder' => 'Some Placeholder', 'value' => 'Some Input Value']]) ?>
Hope this helps.
for yii2 you can try this
<?= $form->field($model, 'user_type',['inputOptions' => ['value' => '2']])->hiddenInput()->label(false) ?>
It worked for me
Alternatively,
echo CHtml::activeHiddenField($model,"[$i]id", array("value" => $model->id));
This would set hidden field value as the id from model. The [$i] is useful for multiple record update.
Here are two ways to do that...
without model
echo CHtml::hiddenField('name' , 'value', array('id' => 'name'));
with model
echo $form->hiddenField($model, 'name');

Categories