Yii2. Language picker widget not working properly - ajax

I use \lajax\languagepicker in Yii2.
Languagepicker dropdown located in header nav inside <li> element.
$menuItems [] = '<li>'.LanguagePicker::widget(['skin' => LanguagePicker::SKIN_DROPDOWN, 'size' => LanguagePicker::SIZE_LARGE,
'itemTemplate' => '<li><i class="{language}"></i></li>',
'activeItemTemplate' => '<i class="{language}"></i>',
]).'</li>';
Language of website is changing only when I add below code to the view being rendered:
use \lajax\languagepicker\widgets\LanguagePicker;
LanguagePicker::widget();
If above code is not added Language does not change.
I suspect it can be related to ajax loading.
What could be a problem?

Check your DB in user table column language
Add it if not exist.
After that LanguagePicker start working for me.

Related

Laravel 5 routing within blade

up until this point I have essentially been using resource routing. One of my routes is for projects. If I create a project and then SHOW it, I see a URL in the form of
myUrl/projects/1
On the show page for a project, I want to be able to add a document. I have set up the relationships so a project can have one document and a document belongs to a project. I then set up the following route to handle the saving of the documents data
Route::post('projects/{id}/docOne', 'DocOneController#store');
So I add an a form in projects/show.blade.php, which opens like so
{!!
Form::model(new App\DocOne, [
'class'=>'form-horizontal',
'route' => ['docOne.store']
])
!!}
I then have my form fields and a save data button. Because of this new form within my projects show page, when I now show a project, it complains that the route for this new form is not defined.
How can I get this route to work within the projects show page?
Thanks
First of all you need to define a route name to your route, if you want to call it by his name.
So your route would be like:
Route::post('projects/{id}/docOne', [ //you need an array to set a route name
'as' => 'docOne.store', //here define the route name
'uses' => 'DocOneController#store' //here the callback
]);
Second you need to change your laravel form to use your route name and set the id
{!! Form::model(new App\DocOne, [
'route' => ['docOne.store', $project], //if you have setted the id variable like $id blade it gonna retturn it automatically only by passing the object, else, you can set $project->id
'method' => 'POST']
) !!}
EDIT:
You can't get an instance of a model on your view.
So the part:
{!! Form::model(new App\DocOne,
gonna fails every time you trye, also, the form:model needs an instance of a class that should have your vars filled with the info that the inputs should have (when you edit it).
You have two solutions:
If it's a new Doc and never before exist on your dataBase
I recomend to change your
Form::model
to:
Form::open
if it's a Doc thath already exist on your DB, like an edit, so in your controller you need to pass your existing Docas $docand remplace the: {!! Form::model(new App\DocOne, to:
{!! Form::model($doc,
and it works.
Form model was created to fill the input values with the data existing in your object instance, like when you edit someting.
So you need to have a correct instance.
Another think it's the MVC scope, a view shouldn't have acces to models, except if are passed by the controller.
Ok that's all.

Symfony2, dynamically refresh form choices

I am creating a simple Blog system with symfony2. Each blog Post is bound to a certain amount of Tags.
Tags can be selected with checkboxes when creating a new blog post. Now I want to be able to dynamically add new tag-checkboxes to the form.
The AJAX part is done and working, I can add new tag names to the Tag entity and append the new checkboxes to the form.
The problem is when I submit the form, symfony2 doesn't recognize the new added tags because they don't belong to the Tag entity yet (at the time the form was generated).
For example: after submitting the form, I dump:
$tags = $form->get('tags')->getData();
The controller ignores the tags that were added through ajax.
I know it has to be solved with events somehow, I already read this documentation: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html but can't figure out how to implement it for this specific case.
Here is my form builder for "PostType":
$builder
->add('title')
->add('content', 'textarea')
->add('tags', 'entity', array(
'class' => 'Bundle:Tag',
'property' => 'name',
'multiple' => true,
'expanded' => true,
))
->add('save', 'submit')
;
You can try to use this: Form Collection
Make sure you persist the newly added Tags before submit the form, and the checkboxes has the right names and values.
Names should be like "post[tags][]" and values should be the database ids of the Tag entities.

Laravel Blade Form, is it possible to create a textbox input if a user selects the 'other' option?

This may not be possible with blade, but I was wondering how to generate a textbox input from an 'other' option in a select dropdown. Is this possible?
{{ Form::select('showType', array(
'Theater' => 'Theater',
'Club' => 'Club',
'Festival' => 'Festival',
'Arena' => 'Arena',
'Closed Show' => 'Closed Show',
'College Show' => 'College Show'
'Other' => 'some kind of text input appears instead'
)}}
You'll need to use javascript to do that, Blade can't help much. But you can create, in Blade, your form input and set a hidden class to it and, when your user select the 'other' option you just have to remove that class.
You need to use JavaScript to do this because it happens on the client side. PHP and Laravel and Blade all happens on the server. An simple jQuery example:
$('select').on('change', function(){
$('body').append("<input type='text' value='whatever'/>");
});

Adding Grid Serializer of a custom module into a custom product tab

I am new to Magento. I have made a custom module named "Custom_Press" using module creator and I customized it little bit. Its working fine.
Following are the fields in the form of my module.
Press Title
Press Image
Press Date
Press Thumbnail
Status
I want to show all of its data in a grid serializer in a custom product tab. So that any press can be selected from the grid serializer against any product like the default magento functionality of upsell and cross-sells product's grid serializer.
you need to add new tab to Tabs block 'Namespace/Module/Block/Adminhtml/Form/Edit/Tabs.php':
$product_content = $this->getLayout()->createBlock('module/adminhtml_form_edit_tab_product', 'adminform_products.grid')->toHtml();
$serialize_block = $this->getLayout()->createBlock('adminhtml/widget_grid_serializer');
$serialize_block->initSerializerBlock('adminform_products.grid', 'getSelectedProducts', 'products', 'selected_products');
$serialize_block->addColumnInputName('position');
$product_content .= $serialize_block->toHtml();
$this->addTab('associated_products', array(
'label' => Mage::helper('module')->__('Products'),
'title' => Mage::helper('module')->__('Products'),
'content' => $product_content
));
The serializer is most important feature which allow you to define fields which will be editable in the grid, and define a name parameter which you should see in Save action of the form.
For more details please have a look at -
http://turnkeye.com/blog/magento-admin-form/

Magento - Can't delete mulitple select value in the product admin

I created a new attribute (multiple select) with some values, everything works fine but when I want to delete all the selected values for a product, I get the message "The product attribute has been saved." but the values are still selected.
Notes:
I press Ctrl + Click to unselect the last value before I save.
I set the parameter Value Required of my attribute to No
If I save a product without any value selected yet, then no values get selected
My Indexes are properly refreshed
See below two screens, on the left the parameters of my attribute and on the right my multiple select.
I'm running out of ideas so thanks for your help.
This is a known (annoying) behaviour of the Magento Adminhtml forms.
The problem is that if no value is selected for the multiselect, no value for that attribute is posted when the form is submitted.
On the server side Magento then loads the model, sets all the posted attribute values on the model and saves it.
Because no value was posted the original value that was loaded on the model wasn't updated.
As a solution for attributes with a custom source model I tend to provide an empty option with a special option value (e.g. -1). That value must not be 0 or an empty string.
Then I specify a backend model for that attribute that checks for that special value in the _beforeSave() method. If it is found the backend model unsets the attribute on the model instance.
Here is an example:
Source Model:
class Your_Module_Model_Entity_Attribute_Source_Example
extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
const EMPTY = '-1';
public function getAllOptions()
$options = array(
array('value' => 1, 'label' => 'One'),
array('value' => 2, 'label' => 'Two'),
array('value' => 3, 'label' => 'Three')
);
if ($this->getAttribute()->getFrontendInput() === 'multiselect')
{
array_unshift($options, array('value' => self::EMPTY, 'label' => ''));
}
return $options;
}
}
Backend Model:
class Your_Module_Model_Entity_Attribute_Backend_Example
extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
public function beforeSave($object)
{
$code = $this->getAttribute()->getAttributeCode();
$value = $object->getData($code);
if ($value == Your_Module_Model_Entity_Attribute_Source_Example::EMPTY)
{
$object->unsetData($code);
}
return parent::beforeSave($object);
}
}
If you find a better workaround please let me know.
There is a feature called <can_be_empty> you need to go to your system.xml and add this configuration into your file:
<can_be_empty>1</can_be_empty>
then inspect the element and remove the selected="selected" and hit save, now you can save the multi-select without any values.
Yes I found this a big pain in the bum too BUT it is an improvement on the previous bug which caused drop down attribute selections to be wiped if you tried to update attributes for several products at once.
Anyway, here is my what I do if I want to remove an option from products using a drop down attribute:
Go to Manage attributes
Click Manage Label Options
Add a temporary option to the list
Assign this new attribute option to all the products you want to
change
Delete the temporary attribute option
All solved.
Add a non existent option to html via chrome/firefox developer tool, select that option and save.
eg.
<option value="99999999">Click this to unselect option</option>
Just ran into this problem in Magento 1.7.0.2, my solution :
Use Firefox with Firebug
right-click the multiselect list, choose Inspect with Element and you'll see something like this at the bottom in Firebug :
XLarge
Double-click on selected, right-click, cut, no more selected attribute and just save the page.

Resources