Zend Form Element Label "for" Attribute - php

I subclass Zend_Form to allow re-use as I describe in my other SO question. It's working very well, except for one issue that I found. In my view script I use this code to render the label for fields:
echo $this->formLabel($this->element->getFullyQualifiedName(),
$this->element->getLabel());
The rendered label has the original element id as the value in the for attribute rather than the new, suffixed, element id. Is there a bug in the Zend code, am I missing a step or doing something incorrectly?

I think the reason is that you use formLabel view helper independently. As a result, the helper is not aware of any attributes that you specified for your input text field. So, you should provide these attributes to the formLabel. For example you could do the following:
echo $this->formLabel(
$this->element->getFullyQualifiedName(),
$this->element->getLabel(),
$this->element->getAttribs()
);
The above code should produce for tag that matches your input elements id. Otherwise, the for tag will be set to the elements name.

Related

Zend File element and setElementsBelongTo()

I'm currently trying to create a form using Zend_Form. Inside the form there is a displayGroup of File element and Text element. Since it is a dynamic form, I would like the input field names to be in array form (something like the one below).
requestRow[1][fileName]
requestRow[1][fileDesc]
requestRow[2][fileName]
requestRow[2][fileDesc]
setElementsBelongTo() worked for the text elements but not on the File elements. Is it the correct behavior? Or do I need extra method call or attribute/property for that?
If it is the specification, is there any workaround for that?
I have also tried manually setting the File element name with array format but it also doesn't work.
I'm using Zend 1.12
Thank you in advance!
Saw the following comment about the current limitation File element.
Posted by Thomas Weidner (thomas) on 2011-09-19T06:30:50.000+0000
No, this is a limitation of php's file upload capability. From ZF's
manual:
{quote} File elements in Subforms
When you use file elements in subforms you must set unique names. For
example, if you name a file element in subform1 "file", you must give
any file element in subform2 a different name.
If there are 2 file elements with the same name, the second element is
not be displayed or submitted.
Additionally, file elements are not rendered within the sub-form. So
when you add a file element into a subform, then the element will be
rendered within the main form. {quote}
Posted by Thomas Weidner (thomas) on 2011-09-29T18:11:15.000+0000
Closing as wont fix as there is actually no way to solve this
limitation
http://framework.zend.com/issues/browse/ZF-11741

Trying to dynamically add options to an array of HTML select fields Php/AJAX/jQuery

I have an HTML array of 10 select fields that must be populated by jQuery each time the div-popup is called. (Each one gets the same options) We have periodic needs for hundreds of employees and this form is intended to allow requests staffing to be forwarded to upper management for approval in a batch fashion by department. I’ll be using .serialize to submit the form but I am unclear as to how to initialize this element with the proper information for input selection. All the info I could find was about submitting the form array, which I already knew how to do.
Here is the HTML structure:
<select id="detJobsCR[]" name="detJobsCR[]"> </select>
And here is my attempt to populate the selection fields:
$.post("events.php",{a: 'detadd-joblist', dept_id: deptID}, function(data) {
for(var i=0;i<10;i++){
$('#detJobsCR[i]').html(data);
}
});
(I tried explicitly defining each elements array position [0],[1]..etc and that did not help.)
The data returned is simple HTML like:
<option value='38'>Admin Support Assistant I</option>
<option value='39'>Admin Admin Support Assistant II</option>...
Thanks for whatever assistance you can offer!
FYI Update: Musa's answer worked perfectly. I also had issues with Date & Time pickers not working, it turns out, for the same reason. When I converted them to classes I was able to use this(below), and the are all now working as well:
$(".detDateStart").datepicker();
Ids should be unique so each select should have its own id. If you want to have one identifiier to represent all the selects you could use a class e.g. <select class="detJobsCR" name="detJobsCR[]"> </select> and then select them with $('.detJobsCR') and set their html with .html(data)
$.post("events.php",{a: 'detadd-joblist', dept_id: deptID}, function(data) {
$('.detJobsCR').html(data);
});
Your problem is two fold:
You're putting i inside your quotes, so it's not interpreting the value of the variable i
Even if i wasn't inside the quotes, when jquery parses the [i] in the expression it's looking for elements with id detJobsCR and with attribute i defined.
Since you have only ONE element, with ID detJobsCR[] you would query it like this: $('#detJobsCR\\[\\]') You need to escape the [ and ] character so jquery knows it's a part of the ID and NOT a parsing rule (for parsing/looking for an attribute).
As suggested by Musa, you can make your life much easier by using a simple class name that doesn't require escaping values. But if you do want to select by id, the example above should do the trick.

Symfony embedded form - possible to NOT show embedded form's "parent" label?

