If I have the following form field (code below) how can I set the field as required within my model so that like other fields the form cannot be submitted without it containing information.
<?= $form->field($model, 'seo[seo_title]')->textInput(['maxlength' => 60])->label('SEO Title') ?>
Add the value for te textinput in the options eg:
<?= $form->field($model, 'seo[seo_title]')->textInput(['maxlength' => 60], 'value'=> $yourValue )->label('SEO Title') ?>
or if you only need these fields required mark required in the model
Related
I have an activeform field.
<?= $form->field($model, 'featured_listings')->dropDownList([1=>'Yes',0=>'No']); ?>
I need this field hidden on create and shown on update.
Add if() statement which uses $model->isNewRecord:
if($model->isNewRecord) {
//do your stuff here
}
Or if you want to have just HTML hidden, add proper class by $model->isNewRecord ? 'hidden' : ''
How do I get a value and make it appear on a dropDownlist on yii2
For example I have 2 models which is hotel guest and service request.
I wanna get the hotelguest_id value from its model and store it on a dropdown on the service request.
Similar like this.
<?= $form->field($model, 'hotelguest_id')>dropDownList(yii\helpers\ArrayHelper::map(hotelguest_id::find(->all(),
'id', 'id')) ?>
I already figured it out
By using
use app\models\Hotelguest;
use yii\helpers\ArrayHelper;
<?= $form->field($model, 'hotelguest_id')->dropDownList(ArrayHelper::map(Hotelguest::find()->all(),
'id', 'id'), ['prompt' => 'Select Hotel Guest ID']); ?>
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.
I am creating a blog using Yii2. I have basic DB structure having tables:
Posts
Categories
Posts_Categories
I am using Yii2 ActiveForm to create post creation form. There are input fields for Title (text field), Content (text area), Categories (list box for multiple category selection).
I am not able to populate listBox with db values.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'Title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'Content')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName',['multiple' => true])); ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
It is throwing following error:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: backend\models\Category::1
There is error in listbox line. Secondly, after populating the data in listbox, how can I handle data insertion with respect to posts and multiple categories relationship.
You have syntax error in listBox. so, complete end parenthesis ) before multiple property.
Like as,
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName'),['multiple' => true]); ?>
For the second part of your question on data insertion. You may need to traverse the array of the selected (posted) items in the list box. Here is an example that may be included in the actionCreate() or actionUpdate() functions of your controller file (I am assuming PostController.php):
$selectedList = $_POST['model_name']['CategoryId'];
if(isset($selectedList)) {
foreach($selectedList as $value){
$pcmodel = new Post_Categories(); //assumption on model name
//do neccessary check here
$pcmodel->post_id = $postmodel->id;
//assumption that value is being stored
$pcmodel->category_id = $value;
$pcmodel->save();
}
}
I want to do some AJAX validation on my ActiveForm, but I want to make AJAX validation false for a single field.
How should I do that in Yii2?
My code for single field:
<?= $form->field($modelChangePassword, 'CurrentPassword',
[
'options'=>['enableAjaxValidation' => false])->textInput(['placeholder'=>Yii::t('frontend/changePassword','CurrentPasswordPlaceholderText')
])
?>
Try this.
<?= $form->field($modelChangePassword, 'CurrentPassword',
['enableAjaxValidation' => false])->textInput(['placeholder'=>Yii::t('frontend/changePassword','CurrentPasswordPlaceholderText')
])
?>