You have on one page ActiveForm as master record, and grid as child records, how can i make relation between them. At create time there isn't id of master record! Any advice is wellcome! TIA. Asim
There is no need to use modal in order to add the child object. You can do it in one form. If you use a modal, you'll need to create the parent object first, and then allow this modal to be shown. In this way, your worries will be invalid.
If you want to add a parent and a child in one form, you can pass both objects to the view and then put their fields in the form. Then on form submit, you validate both objects, and then if everything is fine, save the parent, and then the child, assigning the parent id to the child. You can do this in a transaction so that if the child fails, the parent won't be recorded as well or vise versa.
Here is some code:
class YourController extends Controller
{
public function actionSomething()
{
$parent = new Parent();
$child = new Child();
$request = Yii::$app->request;
if ($parent->load($request->post('Parent')) && $child->load($request->post('Child'))) {
// Do validation and if everything is fine, then save the fields
Yii::$app->db->transaction(function() {
$parent->save(false);
$child->parent_id = $parent->id;
$child->save(false);
});
}
return $this->render('view', compact('parent', 'child'));
}
}
The view:
$form = ActiveForm::begin() ?>
<?= $form->field($parent, 'fieldA') ?>
<?= $form->field($parent, 'fieldB') ?>
<?= $form->field($child, 'fieldA') ?>
<?= $form->field($child, 'fieldB') ?>
...other input fields...
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
Did you get the idea? If you have questions or problems, please ask. Here is another example. This is with an update, but the idea is the same:
Getting Data for Multiple Models
Related
I'm using Yii2, activeForm and the Yii2 pjax widget to handle an search form with result list.
But for layout reason, I have to split this package to two parts: the search form, which should be located in the header of the page and the result listing, which should be placed in the middle of the page.
This means it's not possible to do something like that (pseudocode):
Pjax::begin
$form=>activeForm::begin..
ActiveForm::end();
ListView::widget...
Pjax::end()
What I need is placing the activeForm in the layout header, placing the ListView wiget in the center of the page and telling pjax: read the input from the form, load data from Server and replace the clist view.
Is that possible using the build-in functionality of the widgets?
I didn't get it working, maybe I misunderstood the basic concept?
Using gridview (or widget which use searchModel) you could use a separated model for modelSearch in header and show the resul in the gridview palced where you prefer
in this way you need only a pjax block
<?= $this->render('_search', ['model' => $searchModel]); ?>
<?php Pjax::begin(); ?>
<?= GridView::widget([
...
]); ?>
<?php Pjax::end(); ?>
for other soluzione not based on gridview and when you need multiple seprataed pjax block
You could try using named pjax block using a value for id
<?php \yii\widgets\Pjax::begin(['id'=>'pjax-block1']); ?>
....
....
<?php \yii\widgets\Pjax::end(); ?>
<?php \yii\widgets\Pjax::begin(['id'=>'pjax-block2']); ?>
....
....
<?php \yii\widgets\Pjax::end(); ?>
and if you need you could manage specific refresh using jquery
$.pjax.reload({container: '#pjax-block1', async: false});
$.pjax.reload({container: '#pjax-block2', async: false});
My controller
public function actionIndex()
{
return $this->render('index');
}
In the view I call a series of widgets. All the logic inside the widgets, they do not have any input parameters. In each block for every widget I want to add a button to update the widget inside this block.
<?= Html::a("Обновить", ['???'], ['class' => 'btn btn-sm btn-default', 'data-pjax' => '#formsection']) ?>
I put widget in pjax
<?php Pjax::begin(['id' => 'formsection', 'linkSelector' => '#chart a']); ?>
<div class="chart" id="chart" style="height: 200px; position: relative;">
<?= Chart::widget(); ?>
</div>
<?php Pjax::end(); ?>
How to assign a specific block to a button?
As i understand the chart is drawn via some values from the database which are frequently updated and you want those changes to be reflected in the chart when you click the reload button.
If the above is correct then you should use $.pjax.reload simply to reload the section bind the click event to the button
$this->registerJs('
jQuery(document).on("click", "#my-button", function(event){
$.pjax.reload({container:"#formsection",timeout:1000})
}
);
');
Hope that helps.
I'm trying to add a modal to my navbar.
This is my \views\project_form.php:
<?php Modal::begin(['id' => 'modal', ?>
<?= $form->field($model, 'Wert')->textInput(['maxlength' => true]) ?>
<?php Modal::end(); ?>
and this is my Controller:
function actionShowmodal(){
$js='$("#modal").modal("show")';
$this->getView()->registerJs($js);
return $this->render('create');
}
I did this as it was described here how can I add modal to navbar in yii2 using yii2 -bootstrap extension?.
When I use a modal navbar in my index.php it works. But when I use a modal navbar in my form, an error is issued: Undefined variable: model.
How can I fix it?
In order to create a form in modal you need to pass the respective model for the form
function actionShowmodal(){
$model = new RespectiveModel(); //Model For the Form
$js='$("#modal").modal("show")';
$this->getView()->registerJs($js);
return $this->render('create',["model"=>$model]);
}
and in your "create.php" view file where you are rendering the form, pass the model variable again
echo $this->render('project_form',["model"=>$model]);
Pjax for Alerts
There is alert.php included in layout file above $content. This is as below :
<?php Pjax::begin(['id'=> 'new-alert','enablePushState' => false]); ?>
<?= \odaialali\yii2toastr\ToastrFlash::widget([
'options' => [
'positionClass' => 'toast-bottom-full-width',
//'progressBar' => true,
'timeOut' => 6000,
'extendedTimeOut' => 2000
]
]);?>
<?php Pjax::end(); ?>
And to get flash messages in ajax , there are calls to below container
$.pjax.reload({container:'#new-alert'});
This results in showing of alert messages but also sends an Ajax request to URL whichever page is open. Can we trigger request to "/site/toast" on ajax calls which can renderAjax the above widget inplace of making ajax request on current url ?
Since there are no html "a" tag well as neither any "form" tag here inside widget generated html, Is this correct use of pjax ?
http://localhost:8081/about-us?_pjax=%23new-alert
Pjax for form
Also if we wrap an Active Form inside Pjax, how can we make sure that none of A tags or nested Form trigger a Pjax request except the container form ?
I have removed call for pjax from PHP and made alert as below -
<div id="new-alert">
<?= \odaialali\yii2toastr\ToastrFlash::widget([
'options' => [
'positionClass' => 'toast-bottom-full-width',
//'progressBar' => true,
'timeOut' => 6000,
'extendedTimeOut' => 2000
]
]); ?>
</div>
Instead I am making pjax call from javascript as below -
$.pjax({url: encodeURI(baseUri + "site/toast"), container: '#new-alert', push: false})
public function actionToast()
{
if(Yii::$app->request->getHeaders()->has('X-PJAX'))
{
return $this->renderAjax('//common/alert');
}
else
{
return $this->redirect(Yii::$app->request->referrer);
}
}
$.pjax.reload was previously retrieving additional toasts if there were some for current url as well. Above solution only gets toasts related to ajax.
In developing a site that can have multiple front-end themes, I am looking for a way to allow users on the back-end to manually inject code into the head and body. Two use cases are as follows:
Case 1 - Styles
In the back-end, the user selects a theme.
User likes the theme but would like to make links a different color.
Rather than copy and modify the theme, user can set custom code to execute at end of <head> tag.
Case 2 - Scripts
User wants to add custom JavaScript to end of document but requires an additional JavaScript library as well.
Rather than copy and modify the theme, user can set custom code to execute at and of <body> tag.
I understand that both of these specific cases could be accomplished (in part) with the use of registerCss and registerJs but those automatically wrap whatever is passed to them in <style> or <script> tags. I am hoping there is a way to just directly inject whatever is indicated directly into the head() or endBody() methods. The reason behind this is that I do not wish to limit what the user can inject (perhaps a script tag is needed in the head).
Currently, I am just storing the code-to-be-added in params then manually including them in each theme as follows:
<?php $this->endBody() ?>
<?= $this->params['theme_include_body_end'] ?>
This is undesirable as it can easily be forgotten when creating the theme. I would like to find a way to append my param value to the endBody() call automatically so whenever endBody() is called, my code is included (same for the head() call).
Yii2 already provide this functionality in View Class by using Block Widget
you need 2 simple steps:
1- (in required View file): in any given view
<?php $this->beginBlock('block1'); ?>
...content of block1...
<?php $this->endBlock(); ?>
...
<?php $this->beginBlock('block3'); ?>
...content of block3...
<?php $this->endBlock(); ?>
2- (in layout): define block name and its place in the layout page
...
<?php if (isset($this->blocks['block1'])): ?>
<?= $this->blocks['block1'] ?>
<?php else: ?>
... default content for block1 ...
<?php endif; ?>
...
<?php if (isset($this->blocks['block2'])): ?>
<?= $this->blocks['block2'] ?>
<?php else: ?>
... default content for block2 ...
<?php endif; ?>
...
<?php if (isset($this->blocks['block3'])): ?>
<?= $this->blocks['block3'] ?>
<?php else: ?>
... default content for block3 ...
<?php endif; ?>
...
Referance: Yii2 Guide
http://www.yiiframework.com/doc-2.0/guide-structure-views.html#using-blocks
I hope this will help someone. Thank you.
You can use own View component that overrides methods renderHeadHtml() and renderBodyEndHtml(). In these methods can be injected necessary code as you need:
namespace common/components;
class View extends \yii\web\View {
/**
* #var string Content that should be injected to end of `<head>` tag
*/
public $injectToHead = '';
/**
* #var string Content that should be injected to end of `<body>` tag
*/
public $injectToBodyEnd = '';
/**
* #inheritdoc
*/
protected function renderHeadHtml()
{
return parent::renderHeadHtml() . $this->injectToHead;
}
/**
* #inheritdoc
*/
protected function renderBodyEndHtml($ajaxMode)
{
return parent::renderBodyEndHtml(ajaxMode) . $this->injectToBodyEnd;
}
}
In config file:
// ...
'components' => [
// ...
'view' => [
'class' => '\common\components\View',
]
]
Somewhere in controller code:
\Yii::$app->view->injectToHead = '...';
\Yii::$app->view->injectToBodyEnd = '...';
Another, perhaps more simple alternative depending on your use case is to use the view events. You can inject different items at different parts of the page. For example:
Yii::$app->view->on(View::EVENT_END_BODY, function () {
echo date('Y-m-d');
});
You can read more here: https://www.yiiframework.com/doc/guide/2.0/en/structure-views#view-events