When you embed a form in Symfony, a label for the embedded form is injected into the parent form. For example: If I have a PersonForm and add this code $this->embedForm('Address', $addressForm), my PersonForm will now have an 'Address' label within it, in addition to the labels for the fields that make up the AddressForm. I'd like to keep the labels for the individual fields, but get rid of the 'Address' label, thereby making it appear that the two forms are really one.
It's possible to override the form template and manually iterate over the form elements and echo them one by one, but I run into this situation frequently and I'd prefer to have Symfony handle this automatically.
The following code will allow you to set the label to another an empty string but I suspect it would still appear anyway.
$this->embedForm('Address', $addressForm)
$this->widgetSchema['Address']->setLabel('');
however I suspect the best thing to use is to look at point 6 (embedMergeForm) on this page and use that http://itsmajax.com/2011/01/29/6-things-to-know-about-embedded-forms-in-symfony/
Given your situation, iterating manually over the widgets is the only option. The other option is extending sfWidgetFormSchemaFormatter, but that won't allow hiding a label for an embedded form, while at the same time not hiding it for any other widget.
If you do run into this situation often, you could consider creating a partial just for rendering your form in this specific way.
Here is a simple way of disabling all labels. Add this method to BaseForm if you do it frequently.
public function disableLabels()
{
$fields = $this->getWidgetSchema()->getFields();
$this->getWidgetSchema()->setLabels(array_combine(array_keys($fields), array_fill(0, count($fields), false)));
}
If you only want to disable labels in the embedded form, disable them before embedding:
$form = new FormToEmbed();
$form->disableLabels();
$parent->embedForm('child', $form);

How to hide a field in a Drupal form

I have a specific content type in drupal6. I want to implement a hook, which hides the body field of that content type from the add form, but not from the edit form. How can I do that?
You can use hook_form_alter. Which you can programmatically alter the contents of the form api build. This gives you the full $form array of which you can simply unset($form['the_field_you_dont_want']);.
But the easier way to get rid of the body field is in the edit content type there is a field labelled 'Body field label:' just leave this blank and the body field will be omitted.
unset seems to destroy the value as well, as does the #access property.
I just use this to hide a field (in this case a reference if it was preset using the URL:
$form['field_reference']['#prefix'] = "<div class='hide'>";
$form['field_reference']['#suffix'] = "</div>";
The solution found here https://drupal.stackexchange.com/questions/11237/hide-field-in-node-add-page works great for me. Here I am repeating moon.watcher's solution:
function test_remove_filed_form_alter(&$form, &$form_state) {
if (arg(0) == 'node' && arg(1) == 'add') {
$form['field_test']['#access'] = 0;
}
}
The disadvantage of using unset() is that it will entirely remove the field and you can't further on like, for example, on node presave. In my case, I just wanted to remove the field from the form on the first moment, but I wanted to populate it later on, prior to saving the node. The solution on the link above works perfect for me for this reason.
Did you implement the content type within a module (using hook_node_info)? If so, set the has_body attribute to false.

Zend Framework - Static form elements

I have got a form which a user can use to create a new store and to edit an existing one. When this form is being used to edit a store there are certain fields that I want the user to see but not edit eg. store_id. I have explored the different Zend_Form_Elements hoping to find some kind of static element but with no luck.
So my question is, how do I display information using Zend_Form that a user can't edit?
Thanks.
readonly alone is not enough, because users will still be able to edit it if they really want. You should use $element->setIgnore(true) that will ensure that Zend_Form_Element won't try to populate the element from POST/GET, and I'd double check that also. You have to make sure the values you are getting into the databases can never contain this element.
Finally, if you would like your element to be displayed in a different way than just with readonly, you can do that by changing the element decorators.
I just managed to work this one out myself. The solution was to change the view helper on the elements to the formNote helper eg. $element->helper = 'formNote'. The result of this was that the value gets displayed as straight text instead of being inside a form element.
Thanks for your answers.
That's very good solution when you don't need to populate the element value when the form is submitted.
It's equivalent solution is to use the Form Element method setAttrib() and disable the form element
$formElement->setAttrib('disable','disable')
which will only freeze the element.
But if you need to populate the field, using the previous solutions you will probably need additional hidden field added, which will pass the value. Developing custom form element will be good style but that's not welcomed by each developer so you can use some tricky way to set a form element as a text only but populate its value. That way is when you create the element as a hidden field, set its value and use the Form Element method setDescription() to set and display the element text value.
$formElement = new Zend_Form_Element_Hidden( 'elName',
array( 'label' => 'elLabel', 'value' => 'elValue' ) );
$formElement->setDescription( 'elValue' );
Then you can render that hidden element and display the value with the
$formElement->getDescription().
$element->setAttrib('readonly', 'true');
http://www.w3.org/TR/html401/interact/forms.html#adef-readonly
According to Amr Mostafa, if you use:
$element->setAttrib('readonly', 'true');
OR
$element->setAttribs(array('disabled' => 'disabled'));
User still send values by POST/GET and they are stored in DB.
The only way for me to don't taking into account the values from POST/GES is:
$element->setIgnore(true)
Example:
$element = new Zend_Form_Element_Text('element');
$element->setIgnore(true);

Categories