I have a text field in my view and I want to pass the value of textbox to the controller, but I don't know how to do it.
I have tried googling it but it only gives ides about passing data from conntroller to view in yii, so please give an example of doing this with ajax.
follow the steps:
in form:
<div class="form-group">
<?php echo $form->labelEx($model,'order_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php echo $form->textField($model,'order_id',array('class' => 'form-control',
'ajax' =>
array('type'=>'POST',
'url'=>$this->createUrl('recieveValue'), // write in controller this action
'update'=>'#price',
'data'=>array('order_id'=>'js:this.value'),
)
)); ?>
</div>
<?php echo $form->error($model,'order_id'); ?>
In controller:
public function actionRecieveValue(){
echo $_POST['order_id'];
}
At the top of same controller:
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','recieveValue'),
'users'=>array('#'),
),
Explanation:
Here text field id is order_id, controller action is recieveValuewhat I wrote in the ajax URL as 'url'=>$this->createUrl('recieveValue'),. Go to the controller and write action name as actionRecieveValue just add action before recieveValue. Now go to the top of the controller in the method accessRules and allow it recieveValue into array. Now check through firebug console. Type something into the text box and move the mouse from the textbox. You will find that your textbox value will be recieved into the controller.
Related
I can't find any supporting resources for the error Call to a member function formName() on null. The scenario of getting this error when opening a update page. I've checked the rendered variable on update page by print_r() and its fine.
Then I checked by removing the all input field which codes in yii2 ActiveFrom chtml
<?= $form->field($model, 'name')->textInput(['maxlength' => true, 'class' => ' form-control']) ?>
and kept some input fields which is in normal html syntax
<input type="text" class="qty" name="qty[]" value="1" style="width: 24px;" value="<?php echo $value->qty; ?>">
Then it worked.
Why this error occurred and how to fix it.
Thanks,
You are probably not passing an update model from controller's action to view. Make sure you pass the loaded model in your update action to view like this:
public function actionUpdate($id)
{
$model = $this->findModel($id);
//... other update action code
return $this->render('update', ['model' => $model]);
}
If you are using some common _form template that you include into your create/update templates you also have to make sure the $model is passed from your update template to the _form template.
<!-- ... your update template code ... -->
<?= $this->render('_form', ['model' => $model]); ?>
<!-- ... the rest of update template ... -->
Another option is that you are overwiriting the $model variable somewhere in your templates.
I'm developing a website in php with Yii2 and I have a problem with Rbac issue. I've followed the offical guide, I run the migrations and now I have in my db the four default tables which define my roles and permissions. Now I don't know how to integrate these roles in my project, I mean I would like to have some views only visible to users with specific permissions but can't understand the way to implement this.
I have also a problem with login, I don't know how to discriminate a button click.
login (view):
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button', 'value' => 'login']) ?>
<?= Html::submitButton('Register', ['class' => 'btn btn-primary', 'name' => 'register-button', 'value' => 'register']) ?>
</div>
</div>
SiteController:
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if (isset($_POST['submit']) && $_POST['submit']=='login') {
return $this->goBack();
}
if (isset($_POST['submit']) && $_POST['submit']=='register') {
return $this->render('register');
}
return $this->render('login', [
'model' => $model,
]);
}
I just need to render in a different views the user after the right button click. If Login button is clicked I want to be redirected in login view, if Register button is clicked, I want to be redirected in register view.
This seems to be a two-in-one question.
First, RBAC.
This is explained very well in the docs. You can use AccessControl to only allow certain actions to be accessed by a role or permission. If you need to show some content in a view based on a role or permission, use if(Yii::$app->user->can('permission_or_role)) echo "I can"; (docs).
Second, buttons
Check this link, the name of the button must be the name you check for in the controller (not login-button/register-button and check submit).
I am trying to post a hidden value with yii2 active form, but it's not defined in my database table. Which is why the active record model from that table produces a "Getting unknown property" error.
Is there any way to post a value with active form without making its field in database table or defining it in the active record model to just post the value with the form?
This is my form:
<?php
$form = ActiveForm::begin([
'action' => ['twit/update-reply'],
]);
?>
<?= $form->field($model,'twit')->textarea(['value' => $twit]); ?>
<?= $form->field($model,'id')->hiddeninput(['value' => $id]); ?>
<?= $form->field($model,'rid')->hiddeninput(['value' => $rid]); ?>
<?= Html::SubmitButton('بروز رسانی',['class' => 'btn btn-success green']); ?>
<?php ActiveForm::end(); ?>
The $form->field($model,'rid') input in this form is not defined in model and causes the above mentioned error.
What am I doing wrong?
When you use ActiveForm, you can add Model property only as a field. For solve the problem, you have two solution:
Define a property on your model,
Post your hidden input without ActiveForm field, i.e. replace
<?= $form->field($model,'id')->hiddeninput(['value' => $id]); ?>
with
<input type="hidden" name="id" value="<?= $id ?>" />
ActiveForm as is in its name, show behavior of model actively, for example if you define a rule on your model for attribute called userEmail that must be an email, ActiveForm check your rule that userEmail be have a pattern like emailName#emailHost.emailDomain (More precisely ([A-Za-z0-9\_\-]+)#([A-Za-z0-9]+).([A-Za-z0-9\_\-]{2,3}) for example), then if your model, be an instance of a record on your table, ActiveForm populate your field on your HTML form with saved value.
In my index.php view, I have a button, once clicked, a modal will pop up:
<p>
<?= Html::button(
'Create New BizAdmin',
['value' => Url::to(['createbizadmin']),
'class' => 'btn btn-success',
'id' => 'modalButton'
]) ?>
<?php
Modal::begin(['id' => 'modal']);
echo "<div id='modalContent'></div>";
Modal::end();
?>
</p>
My modal file is createbizadmin.php where it has the following codes:
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model app\models\User */
$this->title = 'Create New Bizadmin';
?>
<div class="user-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_formbizadmin', [
'model1' => $model1,
'model2' => $model2,
]) ?>
</div>
My problem is this:
As you can see, the navbar looks horrible. The menu list seems to overflow outside the modal.
How do I get rid of the navbar inside my modal? I can't seem to find which part of creatbizadmin.php I should edit.
Any thoughts?
I'm guessing that you have a controller somewhere that is handling the url createbizadmin. I'm also guessing that inside that controller action you are rendering the view file like this;
$this->render("createbizadmin");
If so, then that is your problem. By calling a view file directly, Yii will apply default layouts to the view file. You have no control over how this happens from within the called view file, and all your menus etc will be rendered.
To get around this you will probably need to render a partial file. This rendres the file without applying layouts. So use;
$this->renderPartial("createbizadmin")
Alternatively, if the modal content is being generated as a result of an ajax call, you can respond from the controller with;
$this->renderAjax("createbizadmin")
This article seems to have a good explanation of how best to achieve this; Render a form in a modal popup
I'm using Laravel framework and i have two dropdown lists of which both are reading data from the database tables,
the first one it read all the records from the table and populate it on the select list
here is my code:
<div class="form-group">
{{Form::select('advertiserName', Advertiser::all()->lists('advertiserName', 'advertiserId'))}}
</div>
it display the advertiser name and also passes the id on the advertiser.
The second dropdown i want it to display all the Brands related to the above selected advertiser.
here is my code:
<div class="form-group">
{{Form::select('brandName', Brand::where('advertiserId', '=', '3')->lists('brandName', 'id'))}}
</div>
The advertiserId is the foreign key of the advertiser and 3 must be replaced by the the advertiserId you selected on the first dropdown list, basically i want to pass variable from the first select list to the second one
Thank you
Basically you need to use AJAX to get data for Second Dropdown based on option selected from first dropdown.
Create Second Dropdown inside specific div and After ajax Call replace contect of div from ajax response.
As stated, the best way to achieve this is via an AJAX request. Here is an example of how you can achieve this functionality.
Firstly create a view partial that renders the select box for brands. Something like this (this code is based on Bootstrap 3):
{{ Form::label('brandName', 'Brand', ['class' => 'control-label col-sm-2']) }}
<div class="col-sm-10">
{{ Form::select('brandName', $brands, null, ['class' => 'form-control']) }}
</div>
Now, in your routes.php file add the following route:
Route::get('brand-list/{id}', ['as' => 'brands', 'uses' => 'BrandsController#brandList']);
In your BrandsController.php file (or whatever controller your using to handle this) add a method called brandList();
public function brandList($id)
{
// Return an array of brands for the selected advertiser name ID if available
$brands = $this->brand->where('advertiserId', $id)->lists('brandName', 'id'); // I'm using dependency injection here so you'll need to assign your Brand model to the $this->brand property of this classes constructor method
if ( ! isset($brands))
{
$brands = ['No brands available' => null]; // return a generic array containing 1 item with a value of null so you can handle it in your view. You could also return something else here such a boolean of false and then handle that in your AJAX request to return an error or info message
}
return View::make('brands.partials.select', compact('brands'));
}
Now for the AJAX:
$('#advertiserName').change(function(e) {
var advertiserId = $(this).val();
var url = '/brand-list/' + advertiserId;
$.get(url, function(data) {
$('.brands').html(data); // The returned view with the brand select input
});
});
Then in your initial form view you'll want to create an empty div with a class of .brands that we'll use to populate with the HTML returned via our AJAX request. Something like this if you're using Bootstrap 3 for example:
<div class="form-group brands"></div>
This should work as you need but I haven't tested this code, so if you have any issues let me know. Obviously this is generic code based on the info available and there are many ways to skin a cat.