Html textbox without model in Yii2 - yii2

I just moved to Yii2. How to make text box like this:
<?= $form->field($model, 'xxx') ?>
but without using any model?

Use yii\helpers\Html and
Try something like,
<?= Html::textInput('xxx', value, options[]); ?>
textInput()

I would add a label and the form-control Bootstrap class:
<?= Html::label('Person', 'xxx') ?>
<?= Html::textInput('xxx', value, ['class' => 'form-control']) ?>

Related

How to change the address of processing a form in Yii2?

I have Yii2 and a simple form. (It's view):
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin() ?>
<?= $form->field($form_model, 'name') ?>
<?= $form->field($form_model, 'email') ?>
<?= Html::submitButton('Send', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end() ?>
But the form will send the request to the same address, which is itself: /myform
<form id="w0" action="/myform" method="post">
How I can change it?
By setting param action in ActiveForm configuration:
<?php $form = ActiveForm::begin(['action' => 'here_array_or_string']) ?>
More in Yii2 Active Form
Add it in the widget configuration like:
ActiveForm::begin(['action' => ['controller/action']])

Creating custom fields in the yii2 for model form

I'm using active form for the generating the form.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'designation') ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'facbook_url')?>
<?= $form->field($model, 'twitter_url')?>
<?= $form->field($model, 'pinterest_url')?>
<?php ActiveForm::end(); ?>
I want to add a custom fields in this which is not there in model.
You can Yii HTML helper for the this.
use yii\helpers\Html;
<?= Html::textInput('first_name','',array('class'=>'form-control')) ?>
Refer the link for all the available methods for the class
http://www.yiiframework.com/doc-2.0/yii-helpers-html.html
Just add these fields as public attributes in the class of $model and add validation rules for them.
You can find more details about this in the Guide: Creating Forms.

Yii2 form does not work — Class 'yii\bootstrap\ActiveForm' not found

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$form = ActiveForm::begin(['id' => 'login-form']);
ActiveForm::end();
?>
If i run this code the following error will occur.
Fatal error: Class 'yii\bootstrap\ActiveForm' not found in
E:\wamp\www\yii2-paypal-master\backend\views\site\login.php on line 6
Kindly help me to fix this.
Thanks
Replace
use yii\bootstrap\ActiveForm;
with
use yii\widgets\ActiveForm;
Form:
<?php $form = ActiveForm::begin([
'options'=>['enctype'=>'multipart/form-data'],
]);
?>
<?= $form->errorSummary($model) ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
First, check the file assets/AppAsset.php if your Class App Asset is like mine, using Boostrap 3 or 4 or o:
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap4\BootstrapAsset',
];
}
You need to change it to use yii\bootstrap\ActiveForm; change this to instead use yii\bootstrap4\ActiveForm; it worked for me.

Yii2 - How To Add a CSS class to an ActiveForm Field

This is the activeform field. I would like to know how to add a css class to it.
<?= $form->field($model, 'url')->label(false); ?>
You can add it with
<?= $form->field($model, 'url')->textInput(['maxlength' => 255, 'class' => 'your class'])->label(false); ?>
As a rule you can pass html elements when telling activeField what type it should be. The default is textInput so that is why your code works, but if you want to change the input then you have to explicitly tell it the type of input.
http://www.yiiframework.com/doc-2.0/guide-input-forms.html
<?= $form->field($addWithdrawRequest, 'amount', ['options' => ['tag' => false]])->textInput(['class' => 'form-control col-lg-6 amount','required'=>true,'style' => 'background-color: #fff !important;','placeholder'=>"Enter Amount To Withdraw"])->label(false)?>

Yii2: How to add textarea in yii2

