CodeIgniter - form_dropdown defaults to multiple - codeigniter

Im having a little trouble with my form_dropdown in codeigniter, basically it ALWAYS adds the multiple="multiple" and i dont know how to get rid of it.
Here is the code im using to generate the dropdown
$js = 'class="users"';
echo form_dropdown('users', $users, set_value('users', $users), $js);
Is there anything i can add so that it doesnt automatically create it with the multiple option

I think the issue is related to your third option set_value('users',$users)
Since $users is probably an array, the set_value may be setting multiple options to selected and in such a case form_dropdown would generate the multiple property.
Try passing a single user value and make sure that works as you expect.
Also, check the out put from the set_value function to see if it returns an array instead of a single value.

You don't need to use set_value here. Just use the value you want selected.
$js = 'class="users"';
$user = 1;
echo form_dropdown('users', $users, $user, $js);

Related

Correct way to handle Requests in Laravel

I'm thinking about the right way to handle my requests.
At the moment I call a function and put the request input directly into the db query. Like this...
$groupid = Group::where('name', $request->input('groupname'))->select('id')->first();
Is it correct to do it like this? Or is it maybe better to define a variable (Is $groupname actually I variable?) before. Like this...
$groupname = $request->input('groupname');
$groupid = Group::where('name', $groupname)->select('id')->first();
Or does it not matter?
According to me, your first option is correct.
in second option you are declaring a new variable and increasing your memory stack, which is not needed.
So you should get the value form the variable which is already created.
Moreover instead of getting input value from input function you can directly access $request property as $groupid = Group::where('name', $request->groupname)->select('id')->first();
This is my opion, there may be another option to do.

Laravel 5.4 : Dynamic validation rule

I kind of struggle to find the answer to my question, and my test don't prove to be useful. So maybe someone here would have hit the same issue that I'm facing.
I have inputs with the following kind of patterned name projects-0-1, project-0-2, project-1-0 and so on... These are file inputs so people can upload a document/an image.
So basically, I've been trying to get a validation message that would (ideally) be something like that:
$validator->getMessageBag()->add('project-*-*', 'File is empty!');
OR
$validator->getMessageBag()->add('project-*', 'File is empty!');
I tried a couple of things already and nothing seems to work.
The reason I get to add a custom message is that file is simply not validated if it comes empty to the $request object. So I first need to check if the $request->hasFile and in case it doesn't I want to add the error message.
Things to consider:
inputs can be dynamically added to the form, so I don't know the exact number of file inputs I need to validate beforehand.
even if this should not impact the code and validation, it's worth noticing that everything happens through ajax as I embed the form on another website. Therefore I created endpoints etc...
Any hint ?
Right, coming back here in case someone faces that issue too. I found a "hacky" way to get there and it does the trick for me.
As each input file is dynamically added to the DOM, I add an extra hidden input that holds the name of the file input as a value.
Then in my controller I do smth like that:
public function createValuesKeyArray ($preset)
{
$regexPattern = '/^'. $preset .'-[0-9]*$/';
$customPresets = preg_grep($regexPattern, array_keys(Input::all()));
$keys = [];
foreach ($customPresets as $customPreset) {
array_push($keys, $customPreset);
}
return $keys;
}
// This allows me to get all hidden input names in an array in order to get its value from the $request
$hiddenInputs = $this->createValuesKeyArray('hidden-project-name');
Once I get this array, I can do stuff like that and dinamycally add my set of rules for the input files present in the DOM:
foreach($hiddenInputs as $hiddenInput){
$globalRules[$request[$hiddenInput]] = 'required';
}
Not sure if this the right way to get there, but it does the job for me and I don't find that code horrible. I'll stick with it until I find a better way.

Yii : form values retention for CHtml::checkBoxList on form validaton

I am using CHtml::checkBoxList for my form. For some reason I cannot use CHtml:activeCheckBoxList or CActiveForm::checkBoxList. Everything works fine only problem is that I loose checkbox values on form validation error. What could be the easiest way to fix this ?
If you're making a form, you probably want to use CActiveForm's checkboxlist, which is a form-specific wrapper of CHtml::activeCheckBoxList) instead. Something like
echo $form->checkBoxList(
$model,
'condiments',
array(
'ketchup'=>'Ketchup',
'mustard'=>'Mustard',
'relish'=>'Relish',
'onions'=>'Onions'
)
);
should give you a persistent check box list of hot dog condiments, for example.

adding attributes to form_open_multipart

I was wondering how could we add attributes to file_upload in codeigniter
right now I am using form_open_multipart('controller');
but I want to add an id attribute to it.
Any help appreciated thanks!
From the User Guide:
form_open_multipart()
This function is absolutely identical
to the form_open() tag above except
that it adds a multipart attribute,
which is necessary if you would like
to use the form to upload files with.
So, you would use it in the same way you use form_open(). Example:
form_open_multipart('controller', 'id="my_id"');
Or:
form_open_multipart('controller', array('id' => 'my_id'));

Magento: how do I access custom variables in PHP?

I am aware of 'Custom Variables' and how they can be used with {{ }} brackets in email templates as well as in static blocks.
However, I want to use them in template code i.e. view.phtml.
I want to be able to access 'variable plain value' to retrieve a conversion value, i.e. a number/string as number for a given 'variable code'.
Been doing this for some time to create various messages that are editable through the admin interface so I don't have to go code digging when the flavor of the moment changes.
To access the plain value of the custom variable with code custom_variable_code use this:
Mage::getModel('core/variable')->loadByCode('custom_variable_code')->getValue('plain');
NOTE: Single store doesn't show the store select dropdown for the variable scope. This answer is not technically correct, in order to future-proof yourself in case of having multiple stores --> Please see #Mark van der Sanden answer below and give him an upvote.
Unfortunately, all other answers are not 100% correct. Use it like this (note the setStoreId() to get the value for the correct store view):
$value = Mage::getModel('core/variable')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCode('variable_code')
->getValue('text');
Or to get the html value:
$value = Mage::getModel('core/variable')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCode('variable_code')
->getValue('html');
If no html value is defined, getValue() returns the text value if you request the html value.
Stackoverflow almost to the rescue again. Thought this would be it:
Setting a global variable in Magento, the GUI way?
But it wasn't, this was:
$angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value');
The only way I see you can acheive this by having a method in the templates block, that will output the needed result.
For instance say in the template view.phtml you have the following code:
<div id="title_container">
<h2><?= $this->getTitle(); ?></h2>
</div>
The function can represent your variable code and any logic that has to do with what gets displayed in the title should be placed in the block.
Just for clarification sake the block is the variable $this
If you are unsure what is the actual class name of your block you can do something like:
Mage::log(get_class($this));
in the var/log/system.log you will print the class of the block of that template.
That is the best way.
HTH :)
// To get the TEXT value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('text');
// To get the HTML value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('html');
// The store id is set as Custom Variables can be edited for multiple stores
Note: A custom variable might have different values for different stores.
So to access store specific value for the custom variable with the code custom_variable_code
Use this:
$storeId = Mage::app()->getStore()->getId();
$custom_variable_text = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('text');
$custom_variable_plain_value = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('plain');
$custom_variable_html_value = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('html');

Resources