PHPackages                             inspire-agency/yii2-ajax-new - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. inspire-agency/yii2-ajax-new

ActiveYii2-extension[Framework](/categories/framework)

inspire-agency/yii2-ajax-new
============================

This widget allows you to easily create a new related entity from within a form in Yii, in a bootstrap Modal view.

3383PHP

Since Feb 11Pushed 11y ago5 watchersCompare

[ Source](https://github.com/inspire-agency/yii2-ajax-new)[ Packagist](https://packagist.org/packages/inspire-agency/yii2-ajax-new)[ RSS](/packages/inspire-agency-yii2-ajax-new/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

yii2-ajax-new
=============

[](#yii2-ajax-new)

This widget allow you to easily create a new related entity instance from within a form in Yii, in a bootstrap Modal view.

Let's suppose for instance that you have an Article model with a manyToOne relation with a Category model (article.category\_id = category.id)

When creating a new Article, you realize that you didn't create the target Category yet. You would normally cancel the Article creation, create the Category, and then go back to the Article creation.

yii2-ajax-new will allow you to avoid that hassle by creating the new Category right away from the Article form.

A complete sample if provided below.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require inspire-agency/yii2-ajax-new "*"
```

or add

```
"inspire-agency/yii2-ajax-new": "*"
```

to the require section of your `composer.json` file.

Configuration
-------------

[](#configuration)

Assuming you have created both Article and Category models with the relation Article.category\_id = Category.id, you will need to:

1. Configure your CategoryController as follows:

```
    public function actions()
    {
        return [
            'ajaxNew' => [
                'class'      => '\inspire\action\AjaxNewAction',
                'viewFile'   => '_form',
                'model'      => new Category(),
                'attr_id'    => 'id',
                'attr_label' => 'label',
            ]
        ];
    }
```

2. Add the AjaxNew widget in your article/\_form view:

```
$newCategory = AjaxNew::widget([
    'url' => Url::toRoute(['/category/ajaxNew']),
    'header' => '' . Yii::t('app', 'Create new category') . '',
    'selector' => '#article-category',
]);

// ...
