sfWidgetFormDoctrineChoice adding extra choice field - forms

I'm using sfWidgetFormDoctrineChoice and I'm reading options from table. Is it easy way to add one option and place as first which is not in this table?
By option I mean html option:
<select>
<option val="new"></option>
</select>

You could always localize the choices, then prepend the resulting array with the value you would like to use:
$choice = new sfWidgetFormDoctrineChoice(array('model' => 'MODEL', 'order_by' => array('name', 'asc')));
$choices = $choice->getChoices();
array_unshift($choices, array('key' => 'My Custom Value'));
$this->widgetSchema['widget_name'] = new sfWidgetFormChoice(array('choices' => $choices));
$this->validatorSchema['widget_name'] = new sfValidatorChoice(array('choices' => array_keys($choices));

Related

Retrieving values of Form::select

I have a select box with an array of data to populate it like this:
{!! Form::select('language',$languageArray,'null', ['id'=>'language','multiple',]) !!}
I am passing $languageArray with view , and is simply an array of values like ['A','B','C']...
Now while fetching the selected values i am getting numeric value of selected options. Is there a way to change the values from numeric indexes to Text written in option. i did it using an associative array as second argument like this:
['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..']
But it creates a long list and view seems to be overloaded with data is there a better way for achieving below output ???
<select name="language">
<option value="english" ">English</option>
<option value="french">French</option>
....
I suggest you to use Config values ,
Create a file like languages.php in config folder.
<?php
return ['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..'
'german' => Lang::get('languages.german')
];
View :
{!! Form::select('language',Config::get('languages'),'null', ['id'=>'language','multiple',]) !!}
As you can see, in this way you can use config values in another view too and support multi language(its important too.. look at 'german')
Last Option is creating your own Form macro for example,
Form::macro('slugiySelect', function($name , $list , $selected = null , $options = [])
{
$options = [];
foreach($list as $row)
$options[Str::slug($row)] = $row;
return Form::select($name , $options , $selected , $options);
});
In this macro your options array is slugify with Laravel string function and uses as a key , I kept value as yours. You can do it with your way.

add catalog product in wishlist programmatically in magento

I have created a product with custom option and I have showed the detail of this product on a custom page. Now I want to add the product in wishlist with filled custom option by user.
If i have to just add the product in wishlist, I can simply use the following code.
<a href="'.Mage::helper("wishlist")->getAddUrl($_product).'" class="link-cart">Add to Wishlist /a>
but i want to insert the product with custom option. For this i have use following code but it gives me error "Cannot specify wishlist"
$wishlist = Mage::getModel('wishlist/wishlist');
$storeId = Mage::app()->getStore()->getId();
$model = Mage::getModel('catalog/product');
$_product = $model->load($data['productId']);
$params = array(
'product' => $data['productId'],
'qty' => 1,
'store_id' => $storeId,
'options' => array(
'optionId' => 'option value',
'optionId2' => 'option value2',
)
);
$request = new Varien_Object();
$request->setData($params);
$result = $wishlist->addNewItem($_product, $request);
You have to change first line
$wishlist=Mage::getModel('wishlist/wishlist')
to
$wishlist = Mage::helper('wishlist')->getWishlist();

populate id from database in select box option value in zend framework

hi I am new in zend framework Basically I want to populate company name list from the database. Actually I have doen it but i want to also populate its id in option box
example
select
option value='1'> tcs option
select
this is my code
Application_Form_Clientcompanyform extends Zend_Form
$company_list = new Application_Model_Clientcompany;
$showlist = $company_list->companyNameList();
$list=array();
$id=array();
foreach($showlist as $key => $value)
{
$list[]=$value['companyName'];
$id[]=$value['id'];
}
$this->addElement('select', 'companyName', array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:103px'),
'multiOptions' => $list,
'decorators'=>Array(
'ViewHelper','Errors'
but Now i want to set the value in option in select box width $id from database
$companyName = new Zend_Form_Element_Select('companyName');
$companyName->setRequired(true);
$companyName->addFilter('StringTrim');
$company_list = new Application_Model_Clientcompany;
$showlist = $company_list->companyNameList();
//add selections to multioption, assumes object...change notation if using array
foreach($showlist as $company) {
$name = ucfirst($company->name);
$companyName->addMultiOption($company->id, $name);
}
$this->addElement($companyName);
I know I changed the syntax style, I just find it easier to keep things straight this way.
Everything else you may need is in http://framework.zend.com/manual/1.12/en/zend.form.html, get used to using the reference and the the api for the framework, they really help.

CakePHP - input select not taking select options from a variable

I am developing an application with CakePHP 2.3.2 and I am having some trouble with an input select on a form. I am creating an array, in my Controller, which contains a list of states. In my View I find that when I use this variable in the 'options' field of the input I do not get any select options. If I do a print_r on the variable, in the view, I see exactly what I think I should be seeing for the 'options' field. I have even tried copying this print_r output and putting it in the 'options' field and then the input select works fine.
Here is what I have
In Controller
$options = 'array(1 => \'NSW\',2 => \'ACT\',3 => \'NT\');
$this->set('all_states, $options);
In View
<?php
$options = $all_states;
echo $this->Form->create('Refine', array('url => '/ServiceDirectoryResults/view/refine'));
echo $this->Form->input('field' ,array(
'type' => 'select',
'label' => false,
'options' => $options
));
echo $this->Form->end('Refine Search');
?>
When I run this I see a select with no select options
If I add print_r($options) after the echo $this->Form->end('Refine Search'); I see
array(1 => 'NSW',2 => 'ACT,3 => 'NT')
Which is what I would expect as it is the content of the $options variable which was the $all_states variable passed from the controller. If I take this output from the print_r and replace the $option with it in the input select the select drop down works fine and I see the three options. For some reason I can't work out the select is working fine if I hard code the select options but it will not work if I pass a variable containing the array to the input select.
I would really appreciate if if someone could give me a clue what I am doing wrong here.
Kind Regards
Richard
you might try it like below:
echo $this->Form->input('field', array('type'=>'select','label' => false,
'options' => $options,'default'=>'2'));
to the following HTML being generated:
<option value="2" selected="selected">ACT</option>
option two is shown instead any other one.
Likely issue:
Arrays should not be made as strings like you have:
$options = 'array(1 => \'NSW\',2 => \'ACT\',3 => \'NT\');
Instead, just make an array:
$options = array(1 => 'NSW', 2 => 'ACT', 3 => 'NT');
Other notes:
Why are you setting $options to $all_states only to set it back?
Missing quotes all over - make sure if you start quotes, that you also end them
not good practice to hard-code your URLs (like in your Form->create)

Add Database Row Button Cakephp

The Controller:
function add(){
if (!empty($this->data)) {
$qnote = $this->Qnote->save($this->data);
if (!empty($qnote)) {
$this->data['Step']['qnote_id'] = $this->Qnote->id;
$this->Qnote->Step->save($this->data);
}
$this->Session->setFlash('Your note has been saved.');
$this->redirect(array('action' => 'index'));
}
}
The Form.
<?php
$userID = Authsome::get('id');
echo $form->create('Qnote', array('action'=>'add'));
echo $form->input('Qnote.id', array('type' => 'hidden'));
echo $form->input('Qnote.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Qnote.subject');
echo $form->input('Qnote.body', array('rows' => '3'));
echo $form->input('Step.id', array('type' => 'hidden'));
echo $form->input('Step.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Step.body', array('rows' => '3'));
echo $form->end('Save Notes');
?>
This Form Adds Data in 2 Models.
Model 1 = Qnote;
Model 2 = Step;
I am able to add Data to the Models.
I was wondering I could add a button to the form
The Button would allow users to add multiple Step.data to the Step model.
Some like a +1 Button.
Basically I want to add multiple steps Per Qnote.
Could someone point me in the right direction how i can achieve this.
This is something I would do with jQuery. Basically all you need to do is using jQuery to dynamically add more inputs in the CakePHPs conventions: Step.0.user_id for example.
What you need to do now on a +1: you need to count the zero up, so you will get Step.1.user_id and so on.
First option: Use a jQuery-Script for doing this
var count = 1;
$('#add_step').click(function() {
var new_form = $('.Step').eq(0).clone();
$('input, textarea, select, radio', new_form).filter('[name^="data"]').each(function() {
var name = $(this).attr('name');
var new_name = name.replace(/\[\d*\]/, '['+count+']');
$(this).attr('name', new_name).attr('value', '');
});
$('#YourForm').after(new_form);
count+;
return false;
});
In this case you're cloning a div with the class step which holds your inputs for the model Step. You then replace the name-attribute to replace the zeros through the new value of the variable count. count++ enables you to add as many steps as you want.
This is an jQuery only solution and may require additional work for your environment.
Second option: Use AJAX with an element
You could also write a function in your StepsController which renders an element which holds your form and takes care of the counter.
Third option: Use a URL-parameter to decide how many options you want
If you have an URL like /qnote/add/3 you could use the 3 as a parameter in a for-loop to iterate through these form-inputs.
You need to take care, that eventually already typed in values are sent with the form when adding another Step so that these don't get lost.
Hope this helps to get on the right way.