yii2 formwizard checkbox field value set to 1 even if not selected - yii2

I am using yii2-formwizard and I want to insert a checkbox as form input field for the field is_legal in a tabular step. So in fieldConfig array, reading the documentation, I inserted the following code:
'is_legal' => [
'options' => [
'type' => 'checkbox',
'template' => '{input}{beginLabel}{labelTitle}{endLabel}{error}{hint}',
],
'labelOptions' => ['label' => \Yii::t('app', 'Legal Representative')],
],
If I select the checkbox or not the value of the field is always 1 as shown on: .
However, when I add another instance of the model, in the preview step I have NA as value of the legal representative field :

Yes, you are correct about it. It incorrectly shows the value even if the check box is not checked i have updated the section and added a fix.
//check if single checkbox input
if (inputType.attr("type") == 'checkbox') {
return inputType.is(":checked") ? inputType.val() : '';
}
To get the latest code you need to repeat the steps for running composer using,
composer update
and clear the browser cache along with clearing the assets folder in the web directory.
Normally when i am working locally with extensions or if there is an update for an extension which includes javascript updates i add the following settings under the components array in my local config file that takes care of getting the latest files from, and the assets are force copied every time to the web/assets/ directory whenever you refresh the browser or the page reloads.
'components'=>[
'assetManager' => [
'forceCopy' => true,
],
]
Note: Dont leave it open on live site as it would make the page load slower.

Related

yii2 formwizard checkbox field and customize preview step

I am using yii2-formwizard and I want to insert a checkbox as form input field for the field is_legal in a tabular step.
So in fieldConfig array, reading the documentation, I inserted the following code:
'is_legal' => [
'options' => [
'type' => 'checkbox',
],
'labelOptions' => [
'label' => \Yii::t('app', 'Legal Representative'),
],
],
The following image shows the result:
However, when I go the preview step I see the field of the checkbox set as undefined:
In fact, when I try to save the model, the is_legal field is not set.
First Question: where is the problem with the checkbox form field?
Second Question: is there any way to customize the preview step? For example instead of 'Step 5', I would like to write 'Legal Data'.
i created this widget and there are a few things you need to know.
First Answer
The undefined it is showing is not the value but the label of the checkbox, if you look into the getLabel() function in the formwizard.js file it looks into the siblings of the input field for a label and gets its text
let text = $('#' + fieldName).siblings('label').text();
to display on the preview step, and by default Yii2 wraps the input inside the label like
<label><input stype="checkbox"></label>
so you need to use the template option of the checkbox like below
'is_legal' => [
'options' => [
'type' => 'checkbox',
'template' => '{input}{beginLabel}{labelTitle}{endLabel}{error}{hint}',
],
'labelOptions' => [
'label' => \Yii::t('app', 'Legal Representative'),
],
],
Second Answer
No, currently the widget does not support the custom title for the Preview step sections, but i think I can add the support for providing the title of the headings as it makes sense too, if that sorts your problem.
Update
Ignore the Second Answer given above i just pushed the updates to the live branch you can now use the previewHeading option in the step settings. Update your composer by running composer update to update to the latest version and clear cache using CTRL+F5.
See the following sample code how to use previewHeading option
use buttflattery\formwizard\FormWizard;
echo FormWizard::widget([
'enablePreview'=>true,
'steps'=>[
[
'model'=>$user,
'title'=>'My Shoots',
'previewHeading'=>'My Heading Step 1',
'description'=>'Add your shoots',
'formInfoText'=>'Fill all fields'
],
[
'model'=> $profile,
'title'=>'My Shoots',
'previewHeading'=>'My Heading Step 2',
'description'=>'Add your shoots',
'formInfoText'=>'Fill all fields'
],
]
]);

Magento 1.9 add custom column to associated products grid