What is the mapping of textarea in yii2 ?
How to write this in yii2 format?
<textarea name="downloadSourceCode" id="downloadSourceCode"></textarea>
What is an alternative or way to define textarea in yii2?
You can use Active Forms to create fields like textarea for example
<?php $form = ActiveForm::begin(['id' => 'downloadSourceCode']); ?>
<?= $form->field($model, 'description')->textarea(['rows' => '6']) ?>
<?= Html::submitButton('Submit') ?>
<?php ActiveForm::end(); ?>
In the previouse example you are creating a form with a textarea inside, you can give it a name and pass the model from the controller to show the existing content of the model if you are editing it, if you are creating a new model, you will need to create a new object and then pass it to the view.
Text area code in yii2 could be created in many ways It depends on what you need exactly
Situation 1 You have a Model
say the text area connected to that model in an Active form
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'election_description')->textArea() ?>
<?php ActiveForm::end(); ?>
the Code generated will be
<div class="form-group field-election-election_description">
<label class="control-label" for="election-election_description">Description</label>
<textarea id="election-election_description" class="form-control" name="Election[election_description]"></textarea>
<div class="help-block"></div>
</div>
As you can see label and error block is generated along with the textarea code by default since this could be useful in practical scenarios .So What i have written above will be interpreted as
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'election_description',['template'=> "{label}\n{input}\n{hint}\n{error}"])->textArea() ?>
<?php ActiveForm::end(); ?>
Change or remove the label by just doing
<?= $form->field($model, 'election_description')->textArea()->label(false) ?>
<?= $form->field($model, 'election_description')->textArea()->label("Some Label") ?>
Or more advanced customization could be done by modifying the template ,
"{label}\n{input}\n{hint}\n{error}"
is the default template .However template is customizable, If you just want the text area only override the code generation template for text area as
"{input}"
thus
<?= $form->field($model, 'election_description',['template'=> "{input}"])->textArea() ?>
the Code generated will be
<div class="form-group field-election-election_description">
<textarea id="election-election_description" class="form-control" name="Election[election_description]"></textarea>
</div>
The Div wrapping the text filed could be removed by modifying the template of the active form or by using another function activeTextInput
<?= Html::activeTextInput($model, 'election_description'); ?>
the Code generated will be
<textarea id="election-election_description" name="Election[election_description]"></textarea>
Situation 2 You don't have a Model
If we don't have a model and just want to create the exact code as asked best way will be to use Html::textarea
follow this format
textarea ( $name, $value = '', $options = [] )
Refer this example
<?php use yii\helpers\Html;?>
<?= Html::textArea('downloadSourceCode',"",['id'=>'downloadSourceCode']); ?>
Which will generate a code
<textarea id="downloadSourceCode" name="downloadSourceCode"></textarea>
Hope This helps
Refer these links for more info
http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#textarea()-detail
http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#textarea()-detail
http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#activeTextarea()-detail
You can do this like:
<?php $form = ActiveForm::begin(['id' => 'my-form']); ?>
<?= $form->field($model, 'field_name')->textArea(['rows' => '6']) ?>
<?= Html::submitButton('Submit') ?>
<?php ActiveForm::end(); ?>
Use Textarea in template
<?= $form->field($model, 'columnName',
['template' => '
{label}
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-newspaper-o"></i>
</span>
{input}
</div>
{error}{hint}'])->textarea(['rows' => 6])->hint('Max 255 characters.'); ?>
You can use the below code
<?= $form->field($model, 'desc')->textarea(); ?>
OR
<?= $form->field($model, 'desc')->textarea()->label('Description'); ?>
OR
<?= $form->field($model, 'desc')->textarea(array('rows'=>2,'cols'=>5)); ?>
For more details about form field.
If you map with model then following code should be OK for you:
<?= $form->field($model, 'downloadSourceCode')->textarea() ?>
<?= $form->field($model, 'field_name')->textArea(['maxlength' => 300, 'rows' => 6, 'cols' => 50,'placeholder'=>'Enter Message Here.....']) ?>
You can run following command on console
php composer.phar require --prefer-dist yiidoc/yii2-redactor "*"
or
"yiidoc/yii2-redactor": "*"
for instaling Redactor see https://github.com/yiidoc/yii2-redactor
Than you can check following line in codes
<?= $form->field($model, 'address')->widget(\yii\redactor\widgets\Redactor::className()) ?>
Like this:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'die_geschichte')->textArea(array('rows'=>25, 'cols'=>50, 'readonly' => true, 'name'=>'xyz; )) ?>
<div class="form-group">
<?= Html::submitButton('Unangemessenen Inhalt melden', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
It is so simple. Just write it like this in your ActiveForm::begin.
<?= $form->field($model, 'downloadSourceCode')->textarea(['maxlength' => 1000]) ?>
and you can get your text area.
If you want textarea without specifying any model, use:
<?= \yii\helpers\Html::textarea('name_attribute_value') ?>
Output will be:
<textarea id="id_attribute_value" name="name_attribute_value"></textarea>
It's simple. Just like this
<?= $form->field($model, 'notes')->textarea(); ?>
<?= $form->field($model, 'notes')->textarea()->label('Notes'); ?>
<?= $form->field($model, 'notes')->textarea(['rows'=>2,'cols'=>5]); ?>
This can be help you
Text area
<?= $form->field($model, 'desc')->textarea(['rows'=>2,'cols'=>5,'id'=>'textarea_id','class'=>'form-control textarea_class']); ?>
<?= $form->field($model, 'desc')->textarea()->label('Description'); ?>
Text
<?= $form->field($model,'name'); ?>
<?= $form->field($model, 'name')->textInput()->hint('Please enter your name')->label('Name') ?>
Password
<?= $form->field($model, 'password')->input('password') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'password')->passwordInput()->hint('Password should be within A-Za-z0-9')->label('Password Hint') ?>
File
<?= $form->field($model, 'uploadFile')->fileInput() ?>
<?php echo $form->field($model, 'uploadFile[]')->fileInput(['multiple'=>'multiple']); ?>
Radio
<?= $form->field($model, 'gender') ->radio(array('label'=>''))
->label('Gender'); ?>
<?= $form->field($model, 'gender')->radio(array(
'label'=>'',
'labelOptions'=>array('style'=>'padding:5px;')))
->label('Gender'); ?>
<?= $form->field($model, 'population')->radioList(array('1'=>'One',2=>'Two')); ?>
List
<?= $form->field($model, 'population')-> listBox(
array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
array('prompt'=>'Select','size'=>3)
); ?>
<?= $form->field($model, 'population')-> listBox(
array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
array('disabled' => true,'style'=>'background:gray;color:#fff;'))
->label('Gender'); ?>
Its like this.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'downloadSourceCode')->textArea(['maxlength' => true]) ?>
<?php ActiveForm::end(); ?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'descrip', ['options' => ['class' => 'custom-class']])->textArea(['maxlength' => true, 'placeholder'=>'Invoice Address', "class"=>'form-control']) ?>
<?php $form = ActiveForm::end(); ?>
**You can Also add Tinymce widget instead of text area.**
<?= $form->field($model, 'club_description')->widget(TinyMce::className(), [
'options' => ['rows' => 4],
//'language' => 'EN',
'clientOptions' => [
'plugins' => [
"advlist autolink lists link charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
]
]);?>
**Namespace: use dosamigos\tinymce\TinyMce;
Reference: https://github.com/2amigos/yii2-tinymce-widget**
Step 1 : In your view file don't forgot to add ActiveForm Class
use yii\bootstrap\ActiveForm;
Step 2 : Now add the text area as below in the view
field($model, 'body')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => '2']) ?>
This code is added in views file:
<div class="col-md-12 mb-10">
<label class="control-label" for="cancellesson-cancel_note">Cancel Note</label>
<textarea id="cancellesson_cancel_note" class="form-control" name="cancellesson[cancel_note]" placeholder="Enter Cancel Note" aria-required="true"></textarea>
</div>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'description')->textarea(['rows' => '5']) ?>
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
This line is for textarea
<?= $form->field($model, 'description')->textarea(['rows' => '5']) ?>
With the help of Yii2 active form we can create textarea field in form.
$form->field($model, 'fieldName')->textarea(array('rows'=>2,'cols'=>5));
I`m not pretty sure, but activeTextarea() with option attr is nice
there is alternative extension named kartik-v widget:
use kartik\widgets\ActiveForm;
echo ActiveForm::begin();
echo $form->field($model, 'username');
just install with composer
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php $form = ActiveForm::end(); ?>
With Model:
<?= $form->field($model, 'user')->textArea(['rows' => 6]) ?>
Without Model:
<?= Html::textarea('sourceCode',null,['rows'=>6]) ?>
It has 2 possibilities. FOr now do this:
field($model, 'user')->textArea(['rows' => 6]) ?>