I want to update form with clone values in yii2 - php

In update from, I am using values of two models with clone fields, in models variable, there are values but from not showing.
this error is showing
PHP Fatal Error – yii\base\ErrorException
Call to a member function isAttributeRequired() on array
my form code:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use frontend\models\PartyM;
use frontend\models\ProductM;
use frontend\models\Category;
/**
To add date picker and multiselection from dropdown list
Date: 03 April 2017
*/
use kartik\date\DatePicker;
use yii\helpers\Url;
/* #var $this yii\web\View */
/* #var $model frontend\models\SaleorderM */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="saleorder-m-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($modelm, 'ac_code')->dropDownList(ArrayHelper::map(PartyM::find()->where('type= :type', ['type' => 'S'])->all(),'ac_code','firmname'),['prompt'=>'Select Firmname'])->label("Firm Name");?>
<div id='main-product-section form-field' class='clonediv'>
<div class="product-section1 test1" id='1'>
<div class="product-area">
<?= $form->field($modeld, 'categoryid[]')->dropDownList(ArrayHelper::map(Category::find()->all(), 'categoryid', 'categoryname'),['prompt'=>'Select....'],['class' => 'your_class', 'id' => 'brandid1'])->label("Brand") ?>
</div>
<div class="code-area">
<?php
echo $form->field($modeld, 'productcode[]')->dropDownList(['prompt'=>'Select....'],['id'=>'brandid1-productcode'])->label("Product Code")?>
</div>
<div class="qty-area ">
<?= $form->field($modeld, 'qty[]')->textInput()->label("Quantity"); ?>
</div>
<div class="btn-area del">
</span>
</div>
</div>
</div>
<div class="form-group">
<div class="btn-area add">
<a href='javascript:void(0)' class='add_more' id='add_more' >Add More</a>
</div>
</div>
<?= $form->field($modeld, 'ordertype')->textInput()->label("Order Type"); ?>
<?php
// To add date picker
echo $form->field($modelm, 'orderdate')->widget(DatePicker::classname(), [
'name' => 'Saleorder Date',
'type' => DatePicker::TYPE_COMPONENT_APPEND,
'value' => date('Y-m-d'),
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd'
]
])->label("Saleorder Date"); ?>
<?= $form->field($modelm, 'courier')->textInput(['maxlength' => true])->label("Courier"); ?>
<?= $form->field($modelm, 'couriermode')->textInput(['maxlength' => true])->label("Courier Mode"); ?>
<?= $form->field($modelm, 'ordersource')->textInput(['maxlength' => true])->label("Order Source"); ?>
<?= $form->field($modelm, 'remarks')->textarea(array('rows'=>2,'cols'=>5)) ?>
<div class="form-group">
<?= Html::submitButton($modelm->isNewRecord ? 'Create' : 'Update', ['class' => $modelm->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

Related

Yii2 send id to hidden input via AJAX and reach it through POST

I want to send the data.user_id to hidden input with name user_id and after that take it through POST in the another html::a elem.
My view:
<?php
use yii\helpers\Html;
use yii\widgets\Pjax;
/* #var $this yii\web\View */
/* #var $searchModel app\models\PostSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
?>
<div class="post-index" style="position:relative;">
<div class="content">
<div class="row">
<div class="col-md-12 search-post">
<div class="col-md-4">
<?= Html::a('Create Post', ['create'], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Search', [''], ['class' => 'btn btn-default search-form']) ?>
<?= Html::radioList('search-type', 'search-type',
['item1' => 'by title', 'item2' => 'by tag'],
[
'style' => 'display: inline-block; margin-left: 10%'
]) ?>
</div>
<div class="col-md-8 search-input">
<?= Html::textInput('search-info', '',['class' => 'form-control']) ?>
</div>
<?php
$this->registerJs("
$('.search-form').on('click', function(event){
event.preventDefault();
var input = $('input[name=search-info]').val();
$.ajax({
type : 'GET',
url : '" . \yii\helpers\Url::to(['search-post']) . "?title='+input,
dataType : 'json',
success : function( data ){
$('#posts').css('display', 'none');
$('.none').fadeIn(1500);
$('.title-post h3').html( data.title );
$('.content-post').html( data.content );
$('input[name=user_id]').val( data.user_id);
}
});
});
");
?>
</div>
</div>
<div class="row">
<div class="none" style="display: none;">
<div class="title-post text-center"><h3></h3></div>
<div class="content-post"></div>
<div class="foot-post">
<form action="" method="post">
<?= Html::hiddenInput('user_id', '') ?>
</form>
<div class="text-left">
<?= Html::a('Go To Post', ['view', 'id' => $_POST['user_id']]) ?>
</div>
<div class="text-right">
<?= Html::a('Back','index', ['class' => 'btn btn-warning']) ?>
</div>
</div>
</div>
<?php Pjax::begin(['id' => 'posts']) ?>
<div class="col-md-12">
<?php
foreach ($posts as $post)
{
$id = $post->user_id;
$user = $userModel::find()->where(['id' => $id])->one();
echo "<div class='col-md-6 blog-post'>
<div class='col-md-3 post-prof-img'>"
. Html::img('../images/' . $user->image, ['alt' => 'image', 'class' => 'tall img-circle']) .
"<p class='text-center title-par'><strong><em> $user->username </em></strong></p>
<p class='text-center'> $post->date_create</p>
</div>
<div class='col-md-9 post-cont'><p>"
. Html::a('Click for more!',['view', 'id' => $post->post_id]) .
"</p><label class='text-center'> $post->title : </label>
<div class='fade-post'>
$post->content
</div>
</div>
</div>";
}
?>
</div>
<?php Pjax::end() ?>
</div>
</div>
</div>
As I can see it is not possible the way I am trying so please give some advice! Thank you in advance!
I think you got wrong idea about $_POST. The Undefined index: user_id is simply thrown because you have not submitted the form therefore $_POST['user_id'] does not exist.
You should change your code to use plain variable which will not be based on $_POST.
<?= Html::a('Go To Post', ['view', 'id' => $idThatIsNotPOST]) ?>
If you want to change the link using ajax you will have to alter its href. Something like: $("a").attr("href", "<replace current href id with ajax's>").

Rendering two models in View yii2

I am trying to render two models in the actionView method, but without result. Checked some answers on previous questions and it seems my method is OK, but still do not work.
This is what a made for method view:
public function actionView($id)
{
$postModel = Post::find()->where(['post_id' => $id])->one();
$currUser = BlogUser::find()->where(['id' => $postModel->user_id])->one();
return $this->render('view', [
'model' => $this->findModel($id),
'author' => $currUser
]);
}
and my view page:
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* #var $this yii\web\View */
/* #var $model app\models\Post */
//$this->title = $model->title;
//$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];
//$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-view">
<div class="content">
<div class="row">
<div class="col-md-3">
<?= $model->content;
$author->username;
?>
</div>
</div>
</div>
</div>
It tells that the author variable is undefined but cannot realize why when i render it in the action. Will appreciate every advice! Thank you!
Try using:
<div class="post-view">
<div class="content">
<div class="row">
<div class="col-md-3">
<?= $model->content; ?>
<?= $author->username; ?>
</div>
</div>
</div>
</div>

Yii2 Using If else in Views _Form

I Have 2 textinputs called Address and Mail_address
I use Jquery so when I input some texts in Address, Mail_address has exactly same texts with Address.
Now I want to add 1 checkbox so when I click that checkbox,
Mail_address has become disabled or readonly and also it has same value as Address
But How do I implement if else for the checkbox in Jquery or in _form ?
Thank You
Here's my views _Form
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\bootstrap\Modal;
use yii\helpers\BaseHtml;
use app\assets\myassets\InputAsset;
?>
<?php
$button = Html::button('Cancel', ['class' => 'btn btn-default btn-md', 'data' => ['dismiss' => "modal"]]);
$button .= Html::button('Submit', ['id' => 'submit', 'class' => 'btn btn-success success btn-md']);
Modal::begin([
'header' => '<h2>Konfirmasi</h2>',
'toggleButton' => false,
'id' => 'confirm-submit',
'footer' => $button
]);
echo 'Save ?';
Modal::end();
?>
<?php
$this->registerJs('
$(document).ready(function () {
$(document).on("change" ,"#'. Html::getInputId($model ,'Address') .'" ,function(){
$("#'. Html::getInputId($model ,'Mail_Address') .'").val();
var first = $("#'. Html::getInputId($model ,'Address') .'").val();
var third = first;
$("#'. Html::getInputId($model ,'Mail_Address') .'").val(third);
});
});
');
?>
<div class="tcust-form">
<?php $form = ActiveForm::begin([
'id' => 'marketing',
]);
?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'Address')->textInput() ?>
<?= $form->field($model, 'Mail_Address')->textInput() ?>
<?= $form->field($model, 'SameasAbove')->checkbox() ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
all you need to do is to write a change function for checkbox like
$('#checkboxID').on('change',function(){
if($('#checkboxID).prop('checked'))
{
var address = $('#addressID').val();
$('#mailAddress').val(address).addClass('disabled');
}
else
{
if($('#checkboxID).hasClass('disabled')
{
$('#mailAddress).removeClass('disabled');
}
}
})

Pjax update multiple blocks Yii2

I learn Yii2 and I decided to get acquainted with the work of technology Pjax on site: http://blog.neattutorials.com/yii2-pjax-tutorial/. There is example "Multiple blocks". But it is implemented as a example version, and is not quite correct. It is written below. In this example in one action actionMultiple calculating string and key in one place, but it must be relised into different actions. So I decided to do it right but collided with the problem that when I click on the link it redirect me to a new page with the generation of a string or key. I need to do it in same page without reloading.
Controller:
public function actionMultiple()
{
$security = new Security();
$randomString = $security->generateRandomString();
$randomKey = $security->generateRandomKey();
return $this->render('multiple', [
'randomString' => $randomString,
'randomKey' => $randomKey,
]);
}
public function actionString()
{
$security = new Security();
$randomString= $security->generateRandomString();
return $this->render('_randomString', [
'randomString' => $randomString,
]);
}
public function actionKey()
{
$security = new Security();
$randomKey = $security->generateRandomKey();
return $this->render('_randomKey', [
'randomKey' => $randomKey,
]);
}
view multiple:
<?php
use yii\widgets\Pjax;
use yii\bootstrap\Html;
?>
<div class="col-sm-12 col-md-6">
<?php Pjax::begin(); ?>
<?= Html::a("Generate Random String", ['site/string'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomString ?></h3>
<?php Pjax::end(); ?>
</div>
<div class="col-sm-12 col-md-6">
<?php Pjax::begin(); ?>
<?= Html::a("Generate Random Key", ['site/key'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomKey ?><h3>
<?php Pjax::end(); ?>
</div>
view _randomString:
<?php
use yii\helpers\Html;
?>
<?= Html::a("Generate Random String", ['site/string'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomString ?></h3>
view _randomKey:
<?php
use yii\helpers\Html;
?>
<?= Html::a("Generate Random Kay", ['site/key'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomKey ?><h3>
Please tell me what I'm doing wrong.

How to create form in view from different model in yii2?

Iam new to yii. I am developing customer project app. I have a view wherein iam displaying the data from both the models, customer and projects.
How do i create a form to add new projects?
my project is here
To display project data in customer view, iam using
$query=Projects::find()
->where(['projects_clients_id'=> $model->customer_id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
]);
You can render several model and/or dataProvider in a view (properly constructed)
eg:
return $this->render('viewTestMulti', [
'modelOne' =>$modelOne,
'dataProviderTwo' => $providerTwo,
'dataProviderThree' => $providerThree,
'modeFour' => $modelFour,
]);
And then you can use a view with several gridView related to proper dataProvider and several forms everyone with a proper action
so when you press the specified submit you invoke the proper controller action
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $formOne = ActiveForm::begin();
$formOne->action= yii\helpers\Url::to('ControllerOne\create');
?>
<?= $formOne->field($modelOne, 'name') ?>
<?= $formOne->field($modelOne, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php $formFour = ActiveForm::begin();
$formFour->action= yii\helpers\Url::to('ControllerFour\create');
?>
<?= $formFour->field($modelFour, 'name_four') ?>
<?= $formFour->field($modelFour, 'email_four') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
I hope this could be useful
You have all the documentation about ActiveForms and I recommend, if you are new in Yii2 to use The Definitive Guide to Yii 2.0, here the Forms Section
This is a basic form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>

Categories