I would like to use an activeForm and linkPager together.
I mean in the same view, I have defined an activeform with some fields, an sqldataprovider and the showing of the model result (of sqldataprovider). All is nice when I click on the submit button of the activeform.
Now, I would like to add a linkPager by using :
echo \yii\widgets\LinkPager::widget([
'pagination'=>$dataProvider->pagination,
]);
And when I click and one of buttons of linkpager (to change page), the fields of the activeform are not linked, I mean : the datasqlprovider does not filter with these fields...
I think, you need to populate the ActiveForm manually based on the request parameter:
$searchModel = new JobSeekerSearch(); // extended from JobSeeker
$params = Yii::$app->request->queryParams; // this is the query string parameter
if( !empty( $params['JobSeekerSearch'] ) ){
$searchModel->fullName = $params['JobSeekerSearch']['fullName']; // populate your model
}
$dataProvider = $searchModel->search($params); // submit the search parameter to your search model
then make sure to create your ActiveForm based on the model(JobSeekerSearch for this example)
Related
I use ActiveDataProvider and Gridview in my site. Get parameters appear in url only after searching for some field (they are missing on the index page when the site first loads).
How can I by default add some GET parameters in url when first loading the index page has not yet been searched.
If your action is called "mySearch", here an example to use a default value if parameters is missing:
public function actionMySearch()
{
// Flat php
$mySearchName = isset($_GET['MySearch']['name']) ? $_GET['MySearch']['name'] :'John';
$mySearchEmail = isset($_GET['MySearch']['email']) ? $_GET['MySearch']['email'] : 'john#test.com';
// Yii helpers
$mySearchName = \yii\helpers\ArrayHelper::getValue($_GET, 'MySearch.name', 'John');
$mySearchEmail = \yii\helpers\ArrayHelper::getValue($_GET, 'MySearch.email', 'john#test.com');
}
in my db structure
service_request type enum('towel','tissue','napkin')
then have a model
* #property string $service_request
then in my view
<?= $form->field($model, 'service_request')->checkBoxList([ 'towel' => 'Towel', 'tissue' => 'Tissue', 'napkin' => 'Napkin']) ?>
then when i choose towel, tissue and napkin then submit the form, it's have an error said
Service Request must be String
please help me
Thank You
Like Joji Thomas said, checkBoxList prodices an array.
You need to change your database structure so that it supports 1-to-many relations (each $model can have multiple service_requests) if you want to save this. Unfortunately Yii is not very good at this sort of thing out of the box so you have to do a bunch of things yourself.
First you need to create a ServiceRequest ActiveRecord.
Then your $model needs to have a relation like:
public function getServiceRequests() {
return $this->hasMany(ServiceRequest::className(), ['model_id' => 'id'];
}
Then in your controller (model create action) you will need to do something like this:
foreach (Yii::$app->request->post('ServiceRequest',[]) as $data) {
$item = new ServiceRequest($data);
$model->link('serviceRequests', $item);
}
If you wanna update the checkboxes too then you need to do something similar in your model update action as well.
Please change checkBoxList to radioList, because when selecting multiple values service_request becomes an array. Enum type can handle only string values.
First change your filed datatype from enum to varchar. enum only takes a single string value.
Secondly you need to implode service_request array to string for save to db.
Use bellow code before the model save function :
$model->service_request = implode("," , $model->service_request);
$model->save();
In my controller in yii2 I have the following:
$searchModel = new HealthSearch();
$dataProvider = $searchModel->search(['HealthSearch'=>['zip'=>$zipcode]]);
which works but I would like it to also search for zipcodes and speciality
I tried:
$dataProvider = $searchModel->search(['HealthSearch'=>['zip'=>$zipcode,'pri_spec'=>$sspec']]);
but that does not work? What is the correct way of searching??
After removing the last single quoted '. Your code should work fine. An easier to read version may look like :
$searchByAttr['HealthSearch'] = [
'zip' => $zipcode,
'pri_spec' => $sspec
];
$dataProvider = $searchModel->search($searchByAttr);
Also you need to check the HealthSearch class which is the first responsible of making that search. Gii generates a primary boilerplate from your model that need to be adapted to your app in further steps. By default the HealthSearch::search() method should filter by all model's safe attributes and as any ActiveRecord class it has also a rules() method that returns those safe attributes. So if zip and pri_spec are not included in that array they will be simply ignored.
I need to use article name on the url on Yii2 which is like http://example.com/article?id=1, just replace id=1 to article_name, like http://example.com/article/article_name or is it.
This is my controller code-
$model = Articles::find()->orderBy(['id' => SORT_DESC])->one();
View-
<?= Html::a('<b>Read more ...</b>', ['article-details','id' => $model->id], ['target'=>'_blank']) ?>
Thanks advance
You may use yii2 sluggable behavior Refer this.
There are few steps.
You must add the following urlManager rule :
'article/<slug>' => 'article/view',
You should build url in your view files like this :
\yii\helpers\Url::to(['article/view', 'slug'=>$model->title])
or
\yii\helpers\Url::to(['article/'.$model->title]);
And in your action
public function actionArticle($slug){
$model = Articles::find()->where(["title"=>$slug])->orderBy(['id' => SORT_DESC])->one();
//and other code
}
Also your article titles must be valid for url.You can do it easly by trimming whitespaces and so on But also you can add url some identification propery. or add column stored unique slugs for every article
You have two way
one change the related action in the controller changing id with name in the related function declaration
or declare a new action with the name as parameter
public function findModelName($article_name)
{
$model = Article::findOne(['name'=> $article_name]);
........ your related code
}
you can find the model by name this way
Article::findOne(['name'=> $article_name]);
I am using Symfony 1.3.2 on Ubuntu 9.10
I want to set the default value of a textarea widget, with data read froma adb.
My code snippet in the template looks likes this:
<?php $form['notes']->render(); ?>
The API docs dont show how to do this - does anyone know how to do this?
You can use this in your action or the form class:
$this->form = new yourForm(); // If its in your action
$text = // data for prepopulating field, from db or anywhere
$this->form->setDefault('notes', $text);
... or if you've got multiple fields:
$this->form->setDefaults(array('notes' => $text, 'more_notes' => $more_text));
Or if you prefer declaring it just once with the widget in your form class configuration (I think this is right):
$this->setWidgets(array(
// widgets
'notes' => new sfWidgetFormTextArea(array('default' => $text)),
// widgets
));