We have an existing customization that appears to have broken when we upgraded from 1.7 to 1.9 community.
The customization adds a column to the associated products grid.
The customization is a local override of
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Group.php
This was done before I started on the project
$this->addColumn('breakdown_part_no', array(
'header' => Mage::helper('catalog')->__('Part No'),
'name' => 'breakdown_part_no',
'type' => 'varchar',
'index' => 'breakdown_part_no',
'width' => '120px',
'editable' => true,
));
This was added to _prepareColumns()
Another customization was added to method getSelectedGroupedProducts()
public function getSelectedGroupedProducts()
{
$associatedProducts = Mage::registry('current_product')->getTypeInstance(true)
->getAssociatedProducts(Mage::registry('current_product'));
$products = array();
foreach ($associatedProducts as $product) {
$products[$product->getId()] = array(
'qty' => $product->getQty(),
'position' => $product->getPosition(),
'breakdown_part_no' => $product->getBreakdownPartNo(),
);
}
return $products;
}
The behavior is that the column appears in the admin and can be edited, however when saved, it does not save any value.
If I modify the getSelectedGroupedProducts part and set a hard coded value, it displays still no value (blank field), but interestingly if I click save with no value, it saves the value that was hard coded. If I enter any value in the field, it saves as a blank. This is really strange behavior that makes no sense to me.
If I change one of the other fields, such as position to be a hard coded value, it appears instantly and works as expected. Please let me know the proper way for this to work.
There are several posts on various forums about how to do the above and the modification mentioned is true, but what all of the other posts left out was the adminhtml layout input. When a user edits product data in Magento Admin (Associated Products), the data is serialized and sent to the controller save action. I noticed that the fields were not present when a value was entered. This is because the value wasn't in the layout so it was being stripped off of the request before it was posted to the controller.
Add input field in adminhtml/default/default/layout/catalog.xml
adminhtml_catalog_product_supergroup
addColumnInputName

Dynamic Pretty URL's in Yii2 using get Parameters

I am trying to generate dynamic URL's using yii2 routing, but I didnt find any proper example for what I am looking for
I have a page which has list of users. If I click on any user name it gets redirected to particular user's profile page.
The URL for profile page is like
https://www.example.com/frontend/web/users/profile?id=1&name=xyz
I want to show this URL as https://www.example.com/xyz where xyz is users name.
I have seen examples of pretty URL's but couldnt find any specific example.
How to such dynamic URL's. Please help.
You cannot fully remove static link from link. You can leave profile and remove other. Then your link will be https://www.example.com/profile/xyz
First of all add to config:
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'profile/<name>' => 'users/profile',
...
],
Change your action:
public function actionProfile($name) {
$user= User::findOne(['name'=>$name]);

Yii2 get access only using pretty URLs

I'm using URL manager like the following:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'verses/view/<id:\d+>' => 'verses/view',
],
],
It works fine to make access using mysite.com/verses/view/158. The problem is, it is still possible to access the same content using non pretty URL i.e using plain get parameter such as mysite.com/verses/view?id=158. I need any way to restrict the access using the pretty URL.
I have tried the following couples of rules separately, but nothing I have gotten:
'verses/view<?id=>' => 'Error404',
'verses/view?id=<\d+>' => 'Error404',
What is the point of such restriction?
Anyway, one way to do it is something like this:
public function actionView($id)
{
if (strpos(\Yii::$app->request->getUrl(), '?') !== false) {
throw new \yii\web\BadRequestHttpException;
}
// ... the rest of action
}
No changes in UrlManager needed.
Try using UrlManager parameter enableStrictParsing = true.
What happens. UrlManager checks all rulls and they all do not match the request. Thus, by default it checks all default rules. Among default rules it finds the rule with ?id= and preforms routing to that one.
So, in order to avoid that route, you need to list all possible routes in the UrlManger rules and make enableStrictParsing = true. The routes not listed in the config rules parameter will be ignored.

How to use Url::remember in yii2

I want to create a link on my error page to take user back to the previous link.
Suppose the current URL is http://example.com/site/product, and a user try to view
http://example.com/site/product?id=100 and a product with id =100 does not exit, the system should throw 404 error to the error page, now if i want to create a link to take the user back to http://example.com/site/product the previous URl how do I make this work. i can make this work by hardcoding this in my error views file, but i want it dynamically as i have many controller an action using the same view file.
I try this in my site conteoller
controller/site
public function actions()
{
$url = Url::remember();
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
$this->render('error',['url'=>$url]),
];
}
and try to get the value the in error view file like this
/views/site/error.php
<p>
<?= Html::a('go back', [$url)?>
</p>
but it has no vaule..
please any good idea on how to make this work, am also open to new solution
this is form Yii2 Guide http://www.yiiframework.com/doc-2.0/guide-helper-url.html#remember-urls
There are cases when you need to remember URL and afterwards use it
during processing of the one of sequential requests. It can be
achieved in the following way:
// Remember current URL Url::remember();
// Remember URL specified. See Url::to() for argument format.
Url::remember(['product/view', 'id' => 42]);
// Remember URL specified with a name given
Url::remember(['product/view', 'id' => 42], 'product');
In the next
request we can get URL remembered in the following way:
$url = Url::previous();
// or
$productUrl = Url::previous('product');