yii2 detailview conditional row class - class

I would like to change class for one single attribute in detailview, based on a condition:
If I wouldn't want to make it conditional, it would be working like so:
[
'attribute' => 'ungueltig',
'format' => 'boolean',
'contentOptions' => [
'class' => 'danger',
]
],
I want this one to change to conditional, and I have tried a lot of different ways, e.g.:
[
'attribute' => 'ungueltig',
'format' => 'boolean',
'contentOptions' => function ($model) {
if ($model->ungueltig == 1) {
return ['class' => 'danger'];
} else {
return '';
}
},
],
(I would think this is the most logical solution, but nothing happens, so page is loading fine but without class danger at the attribute, no error message)
or
[
'attribute' => 'ungueltig',
'format' => 'boolean',
'contentOptions' => ['class' => function ($model) {
if ($model->ungueltig == 1) {
return 'danger';
} else {
return '';
}
},]
],
= error message: htmlspecialchars() expects parameter 1 to be string, object given
so I have no clue and I don't even find any help on the web. Can you please point me to the right direction? Many thanks!

You should simply try :
'contentOptions' => [
'class' => ($model->ungueltig == 1) ? 'danger' : '',
],
DetailView display only one model, you don't need any function here.

Related

Yii2 multiple models in one form js validating

I have two fileds, that are uses two different instances of the same model class.
Test Case Video
$form->field($billing_address, 'zip',
[
'selectors' => [
'input' => '#billing-zip',
'container' => '#billing-container',
],
'options' => ['id' => 'billing-container'],
])->textInput(['maxlength' => 11,
'name'=> 'Billing_Address[zip]',
'id'=>'billing-zip']);
//Shipping
$form->field($shipping_address, 'zip',
[
'selectors' => [
'input' => '#shipping-zip',
'container' => '#shipping-container',
],
'options' => ['id' => 'shipping-container'],
])->textInput(['maxlength' => 11,
'name'=> 'Shipping_Address[zip]',
'id'=>'shipping-zip']);
When I finish filling fields, errors are shown for only fields that has errors.
But when I push submit, if one of zip fields has errors, error appears for all zip fields
public function rules()
{
return [
[['zip'], 'string', 'max' => 23],
];
}
I think you can use something like this in your controller:
if( Model::loadMultiple($model_array, Yii::$app->request->post()) && Model::validateMultiple($model_array) && $model->validate()){
//your stuff
}
where $model_array are an array of models
$model_array['model1'] = new YourModelClass();
$model_array['model2'] = new YourModelClass();
Documentation

Yii2 advanced change views default path (theming)

I would like for my application to automatically change template
so i created this structure frontend/web/themes/myTheme
following http://www.yiiframework.com/doc-2.0/guide-output-theming.html i added this code in frontend/config/main.php
'components' => [
'view' => [
'theme' => [
'basePath' => '#app/themes/myTheme',
'baseUrl' => '#web/themes/myTheme',
'pathMap' => [
'#app/views' => '#app/themes/myTheme',
],
],
],
],
however i kept getting the error that " /var/www/html/myProject/app/frontend/views/site/index.php" The view file does not exist???
i also tried to put this function inside the controller based on How to change default view path in Yii2?
public function getViewPath()
{
return Yii::getAlias('#web/themes/myTheme/site');
}
so my question is:
1. how can I change the views default path?
2. how can i do it automatically since i can not change the common/config/main.php settings during a session?
site controller
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['index'],
'allow' => true,
'roles' => ['#'],
],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* #return mixed
*/
public function actionIndex()
{
$searchModel = new ProductSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
I think you are configuring the wrong file.Don't configure themes in the common/config
Try this:
in frontend/config/main.php
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#frontend/views'=>'#frontend/themes/myTheme',
],
],
],
],
if you need to configure the backend then in the backend/config/main.php
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#backend/views'=>'#backend/themes/myTheme',
],
],
],
],
The common folder is has to contain the files that are required by both
frontend and backend.
Hope this helps.
First question:
I think than you have a common mistake in yii when used advanced app: the alias #app references root directory of frontend, backend, or common depending on where you access it from View documentation here.
You would used the solution proposed by ovicko.
Second question:
You can change the theme configuration dynamically in controller through view object:
$this->view->theme->pathMap =['#app/views' => '#app/themes/myTheme/',];
EDIT
According to Documentation:
Theming is a way to replace a set of views with another without the need of touching the original view rendering code.
What means that the original view file must exist and theming simply replace it in during rendering. So you must create a file in /var/www/html/myProject/app/frontend/views/site/index.php (a empty file is valid) in order to theming works.
This sounds quite ridiculous, I Know, but it works.
But I think that is much better and easier the use of differents layouts, again, to change dinamically the layout in your controller:
$this->layout = 'route/yourlayout';

fuction() not working in Detailview yii2

