What I want to do is to change from bootstrap vertical to horizontal form and this is what I've tried:
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'action' => ['index'],
'method' => 'get',
'fieldConfig' => [
'horizontalCssClasses' => [
'label' => 'col-sm-2',
'offset' => 'col-sm-offset-2',
'wrapper' => 'col-sm-4',
],
],
]); ?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'firstname') ?>
<?= $form->field($model, 'lastname') ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'bla') ?>
</div>
</div>
<?php ActiveForm::end() ?>
but it gave me an error Setting unknown property: yii\widgets\ActiveForm::layout
Please help!!!
For layout option to work ActiveForm must be instance of yii\bootstrap\ActiveForm instead of yii\widgets\ActiveForm.
Change your use statement.
In boostrap you can use
<form class="form-inline"> // for inline fields form
or
<form class="form-horizontal"> // for horizonatal fields form
eg:
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'action' => ['index'],
'method' => 'get',
'class' => 'form-horizontal',
]); ?>
Related
I want to update query when user field search field and click on button "search", but this is not send my get parameters to update action ..
here is view:
<?php
Pjax::begin([
'enablePushState' => true, // I would like the browser to change link
'timeout' => 100000 // Timeout needed
]);
echo LinkPager::widget([
'pagination' => $pagination,
'hideOnSinglePage' => true,
'prevPageLabel' => 'Предишна',
'nextPageLabel' => 'Следваща',
'firstPageLabel' => 'Начало',
'lastPageLabel' => 'Край'
]); ?>
<?php $form = ActiveForm::begin([
'action' => ['update'],
'method' => 'get',
]); ?>
<input type="text" id="fname" class="form-control search-option-input" placeholder="търси прецификация по име, номер" name="search-query-main" value=' '>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>
i use pjax for pager with result of all specification, because they are over 600 ... this code is in view _form and i want to send it with this search word to controller action Update, who can not work without $_GET['id']
If you want to pass ID in URL you can add it into action URL of your form:
<?php $form = ActiveForm::begin([
'action' => ['update', 'id' => $id],
'method' => 'get',
]); ?>
Compare validation is action strange - no matter what, emails are never the same, and error is shown all the time.
This is the code for validation:
public function rules()
{
return [
[['first_name', 'last_name', 'email', 'conf_email'], 'required'],
['title', 'match', 'pattern' => '/^[a-zA-Z]{0,100}$/',
'message' => 'Title must contain only letters.'],
[['first_name', 'last_name'], 'match', 'pattern' => '/^[a-zA-Z]{0,45}$/',
'message' => 'The {attribute} must contain only letters.'],
[['email', 'first_name', 'last_name'], 'trim'],
[['email', 'conf_email'], 'email'],
['email', 'string', 'max' => 255],
['email', 'UniqueValidator'],
['conf_email', 'compare', 'compareAttribute'=>'email', 'skipOnEmpty' => false,
'message' => 'Emails do not match.'],
];
}
And code for the form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
$form = ActiveForm::begin([
'action' => Url::toRoute([
'team/ajax-add-team-member',
'id' => $team->idteam,
'portalID' => $portal->idportal
]),
'enableAjaxValidation' => true,
'validationUrl' => '/team/team-validation',
'id' => 'team-invite-form',
'options' => [
'class' => 'clearfix'
]
]) ?>
<div class="col-md-12">
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-12">
<?php echo $form->field($model, 'title')->textInput() ?>
</div>
<div class="col-md-5 col-sm-5 col-xs-12">
<?php echo $form->field($model, 'first_name')->textInput() ?>
</div>
<div class="col-md-5 col-sm-5 col-xs-12">
<?php echo $form->field($model, 'last_name')->textInput() ?>
</div>
</div>
</div>
<div class="col-md-12">
<?php
echo $form->field($model, 'email')->textInput();
echo $form->field($model, 'conf_email')->textInput();
echo $form->field($model, 'is_medical_professional')->checkbox();
?>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<?php
echo Html::submitButton('Send', ['id' => 'add-team-member-form-btn', 'class' => 'btn btn-default']);
ActiveForm::end();
?>
What is going on here o.O?
Thiis line looks strange:
['email', 'UniqueValidator'],
Try using ['email', 'unique'], instead
If this will not help - provide more details. What exact error do you faced?
I have a simple form:
<?php $form = ActiveForm::begin([
'id' => 'answer-form',
'action' => Yii::$app->getUrlManager()->createUrl('test'),
'enableClientValidation' => false,
]); ?>
<?= $form->field($user_answer, 'user_text')->textInput(['value' => $text])->label('Text') ?>
<?php ActiveForm::end(); ?>
I want to show input with red color (div with class "has-error" by default)- like somebody added wrong data to the input. How can i do it?
Try this
<?= $form->field($user_answer, 'user_text', [ 'options' => [ 'class' => 'has-error'])->textInput(['value' => $text])->label('Text') ?>
My application have to have multiple language, so I decided to separate each language by using tab (Yii2 gui), but how can I render the form in side the 'content' key?
<?php
$language_tab=[];
$increment=0;
$content="I love you";
foreach($language as $obj){
$language_tab[$increment] = array('label' => $obj->name ,'content' => $content);
$increment++;
}
echo Tabs::widget([
'items' => $language_tab,
'options' => ['tag' => 'div'],
'itemOptions' => ['tag' => 'div'],
'headerOptions' => ['class' => 'my-class'],
'clientOptions' => ['collapsible' => false],
]);
?>
<div class="status-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'date_created')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I just wanna change from $content to the form below.
Please help!!!
You may create separate view for the form and render it:
...
'content' => $this->render('_language_form', ['language' => $obj, 'model' => $model]),
...
I'm creating a form view and I want to organize the form fields with tabs structure, using the official Tabs widget.
Is it possible init the Tabs widget with the id (or class) of the div elements that contains the active form fields?
One example of how you can manage it is doing like this:
First, divide your contact-form into one view-file for each tab.
Place the ActiveForm::begin() and ActiveForm::end() around the Tabs::widget()
Render the contact-form pages into content, with parameters $model and $form
Example code:
views/site/contact.php
<?php
/* #var $this yii\web\View */
$this->title = 'Contact';
use yii\bootstrap\Tabs;
use yii\bootstrap\ActiveForm;
?>
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= Tabs::widget([
'items' => [
[
'label' => 'One',
'content' => $this->render('contact_form1', ['model' => $model, 'form' => $form]),
'active' => true
],
[
'label' => 'Two',
'content' => $this->render('contact_form2', ['model' => $model, 'form' => $form]),
],
]]);
?>
<?php ActiveForm::end(); ?>
views/site/contact_form1.php
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
views/site/contact_form2.php
<?php
use yii\helpers\Html;
use yii\captcha\Captcha;
?>
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
Hope this helps!
Just add at the top of your contact.php global $form; and all works fine.
I have another solution:
When we call $form->field($model, 'name')->textInput(), it will return the model of class yii\widgets\ActiveField, so just continue calling a method of this class as $form->field($model, 'name')->textInput()->render(). It will return a string then you can use it for the tab's content.
I have an example code in my application for translating multi languages as the following code:
<?php
$items = [];
foreach ($translateModels as $translateModel) {
$tabContent = $form->field($translateModel, "[{$translateModel->code}]name")->textInput()->render();
$items[] = [
'label' => $translateModel->language->name,
'content' => $tabContent,
];
}
?>
<?= Tabs::widget([
'options' => [
'class' => 'nav-tabs',
'style' => 'margin-bottom: 15px',
],
'items' => $items,
]) ?>
Maybe it's help.