symfony 1.4 FORMS - how to replace default (related) dropdown field by code generated in module / component? - forms

I've been creating with a ajax and doctrine chain-dropdown to select a category. I put them in component. It works like this:
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php
Im trying to replace default category dropdown build on schema and related with current table. When in
/lib/form/doctrine/TabbleForm.class.php
I remove a field with code:
unset($this['category']);
And in the tamplate which has a form body I paste a component code (with this dynamic ajax-based dropdown):
include_component('add', 'selectcategory',array('catid' => 0));
I have notice:
Unexpected extra form field named "category".
How can I replace that default code with category-field code from component / module?

How do you display your form in the template? Using a basic <?php echo $form; ?> or manually like:
<?php echo $form['field']->renderLabel() ?>
<?php echo $form['field']->render() ?>
<?php echo $form['field']->renderError() ?>
I suggest you to use the second method, at least using <?php echo $form['field']->renderRow() ?>. This way, you won't have to unset the categoy field from your Form class. But you will have to name the select tag in your component the same way the form do.
If the form display:
<select id="formname_category" name="formname[category]">...</select>
You will have to use the same name (for the second select I guess). Then, you won't have any problem in your form and, more important, the validator related to your category field will also work!

Related

How do i add a placeholder to a Multiselect form field on mobile devices

I'm using Code Ignitor to generate a multiselect form using the form_multiselect() function. I don't however believe this issue is specific to CI, rather the way that mobile browsers render multiselect form fields.
My form code:
<div id="searchform" class="search search-form search-horizontal">
<?php
$detectselect = isset($selecteddetec) ? $selecteddetec : '';
$gasselect = isset($selectedgases) ? $selectedgases : array();
echo form_open('searchproduct', array('name' => 'contact', 'class' => 'searchproduct'));
echo ('<div class="boxes">');
echo form_label('Type of detector', 'typeofdetect');
echo form_dropdown('typeofdetect', $detecttype, $detectselect);
echo ('</div>');
echo ('<div class="boxes">');
echo form_label('Gases to detect', 'gastodetect[]');
echo form_multiselect('gastodetect[]', $gasdetectopt, $gasselect, 'class="gas-multiselect" placeholder="Choose gases..."');
echo ('</div>');
echo form_hidden(array('posturi' => $pgalias));
echo form_submit('submit', 'Search', "class='buttons'");
?>
</form>
</div>
On a desktop browser, the form has the Chosen jQuery library applied which in turn handles the placeholders for the fields, these work correctly.
On mobile devices the Chosen library is disabled so normal form markup is used. The placeholder for the single select field works as expected however the multiselect displays '0 Selected' by default and nothing i do seems to change that.
Does anyone have any experience with overriding it? passing a placeholder attribute through the form_multiselect() function makes no difference, and I can't find any answers online. We need a placeholder as on mobile devices we have no form labels (and due to space in the display, we would really rather avoid them)
Edit:: Screenshot of the issue
Edit 2::
I have accepted Dray's answer below, for completeness this is what i needed to pass into the options array in code ignitor.
$a['0" default selected disabled="disabled'] = 'Choose gases...';
you can make the first option of your select field as:
<option value="" default disabled selected>
Your place holder here
</option>
P.S. This is just a workaround.

Drupal 7 custom node add form with Field Collections

I have a content type with a number of Field Collection fields.
The design for this form is relatively complex and cannot simply be styled with CSS. I want to output the form in a template, so each individual field can be styled.
I spent 6 hours last night looking for a solution to this. I've seen custom modules, hook_form_alter and various methods that might use Display Suite or other modules. None have worked for me so far.
My goal is to simply output the form fields (including Field Collection fields) in a template for styling. Has anybody achieved this?
You might be able to do it by creating an override template for the node add/edit page.
You will first need to add a new template suggestion for node forms. The first section of this page shows you how to do this:
https://www.drupal.org/node/1092122
After that, you should be able to print individual form elements into the new template. For example:
<div class="column-left">
<?php print drupal_render($form['first_name']); ?>
<?php print drupal_render($form['last_name']); ?>
</div>
<div class="column-right">
<?php print drupal_render($form['email']); ?>
<?php print drupal_render($form['phone']); ?>
</div>

