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');
Related
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
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]);
?>
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)); ?>
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'));
?>
I have table types and i want to build selectbox with all values from this table
In my controller i wrote this code
$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))
How build selectbox in view file ?
Well then its pretty simple all you need to do is first create List Data like
CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');
for ex
typeList = CHtml::ListData(allRegistrationTypes,'id','type');
now remember both id and type are fields in table
now all you have to do is if you are using form then
<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>
and if you need multiple you can pass multiple => multiple in the array as htmlOptions
You would use CHtml::dropDownList, or activeDropDownList if there is a "parent" model and you want to leverage its validation rules.
If you want to make the <select> element multiple-selection-capable, pass in 'multiple' => 'multiple' and 'size' => X as part of the $htmlOptions parameter.
Simplest Method to get "Select Box" in YII Framework:
<div class="row">
<?php
echo $form->labelEx($model,'county');
$data = CHtml::listData(County::model()->findAll(), 'id', 'county');
$htmlOptions = array('size' => '1', 'prompt'=>'-- select county --', );
echo $form->listBox($model,'county', $data, $htmlOptions);
echo $form->error($model,'county');
?>
</div>
Good Luck..