Translating propery name of html link - yii2

How can I translate the property named Create NEW inside the link?
Html::a('Create NEW', ['create'], ['class' => 'btn btn-success']),
I tried this
$var = Yii::t('app', 'Create NEW');
Html::a($var, ['create'], ['class' => 'btn btn-success']);
but the link gets lost.

Yes, you can place the translation inside the link widget:
Html::a(Yii::t('app', 'Create NEW'), ['create'], ['class' => 'btn btn-success']),

Related

How to render another controller method by image link using yii2?

following code will render an image:
<?= Html::img('#web/img/accept_all_you_see-wallpaper-1680x1050.jpg', ['alt' => 'PicNotFound', 'class' => 'scale-with-grid wp-post-image', 'style' => 'width:960;height:700']); ?>
following code will render an image as a link
<?= Html::a('', Yii::getAlias('#web') . '/img/pic1.jpg', ['target' => '_blank', 'alt' => 'PicNotFound', 'class' => 'icon-search']) ?>
How to code, if I want render another Conroller method by clicking on an image?
Following code will implement my intention by bootstrap icon, not by image!
<div class="col-md-12">
<?= Html::a('<span class="glyphicon glyphicon-paperclip"></span>', ['/dateianhang/dateianhang/index'], ['title' => 'Anlagen anzeigen', 'data' => ['pjax' => '0']]); ?>
</div>
<?= Html::a(
Html::img(
'#web/img/accept_all_you_see-wallpaper-1680x1050.jpg',
['alt' => 'PicNotFound', 'class' => 'scale-with-grid wp-post-image', 'style' => 'width:960;height:700']
),
['/dateianhang/dateianhang/index'],
['title' => 'Anlagen anzeigen', 'data' => ['pjax' => '0']]
) ?>

Disable ActiveForm Clientside Validation on button other than submit Yii2

I have an upload excel to DB form.
It has 1 File input and 2 buttons 'Upload' and 'Delete All'.
Problem Scenario:
I don't want client-side activeform validation on 'Delete All' as File is not required here.
Code:
<?php $form = ActiveForm::begin([ 'enableClientValidation' => false,
'options' =>['action' => Url::toRoute('/site/halltickets')
,'method' => 'POST'
, 'enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'file')
->fileInput()->label("Upload Only Excel File [Allowed Formats: xlsx, xls]") ?>
<center>
<?= Html::submitButton('Upload »', ['class' => 'btn btn-warning']) ?>
<?= Html::a('Delete All »',
Url::toRoute(['site/delete-halltickets']),
['class' => 'btn btn-danger',
'data-confirm' => Yii::t('yii', 'Delete all Hall Ticket Allocations?'),
]);?>
</center>
<?php ActiveForm::end(); ?>
to understand better, below is the image.
It is bad idea to place "Delete All" button on the upload form. Insert this button outside the form. It will more user-friendly.
Additionally you can remove "required" validator from model. Or define it only for the specific scenarios.

Yii2 disabled textInput ActiveForm

how to disabled textInput in ActiveForm ?
i try like this but can't
<?= $form->field($model, 'title_series')->textInput(['class' => 'form-control class-content-title_series', 'placeholder' => 'Title', 'disabled' => 'disabled'])->label(false) ?>
'disabled' => 'disabled or 'disabled' => true
both of them can't too
I don't really know yii2/ActiveForm, but I believe you need to do it this way:
<?= $form->field($model, 'title_series')->textInput(['class' => 'form-control class-content-title_series', 'placeholder' => 'Title', 'disabled' => true])->label(false) ?>
To ensure that the field values are send on submit, use
echo $form->field($model, 'name')->textInput([
'readonly' => true,
]);
This solution won in 50 different tries.
I think issue was with your label thing
<?= $form->field($model, 'title_series')->textInput(['class' => 'form-control class-content-title_series', 'placeholder' => 'Title', 'disabled' => 'true'])->label(''); ?>
This is working, keep the disabled in same array than 'value'
<?= $form->field($model, 'type')->textInput(['value' => $type,'disabled' => true]) ?>

Yii2- Multiple Bootstrap Modal Items on Same Page

I am creating a page which would hold some buttons for creating different model items loaded via modal.
I'm using this code in a view file: create-all that belongs to the controller: ProductsController
Modal::begin([
'toggleButton' => [
'label' => '<i class="glyphicon glyphicon-plus"></i> Add Product',
'class' => 'btn btn-success'
],
'closeButton' => [
'label' => 'Close',
'class' => 'btn btn-danger btn-sm pull-right',
],
'size' => 'modal-lg',
]);
$class_products = 'app\models\Products';
$productModel = new $class_products();
echo $this->render('/products/create', ['model' => $productModel]);
Modal::end();
Modal::begin([
'toggleButton' => [
'label' => '<i class="glyphicon glyphicon-plus"></i> Add branch',
'class' => 'btn btn-success'
],
'closeButton' => [
'label' => 'Close',
'class' => 'btn btn-danger btn-sm pull-right',
],
'size' => 'modal-lg',
]);
$class_branch = 'app\models\Branch';
$branchModel = new $class_branch();
echo $this->render('/branch/create', ['model' => $branchModel]);
Modal::end();
branch/create code:
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model app\models\Branch */
$this->title = Yii::t('app', 'Create {modelClass}', [
'modelClass' => 'Branch',
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Branches'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="branch-create">
<?php
//echo Yii::$app->request->getReferrer();
$route = parse_url(Yii::$app->request->getReferrer(), PHP_URL_QUERY);
if ($route == urldecode('r=products%2Fcreate-all'))
print_r ($route);
?>
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
The buttons work just fine and a modal loads for both of the buttons
Problem
Add Product modal adds a new product successfully, but Add Branch doesn't. It just refreshes the page.
An attempt to solve your issue:
As far as I know, simply rendering view file inside the view, will not post data to the controller, unless you specify the action inside the form which you are rendering.
So inside the _form.php
<?php $form = ActiveForm::begin([
'action'=>'your_path_to_the_url',
]); ?>
You can also make an ajax submission from the form and handle the ajax data using Yii::$app->request->isAjax
This is just an idea. I have not seen your controller code nor your forms. Still a wild guess of the problem. See this will help you or not.

Render Form inside Yii2 Gui Tab

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]),
...