CakePHP DIV option for Dropdowns - forms

It's easy to configure a Div using the form helper for standard input boxes. An example int he manual is...
echo $this->Form->input('User.name', array('div' => 'class_name'));
However, I can't achieve the same thing with dropdown menus?
Can anyone help out as to how to wrap a dropdown with a DIV using the form helper method?
thanks

I imagine you've been building your dropdowns with FormHelper::select, which doesn't include all the sugar of FormHelper::input, like automatic <div /> wrapping, magic error-messages, etc. You can get FormHelper::input to output a dropdown using the following.
$this->Form->input(
'User.country',
array(
'options'=>$arrayOfCountries,
'div'=>'class_name'
)
);
The options parameter indicates to FormHelper::input that you want a dropdown. You could achieve the same effect with the type parameter (ie. 'type'=>'select'), but the options parameter gives the same effect while also taking care of preparing the dropdown's options.

Related

symfony2 twig render form 'choice' as list

How can I (in symfony2 twig) render the form control 'choice' (expanded, checkboxes) as list ('li's, or just with a 'br' inbetwwen each option) and not have all options appear on one line?
I guess I could bach a jQuery together, but isn't there a more native way?
You can override the appropriate theme block. There is a great description in the docs how to do that.

drupal 7 form validate add class?

In my form I create a checkbox
$form['existing_customer'] = array(
'#type' => 'checkbox',
'#title' => t('Are you an existing customer?')
);
When I validate it using hook_validate I would like to add a class to the label? Any ideas how to achieve this?
I can't imagine why you'd want to do this in a validation function, and I think there's a far easier way to accomplish what you're trying to do.
Each element in a Drupal form is wrapped with a container (which has an ID). Inside this container there will only ever be one label.
So if you need to target the element in CSS or JS you just need to do something like this:
#existing-customer-edit label {
// The rule
}
OR
$('#existing-customer-edit label').something();
If you really need to edit the label manually then you're going to have to provide a custom theme for that element, have a look at this example for more information (it's for Drupal 6 but the concept is the same in Drupal 7).
thanks Clive did a fairly nasty work around in the form validation function
$form_state['complete form']['myselectbox']['#title'] = '<span class="privacy-error">you did not check me</span>';
It ain't pretty but it works!
You can add a class in hook_validate():
$form_state['complete form']['submitted']['existing_customer']['#attributes']['class'][] = 'class_name';

How do i preserve form input - after submiting form with dynamic menu list ?? Zend framework

Im trying to preserve the user inputs from dynamic menu dropdown lists - I have an number of drowpdowns and a user input text field , when the user submits the form after selecting the options from the dropdowns.
I would like to be able to preserve the last choices made so the user does not have to reselect the options again when re posting the form with another value in the text field, i would also like this to work on errors as well ?
Im using ZF to validate the form.
i have tried the follwing code in the value attr of the option:
<option value="<?php if ($_POST && errors) {
echo htmlentities($_POST['CategoryID'], ENT_COMPAT, 'UTF-8');
}?>">Main Category</option>
But does not seem to work ?
I have a static options "Main Category" ect. which is what the form defaults to after submiting
can anyone help me on this one ??
Thanks in advance
I would highly recommend using Zend_Form. If that is not possible, I would next use Zend_View Helpers to build your HTML manually. Then you can use the formSelect in your view like this:
echo $this->formSelect('CategoryId', $selected, $attribs, array(
'main' => 'Main Category'
// ... other options
));
Where $selected variable equals to one of the following: posted value(s), default value(s), or is null and $attribs variable is simply attributes for the select element.

How to add a custom field button to a field in SugarCRM

I looked through the developer blogs and I can only find ways to create a custom global form button or to replace a field with custom code. I don't want to replace the field, I want to add a button beside a field.
Here's what I have in mind:
[Date Field][date_picker_button] [time field][time_is_now]
The button I want to add is the time_is_now, allowing the user to populate the time field with the current time.
I have tried updating custom/modules/Notes/metadata/quickcreatedefs.php with:
array (
'name' => 'billable_start_c',
'label' => 'LBL_BILLABLE_START',
'customCode' => '<button</button>',
)
But this replaces the input field with the custom code. I could just put in the stock code and then my custom code, but date fields use yui and a widget, so I'd prefer not to.
Is there anyway to do something like:
'customCode' => '{default} <button></button>',
so that the default code gets output and then the custom code?
You could create a new SugarField template, which will essentially allow you to create the markup for your field. If you named your SugarField TimeIsNow, you could create a field with that type (instead of text,int, date or whatever). You should take a look at some of the other SugarFields to see how they are formatted, but it's really just HTML. When the field is set to that type, it pulls in that HTML template. You can tie your JS to the onclick function. Sugar is very easy to customize, so I definitely wouldn't get in the habit of solving all your customization needs this way...It's another way to look at it though.
Don't use custom code if you can help it. Either copy over the tpl to custom/modules/[yourmodule]/tpls/[yourtemplate] and code the bitton in there or use an after_ui_frame hook and some js to insert you custom button into the DOM.

How do I get a regular Checkbox in a Zend Form?

I have a form in Zend_Form that needs some checkboxes and I'd like them to be regular old checkboxes. You know, you give em a name and a value. If they are checked your post data contains name=>value.
Zend_Form is generating two inputs fields. One, the checkbox with a value=1 and the second a hidden input with a value=2. Both have the same name. I understand in theory how Zend expects the checkbox to work, but that's not how I expect it to work and it's not how I want it to work. How do I get my old fashion HTML checkbox back?
I have tried using $this->createElement, $this->addElement and creating a Zend_Form_Element_Checkbox manually. None allow me to set the checkbox's value and all generate the hidden input.
The final and REALLY correct answer is to add an option to the element :
$this->addElement('checkbox', 'my_element', array(
'label' => 'My Element Label',
'name' => 'my_element_name',
'disableHidden' => true
));
Zend_Form_Element_MultiCheckbox is what you're looking for.
The standard Checkbox element is meant to represent "yes/no" scenarios.
You could extend Zend library and add your own custom form element to render it just like you expect it. I did it for having a date field and it worked just fine.
I wonder why that does not work for you. You can set the values to anything you want (setCheckedValue() and setUncheckedValue()). So the only difference to normal checkbox is
if (null == $this->_getParam('checkbox', null)) {
//vs.
if ($unchecked == $this->_getParam('checkbox')) {
What exactly are you trying to do?