I have 3 tables:
Order
Ordered_number
Number
Ordered_number connects the other two - it has order_id and number_id columns.
In my Index Action, I want to render view named "call", where I have a form with choosing one order. When I submit which order I want, I want to go to actionCall and send there numbers connected with this order. Something isn't correct and I think I may be doing something wrong.
public function actionIndex()
{
$model = new \common\models\Order();
if ($model->load(Yii::$app->request->post())){
$numery = \common\models\Number::find()
->joinWith('ordered_number')
->where(['ordered_number.order_id' => $model->id]);
return $this->redirect(['/call', 'numery' => $numery]);
} else {
$this->render('call', ['model' => $model]);
}
}
When I open my actionIndex it displays a blank page instead of rendering a view that I want.
My "call" view:
<?php
use common\models\Customer;
use common\models\Order;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\View;
use yii\widgets\ActiveForm;
/* #var $this View */
/* #var $form ActiveForm */
/* #var $model Order */
var_dump($this);
die();
echo "<h1>Dodaj numery do przedzwonienia</h1>";
echo "Wybierz zamówienie, którego numery chcesz przedzwonić: ";
$form = ActiveForm::begin();
$form->field($model, 'id')->dropDownList(ArrayHelper::map(Customer::find()->all(), 'id', 'id'));
Html::submitButton("Przedzwoń", ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
ActiveForm::end();
I added var_dump in my view, and it displays it, but without var_dump and die() command, there is simply blank page without text I want to be next.
Missing return statement in else
return $this->render('call', ['model' => $model]);
Related
I created a table named (users) it has two columns: name and pass.
In view I created form like this:
<?php
$form = ActiveForm::begin() ?>
<div class="form-group">
<?= $form->field($model, 'user') ?>
<?= $form->field($model, 'password') ?>
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end() ?>
So I need to pass this two attributes to controller to insert into data base.
In YourController.php
assuing you have an Create action for manage your insert in db
and a create view for manage the user input (a view for only display the correct result )
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\EntryForm;
class YourController extends Controller
{
// ...existing code...
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// valid data received in $model
//
if( $model->save()){
return $this->render('create', ['model' => $model]);
} else {
// this is just for debug
var_dump('not inserted');
die();
}
} else {
// either the page is initially displayed or there is some validation error
return $this->render('view', ['model' => $model]);
}
// render the initial page per allow to the user input
return $this->render('create', ['model' => $model]);
}
....
}
In your controller you can get your post form in this way:
Yii::$app->request->post()
And this way you can load the value into your model
$model->load(Yii::$app->request->post())
Here you can find a starting point
I have a problem.
I need to open a modal to insert data in a table in the form of another table.
I can open a modal using Ajax with the view of the create view using the actionCreate from the controller, but when i clic save in the modal, the controller redirect me to the action view by default, and if i chage that, it open the full view in the current tab.
The problem was temporary resolved by making a redirect to the view that the modal should be in the if condition, but when i test to insert invalid data, the view render full-windowed with the message errors (in ajax render).
Also, the data always is save.
I know there's a way to render it in the modal without change view, but i'm new and not have any experience working with modals.
Need your help please.
There's my view file:
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use yii\bootstrap\Modal;
use yii\helpers\Url;
use app\models\Catalogo;
/* #var $this yii\web\View */
/* #var $model app\models\Articulo */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="articulo-form">
<?php $form = ActiveForm::begin(['id'=>'articuloCreate']); ?>
<?= $form->field($model, 'ID_CATALOGO')->dropDownList(
ArrayHelper::map(Catalogo::find()->all(),'ID_CATALOGO','NOMBRE_CATALOGO'),
[
'style'=>'width:500px',
'prompt'=>'Seleccionar catálogo',
]); ?>
<?= Html::button('Agregar catálogo', [
'value' => Url::to(['catalogo/create']),
'class' => 'btn btn-primary',
'id' => 'BtnModalCatalogo',
'data-toggle'=> 'modal',
'data-target'=> '#catalogo',
]) ?>
<?php ActiveForm::end(); ?>
The modal in the same view:
<?php
Modal::begin([
'header' => 'Crear catálogo',
'id' => 'catalogo',
'size' => 'modal-md',
]);
echo "<div id='modalContent'>BtnModalCatalogo</div>";
Modal::end();
?>
There's more code, but i think is pointless putting it.
This is my JS:
$('#BtnModalCatalogo').click(function(e){
e.preventDefault();
$('#catalogo').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
return false;
});
And this is the action create in the controller:
public function actionCreate()
{
$model = new Catalogo();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->ID_CATALOGO]);
}
if(Yii::$app->request->isAjax){
return $this->renderAjax('create', [
'model' => $model,
]);
}
return $this->render('create', [
'model' => $model,
]);
}
Just in case, this is the action view:
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
First of all, thanks.
At the beggining, i expectet that the controller should load any other views in the modal, but not was like that.
This action only responds when the action is called via ajax and will only redirect if the model is saved.
public function actionCreate()
{
$model = new Catalogo();
if ($model->load(Yii::$app->request->post())) {
if($model->save()) return $this->redirect(['view', 'id' => $model->ID_CATALOGO]);
}
return $this->renderAjax('create', [
'model' => $model,
]);
}
I am newbie to yii2. I am trying to create my simple form in yii2 to retrieve password. Here is class code:
<?php
namespace app\models;
use yii\base\Model;
class RetrievePasswordForm extends Model
{
public $email;
public function rules()
{
return [
['email', 'required'],
['email', 'email'],
];
}
}
Here is action code:
$model = new RetrievePasswordForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()){
return $this->render('retrievepassword-confirm', ['model' => $model]);
} else {
return $this->render('retrievepassword', ['model' => $model]);
}
My form looks like this:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title = 'Retrieve password';
$this->params['breadcrumbs'][] = $this->title;
?>
<h1><?= Html::encode($this->title) ?></h1>
<p>We will send link to retrieve your password to the following email:</p>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'email')->textInput(['style'=>'width:200px'])?>
<div class="form-group">
<?= Html::submitButton('Send', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
The problem is that $model->load(Yii::$app->request->post()) always returns false, so when I am clicking "submit" button, page just reloads.
I am currently working without database. I am just creating form and trying to go to another form, when valid data received in model. Thanks for help.
Try explicitally assign the method and the action to the active Form
Then Assuming that your target action is named actionRetrivePassword
<?php $form = ActiveForm::begin([
'method' => 'post',
'action' => Url::to(['/site/retrivepassword']
); ?>
I'll go with I feel it's wrong, if this doesn't help, please give more information.
It's a registration form, so I assume you need email and password for that (since you have those columns in database). But you also declared public member $email in your model. This removes any value associated to $email from database. Therefore, remove this line:
public $email;
I Have my model with 2 fields Product.php:
[['ID_PRODUCT'], 'integer'],
[['NAME_PRODUCT'], 'string'],
my Controller ProductController.php:
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->ID_PRODUCT]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
And i want insert many times the same table with ActiveForm:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'ID_PRODUCT')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'NAME_PRODUCT')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ID_PRODUCT')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'NAME_PRODUCT')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
But when i save the information the fields are overwritten and only the last record is inserted
What you are trying to do is collect, validate and save tabular data. The reason it doesn't work is that in the form, Yii generates a name tag based on the field name and model, e.g. name="[Product]["ID_PRODUCT"]. When the form is sent to the server, the first fields get overwritten by the last ones, as they have the same name. The correct way to collect tabular input in a form is to add brackets at the end of the name, like this; name="[1][Product]["ID_PRODUCT"].Using this method, Yii gives ways of loading and validating multiple models.
Modify your controller code to use multiple models;
<?php
namespace app\controllers;
use Yii;
use yii\base\Model;
use yii\web\Controller;
use app\models\Product;
class ProductController extends Controller
{
public function actionCreate(){
//Find out how many products have been submitted by the form
$count = count(Yii::$app->request->post('Product', []));
//Send at least one model to the form
$products = [new Product()];
//Create an array of the products submitted
for($i = 1; $i < $count; $i++) {
$products[] = new Product();
}
//Load and validate the multiple models
if (Model::loadMultiple($products, Yii::$app->request->post()) && Model::validateMultiple($products)) {
foreach ($products as $product) {
//Try to save the models. Validation is not needed as it's already been done.
$product->save(false);
}
return $this->redirect('view');
}
return $this->render('create', ['products' => $products]);
}
}
Now you have all the data you need to populate the form, including any error messages generated for individual instances of you product model. The view file for the form needs to be altered like this, to use the multiple models;
foreach ($products as $index => $product) {
echo $form->field($product, "[$index]ID_PRODUCT")->label($product->ID_PRODUCT);
echo $form->field($product, "[$index]NAME_PRODUCT")->label($product->NAME_PRODUCT);
}
All of this is covered in the Yii2 documentation
Is there any way to render partial view which contains a part of form that it's main part is in another view file with AJAX?
I exactly mean one form variable:
`<?php $form = ActiveForm::begin(['enableAjaxValidation' => true,]); ?>`
For Example :
Controller
public function actionOlddetform()
{
return $this->renderAjax('_olddet');
}
View
<?php $form = ActiveForm::begin(['enableAjaxValidation' => true,]); ?>
<?= $form->field($model, 'date')->input() ?>
<?= $form->field($model, 'annotations')->textarea(['rows' => 3]) ?>
<div id="details-form"></div>
<?php ActiveForm::end(); ?>
Part of form included with AJAX for details-form container depends on date value. I know how to check date and show any content of that partial view but when I want to include a part of form I get an error:
PHP Notice 'yii\base\ErrorException' with message 'Undefined variable: form'
It seems you forgot to actually pass the model into your view:
public function actionOlddetform()
{
return $this->renderAjax('_olddet', ['model' => $dataModel]);
}
And if you want to render "sub-views" from your main view, you need to pass the variables in there as well (even though I don't see a render call in your view):
<?= $this->render('_formPart', ['form' => $form, 'model' => $model]) ?>