When i am going to show user details in Detailview than it throws:
htmlspecialchars() expects parameter 1 to be string, object given
Below is my code for Detailview:
view.php
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'userID',
'userEmail:email',
'userName',
'userMobile',
'userBirthDate',
'userGender',
[
'attribute' => 'interestName',
'format' => 'raw',
'label' => 'Interest',
'value' => $model->getUserinterest(),
],
'userStatus',
'userType',
],
]);
?>
function getUserinterest() {
foreach ($model->userinterest as $userinterest) {
$interestNames[] = $userinterest->interestName;
}
return implode("\n", $interestNames);
}
Since version 2.0.11 value can be defined as closure. Upgrade Yii version to developer version 2.0.11+ and it will work.
Follow the final answer as below:
view.php
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'userID',
'userEmail:email',
'userName',
'userMobile',
'userBirthDate',
'userGender',
[
'attribute' => 'interestName',
'format' => 'raw',
'label' => 'Interest',
'value' => $model->getviewinterest(),
],
'userStatus',
'userType',
],
]);
?>
Users.php(Model)
public function getviewinterest()
{
foreach ($this->userinterest as $userinterest)
{
$interestNames[] = $userinterest->interestName;
}
if(!empty($interestNames)){
return implode("<br/>", $interestNames);
}else{
return "(not set)";
}
}

What are the correct parameters for Magento Product Attribute creation in Perl

I've got several calls working already, but for the life of me I can't figure out how to make product_attribute.create work. I'm always getting a 102 Invalid request parameters or 623 Wrong Method Signature.
making the call like this my $res = $self->_useragent->call( call => $self->_session, #{$payload} ); (note: useragent is a XML::RPC object.
This Dumper $payload;
$VAR1 = [
'product_attribute.create',
[
'test',
{
'frontend_label' => [
{
'label' => 'Test ME',
'store_id' => 0
}
],
'scope' => 'store',
'frontend_input' => 'text'
}
]
];
I've read the API Documentation but figuring out what the call should look like in Perl is tricky.
I'm not familiar with the XML-RPC library you're using in perl, but he error you're seeing is a Magento API exception, configured in
<!--File: app/code/core/Mage/Catalog/etc/api.xml -->
<!-- ... -->
<invalid_parameters>
<code>102</code>
<message>Invalid request parameters.</message>
</invalid_parameters>
<!-- ... -->
Using the exception's name, you can find the place Magento threw it
#File: app/code/core/Mage/Catalog/Model/Product/Attribute/Api.php
//...
if (empty($data['attribute_code']) || !is_array($data['frontend_label'])) {
$this->_fault('invalid_parameters');
}
//...
So, my guess is your call is correct, you're just missing an attribute_code.
After some digging through Magento's code, I copied this from the test suite and it converted to perl, it appears to work. maybe all attributes are required.
$VAR1 = [
'product_attribute.create',
[
{
'default_value' => '1',
'is_configurable' => 0,
'used_in_product_listing' => 0,
'is_visible_on_front' => 0,
'apply_to' => [
'simple'
],
'is_comparable' => 0,
'is_used_for_promo_rules' => 0,
'is_required' => 0,
'scope' => 'store',
'is_unique' => 0,
'frontend_input' => 'text',
'is_searchable' => 0,
'attribute_code' => 'unique_code',
'is_visible_in_advanced_search' => 0,
'frontend_label' => [
{
'label' => 'some label',
'store_id' => '0'
}
]
}
]
];
Further experimentation somewhat based on Alan Storm's Answer, suggest that the following fields are required, as I was not able to successfully create a request without all of these fields at minimum being defined.
$VAR1 = [
'product_attribute.create',
[
{
'frontend_input' => 'text',
'attribute_code' => 'test1374438470',
'frontend_label' => [
{
'store_id' => 0,
'label' => 'Test ME'
}
]
}
]
];

Populating $form_input with 'select' element options?

I'm trying to understand the data-structure required to populate a
form with 'select' element values (options).
When I dump (Data::Dumper) the FormFu object, I see that the object structure
looks similar to the following:
'name' => 'EmailDL',
'_options' => [
{
'label_attributes' => {},
'value' => 'm',
'container_attributes' => {},
'label' => 'Male',
'attributes' => {}
},
{
'label_attributes' => {},
'value' => 'f',
'container_attributes' => {},
'label' => 'Female',
'attributes' => {}
}
],
Seeing this, I figured that the way to structure $form_input (being that $form_input = \%cgivars) would be something like the following:
'Firstname' => 'Faisal',
'EmailDL' => [
{
'value' => 'myvalue',
'label' => 'mylabel'
}
],
However this doesn't seem to work. I've found that structuring $form_input correctly, and then issuing a $fu->default_values($form_input) to be simple and effective, except in this instance when I'm trying to include the select/options sub-structure.
So the question is: How should I structure 'EmailDL' above to correctly populate 'select' options when doing $fu->default_values($form_input) or $fu->process($form_input)?
To set the options you use the options call,
$fu->get_all_element('EmailDL')->options([ [ 'myvalue', 'mylabel' ],
[ 'val2', 'label2' ] ]);
If you then want to set one of those values you can use the default_values.
$fu->default_values({ EmailDL => 'val2' });
Further help is available here in the Element::Group documentation. Note the code examples are in the text of the help.