How to integrate a non-model radio button element to a CActiveForm in Yii?

In a Yii application, I'm working on, there is a simple CActiveForm, where all elements are model related and look like this:
<div class="row">
<?php echo $form->labelEx($model, 'foo'); ?>
<?php echo $form->textField($model, 'foo', array(/* HTML options */)); ?>
<?php echo $form->error($model, 'foo'); ?>
</div>
Now I want to implement following case:
The should be a radio button element bar with two options. Depending on the selected option different form elements should be displayed (a dropdown list with existing bar-entries oder a set of field to create a new one). In the controller I want to analyze the field bar and, if needed (means e.g. if bar == 1) use the provided field set to create a new table entry.
CActiveForm#radioButton(...) needs a model as input. But for this case the model is not relevant -- the radio button I need should only contain/provide the information, how the data has to be processed. Is there a Yii conform way to implement this requirement and create a "non-model" form element for a Yii CActiveForm?

Symfony2: how to use one form for create/update like in symfony 1.x?

Symfony crud generator by default create two forms, for create and update. I want to create a twig template form.html.twig with only one form for both actions, I remember symfony 1.x I did this:
<form action="<?php echo url_for('blog/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
If object don't have id this is a new object.
You can check if it is a new object in template like this:
Twig:
action="{{ entity.id ? path('edit_url') : path('create_url')}}"
You do it the same way, you just need to provide a mechanism for checking if the entity is new. The easiest way to do that is to pass an is_new var to your template.

porting template to zend framework

I got few issues proting a pear based form to zend form.
I have few elements I need :
Basic Elements
Groups
Group Elements
Sections
I previously used templates to render the forms on Pear. I obviously cannot use pre-existing zend decorators, since I need to specify css classes for each of the components of my base elements.
To see the issue I need to render this, which is the template for a basic element :
<li class = "{position_in_the_form} {error}">
<label class="{label_class}"> {label}
[<span class="required_class"> * </span>]
</label>
<div> {element_content} </div>
[<p class = "{error_class}"> {error_message} </p>]
</li>
So as you can see I have many dynamic things I would like to be able to specify : position in the form, class for the label, class for the required section, the class for the error.
I would also like to be able to specify this from an ini file. I manage to set up the basic meta from the ini but not custom fields.
One of the reason I cannot use basic decorators is that I need to have "error" in the "li" class when there is an error in the element or the sub_form.I'm not sure this is possible with the error decorator... (correct me if I'm wrong)
Also, for the group I need something handling the errors, and since the core groups don't handle errors I need to subclass the sub_form. But how can I create a subform in an ini file and I don't know how to provide parameters to the sub form fromn the ini.
The main idea here is to be able to have visual and logic groups of elements in a form. For example I need a 'name' group with fullname, middle name, etc. This also implies a global validator for this "name" group.
An other thing is that I want to be able to position these groups : left half, right half, full
I got the css ready for this and working with pear.
So what I need is a simple solution, with few code and ini configurations. Unfortunately I think I got stuck in something too complicated, so if someone has any idea about a simple architecture it would be amazing!
Thanks in advance for your help,
Best, Boris
In your complex decoration need, you might want to use the ViewScript Zend_Form_Element_Decorator
$element->setDecorators(array(
array('ViewScript', array('viewScript' => 'path/to/your/views/element.phtml')),
));
and then in path/to/your/views/element.phtml, more or less something like
<li class="<?php echo $this->element->getAttrib('position_in_the_form') ?> <?php echo $this->element->hasErrors() ? 'error' : '' ?>">
<label class="<?php echo $this->element->getAttrib('label_class') ?>">
<?php echo $this->formLabel($this->element->getName(),
$this->element->getLabel()) ?>
<? if ( $this->element->isRequired() ) { ?>
[<span class="required_class"> * </span>]
<? } ?>
</label>
<div> <?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?> </div>
<? if ( $this->element->hasErrors() ) { ?>
[<p class="<?php echo $this->element->getAttrib('error_class') ?>"> <?php echo $this->formErrors($this->element->getMessages()) ?> </p>]
<? } ?>
</li>
This is only a drafty snippet of code, but should lead you in the direction you aim.
Regards