Undefined class constant 'FILTER_POS_FOOTER' - yii2

When using gridview tried to add property filterPosition
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'filterPosition'=>self::FILTER_POS_FOOTER,
..........
?>
But it shows as Undefined class constant 'FILTER_POS_FOOTER'

self is used to access the current class itself. In here, you're using view file which usually does not contain any classes.
Defined constant from class (GridView in this case) can be used by writing class's name and then it's constant. In here:
'filterPosition' => GridView::FILTER_POS_FOOTER,

Related

Yii2 ActiveForm TextField Number Max

I have fields like below
<?= $form->field($model, 'phone')
->textInput(['type' => 'number', 'maxlength' => 13])
->label('Phone')
?>
why the 'maxlength' is not work? and how to make it work?
thank you before
It will not work because you are using type=>number for your input field, you have to change it to type=>text.
<?= $form->field($model, 'phone')
->textInput(['type' => 'text', 'maxlength' => 13])
->label('Phone')
?>
Looking at your input it seems like you are doing it because you do not want the user to enter any other thing than numbers for the Phone field, Yii2 provides you a very nice way to accomplish this i.e yii\widgets\MaskedInput, you can format your input using the mask option to tell it how many digits to allow and in which sequence see the demos HERE
<?= $form->field($model, 'phone')->widget(\yii\widgets\MaskedInput::className(), [
'mask' => '999-999-9999',
]) ?>
apart from the solutions above, you can also have the option of validating this inside your model by using the custom validation option.
Your rule for phone inside your model should look like
[['phone'],'PhoneLimit']
public function PhoneLimit($attribute)
{
if (!preg_match('/^[0-9]{13}$/', $this->$attribute)) {
$this->addError($attribute, 'Please provide digits only no more than 13.');
}
}
try for type number -> 'type'=>'number', 'min' => 1, 'max' => 999

Yii2: Using TinyMCE into Kartik's DetailView with custom settings

I'd like to insert 2amigos' TinyMCE widget in Kartik's DetailView edit mode. This is what I got by now:
[
'attribute' => 'myAttribute',
'format' => 'raw',
'type' => 'widget',
'widgetOptions' => ['class' => TinyMce::classname()],
'value' => $model->myAttribute,
],
With this chunk I managed to show TinyMCE editor with default settings. What I'm trying to do now is to show it with custom settings defined by:
Yii::$app->params['myTinyMceParams']
In form I'm doing this:
<?= $form->field($model, 'myAttribute')->widget(TinyMce::className(), Yii::$app->params['myTinyMceParams']) ?>
Any ideas?
I finally found a solution, maybe not ideal but fully operative: to merge both 'class' array and rest-of-options array into 'widgetOptions':
'widgetOptions' => ArrayHelper::merge(['class' => TinyMce::classname()], Yii::$app->params['tinyMceParams']),

How do I add Kartik ActiveField on Kartik DetailView?

I'm using Kartik's awesome extension called DetailView. I'd like to add the Kartik widget called ActiveField to the DetailView. What is the proper way to use the ActiveField widget in the DetailView widget?
The code I'm using generates the error First parameter must either be an object or the name of an existing class. According to Kartik's documentation, any widget (not just the DetailView widgets) can be used. What is the proper way to do that?
$attributes = [
['attribute' => 'definition_summary',
'format' => 'raw',
'type'=>DetailView::INPUT_WIDGET,
'widgetOptions'=>[
'class'=>ActiveField::className(),
'addon' => ['prepend' => ['content' => '#']],
],
];
echo DetailView::widget([
'model' => $model,
'attributes' => $attributes,
]);

yii2 join model as dataProvider

I've joined 2 tables like following:
$model = SalesEntry::find()
->joinWith('salesItems')
->all();
then in view used DataProvider like following:
GridView::widget([
'dataProvider' => $model,
'columns' => [
'date', // sample field from first table to see if ok
],
]);
and I’ve got following error:
Call to a member function getCount() on a non-object
What am I doing wrong here?
That is because an ActiveQuery instance is not a DataProvider, which the widget expects. You need to wrap it in an ActiveDataProvider for it to work:
GridView::widget([
'dataProvider' => new \yii\data\ActiveDataProvider(['query' => $model]),
' columns' => [
'date', // sample field from first table to see if ok
],
]);
DataProvider in GridView should be an instance of yii\data\DataProviderInterface
See docs.

Getting value from url for filter field in GridView

In my grid view I have added my own filter. But as you can see I had to use "value" to get the value for that input field from url. Other filter fields doesn't require anything, they pick up value automatically, but custom filter field doesn't. I type something in, it accepts and search and after that, field is empty.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'tableOptions'=>['class'=>'table table-striped table-hover table-bordered responsive',],
'columns' => [
[
'attribute'=>'date_created',
'filter'=>Html::activeTextInput($BreederResultsSearch, 'date_created', ['class'=>'js-datepicker', 'value'=>isset($_GET["BreederResultsSearch"])?$_GET["BreederResultsSearch"]["date_created"]:NULL]),
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
In your filter you can add:
$this->date_created=Yii::$app->getRequest()->getQueryParam('date_created',NULL);
Your problem could be, that you don't have a public attribute in your SearchModel that gets properly validated by rules. If the attribute is not in the rules (for your given scenario) it doesn't take the value but sets the field/attribute to be null.
This part of the docu explains how filtering with SearchModels and attributes works. http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#filtering-data
Edit: Also to mention is maybe, that the filters don't get their values from the url, but from the according attributes of the model that is transfered to the view (filterModel of your gridview)
Edit:
Is the searchModel the same model you use for the activeInputField in the filter? It should! or did you just rename it here?