Not reading value from dropdown list - forms

'Duration' is a column in the table, which is 'required'
form:
<div class="row">
<?php echo $form->labelEx($model,'duration'); ?>
<?php
echo "<select>";
echo "<option value=\"1\">1</option>";
echo "<option value=\"1.5\">1.5</option>";
echo "<option value=\"2\">2</option>";
echo "<option value=\"2.5\">2.5</option>";
echo "<option value=\"3\">3</option>";
echo "</select>";
?>
<?php echo $form->error($model,'duration'); ?>
</div>
When i choose an option it comes with the error 'Duration cannot be blank'. How to make it read the value?

Make sure the name of the select is of Model[duration]
You need to have:
echo '<select name="ModelName[duration]">';
Also please don't mix PHP vanilla code in the Yii views, and use the methods provided by Yii.
I would do something like this:
echo CHtml::dropDownList('duration', $model, array('0' => 'Private', '1' => 'Public'));
Or even better:
echo $form->dropDownList($model, 'placement', $model::$def_duration, array('prompt' => 'Select'));
where def_duration would be a key=>value array defined in your model.

Related

How can I create an array of numbers based on which checkboxes are checked in a form?

I need to allow multiple values to be selected, and I want to create an array of the selected values. Here is what I have so far:
<p>Select Multiple Values
<?php echo $this->Form->create(); ?>
<?php foreach($possibilities as $possibility):
echo $this->Form->input($possibility['name'],
['type' => 'checkbox', 'value' => $possibility['id']]);
endforeach; ?>
<?php echo $this->Form->button(__('Submit'));
echo $this->Form->end(); ?></p>
How can I check this form to see what values have been selected and store them in an array?
You can try this script:
echo $this->Form->select('possibilities', $possibilities, [
'multiple' => 'checkbox'
]);
In controller simply:
$selected = $this->request->data('possibilities');
You can visit this CookBook Page

Inserting echo inside echo on php

I try to call an echo inside the shortcode.
The line of code is like this:
<?php echo do_shortcode('[audio src="<?php echo $audio; ?>"]'); ?>
The complete code is like this:
<?php if(get_post_meta($post->ID, '_format_audio_embed', true)!=''){
$audio = get_post_meta($post->ID, '_format_audio_embed', true);
} else {
$audio = '';
}
?></h1>
<?php echo do_shortcode('[audio src="<?php echo $audio; ?>"]'); ?>
My code did not work. Am I missing something?
Why are you using PHP inside PHP? Try to concat:
<?php echo do_shortcode('[audio src="'.$audio.'"]'); ?>

Form fields and label position in Zend framework 2

I have a Zend form created using Zend\Form\Annotation. I am using following code to generate the HTML:
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();
The problem is labels are placed just before the input text fields like this:
Label: Input text Box
I want to do like this:
Label:
Input Text Box
How can I format my labels to get my desired position?
What I am going to do is use individual elements print instead of using formCollection method like below:
<dl class="zend_form">
<?php echo $this->formElementErrors($form->get('identity')) ?>
<dt><?php echo $this->formLabel($form->get('identity')) ?></dt>
<dd><?php echo $this->formInput($form->get('identity')) ?></dd>
<dt><?php echo $this->formLabel($form->get('credential')) ?></dt>
<dd><?php echo $this->formInput($form->get('credential')) ?></dd>
<?php if ($this->redirect): ?>
<input type="hidden" name="redirect" value="<?php echo $this->redirect ?>" />
<?php endif ?>
<dd><?php echo $this->formButton($form->get('submit')) ?></dd>
</dl>
You just could use css:
label {
display: block;
}
just give the elements an id in your annotation like #Annotation\Attributes({"id" : "id"})

select menu default values not rendering in symfony

I am using Symfony 1.4, and am using embedded forms to place multiple similar forms into one form for a configuration page. I am having success showing the form, but the default values of the sfWidgetFormChoice widgets are not being rendered, i.e. the selected="selected" attribute is gone from the HTML.
Incidentally, the default values show up if I don't use embedded forms. The problem with avoiding embedded forms is that each form has identical inputs and therefore overwrites itself.
The action code is as such, some code omitted for brevity:
$serviceFormArray = array();
$this->fullForm = new ConfigForm();
foreach($this->serviceArray as $net => $service)
{
$this->partialForm = new ConfigForm();
foreach($service as $typeId => $val)
{
$typeObj = Doctrine::getTable('Type')->find($typeId);
$typeField = new sfWidgetFormChoice(array(
'default' => $val,
'choices' => array('1' => 'on', '0' => 'off'),
'label' => $typeObj->name)
);
$typeField->setDefault($val);
$serviceFormArray[$typeObj->name] = $typeField;
}
$netObj = Doctrine::getTable('Network')->find($net);
$this->partialForm->setWidgets($serviceFormArray);
$this->fullForm->embedForm($netObj->name,$this->partialForm);
}
and the template looks like this, some code omitted for brevity:
<div class="sectionBox">
<?php echo $fullForm->renderFormTag('/configure/submitconfig') ?>
<?php foreach ($fullForm->getVisibleFields() as $part => $field): ?>
<div class="settingsField">
<?php echo $field->renderLabel() ?>
<?php echo $field->render() ?>
<input type="hidden" name="plug" value="<?php echo $plugName; ?>"/>
</div>
<?php endforeach; ?>
<div id="submitConfig"><input type="submit" value="Save"/></div>
</form>
</div>
Try setting default value via $form->setDefault($name, $default).
$this->partialForm->setDefault($typeObj->name, $val);

Rendering only the <form> tag

Is there any way that i can render ONLY the start <form> tag of a Zend_Form object?
print $this->registerForm->renderForm();
renders <form></form>, and i only need <form>
Edit:
After Asleys possible solution i wrote this for My_Form class
public function renderFormOpen() {
return str_replace('</form>', '', $this->renderForm());
}
public function renderFormClose() {
return '</form>';
}
Still looking for at ZF way of doing thins, even though i don't think there is any - after going through the code in the ZF library.
You could write an custom form-decorator that uses a custom view-helper that only renders the open form tag. But I think this would be overkill.
Just "hardcode" the form-tags and fill the attributes with the data provided by the form-variable in your view.
<!--in your view-template -->
<form action="<?php echo $this->form->getAction() ?>"
enctype="<?php echo $this->form->getEnctype() ?>"
method="<?php echo $this->form->getMethod() ?>"
id="<?php echo $this->form->getId() ?>"
class="<?php echo $this->form->getAttrib('class') ?>" >
<!--in case your products are represented as elements -->
<?php foreach ($this->form->getElements() as $element): ?>
<?php echo $element ?>
<?php endforeach; ?>
<!--in case your products are represented as displayGroups -->
<?php foreach ($this->form->getDisplayGroups() as $displayGroup): ?>
<?php echo $displayGroup ?>
<?php endforeach; ?>
<!--in case your products are represented as subforms -->
<?php foreach ($this->form->getSubforms() as $subform): ?>
<?php echo $subform ?>
<?php endforeach; ?>
<!--in case your products are rendered by a view helper -->
<?php foreach ($this->products as $product): ?>
<?php echo $this->renderProduct($product) ?>
<?php endforeach; ?>
</form>
Just for fun the overkill way
// Get your products form
$form = new Form_Products();
// Add custom prefix path
$form->addPrefixPath('Foobar_Form_Decorator', 'Foobar/Form/Decorator', 'decorator');
// Set OnlyOpenTagForm-ViewHelper for FormDecorator
$form->getDecorator('Form')->setHelper('OnlyOpenTagForm');
// copy Zend/View/Helper/Form to Foobar/Form/Decorato/OnlyOpenTagForm.php
// In OnlyOpenTagForm.php
// replace Zend_View_Helper_Form with Foobar_View_Helper_OnlyOpenTagForm
// replace method "form" with onlyOpenTagForm"
// replace
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
// with:
if (false !== $content) {
$xhtml .= $content;
}
Done! - The Java-Guys will love it ;)
You can render just the open form tag by passing false to the form decorator like so:
<?php echo $this->form->renderForm(false) ?>
Which will output something like:
<form id="post" enctype="multipart/form-data" method="post" action="/post">
Additonally you can pass a string to the form decorator to be enclosed by the form tags like so:
<?php echo $this->form->renderForm('Some Text') ?>
Which outputs something like:
<form id="post" enctype="multipart/form-data" method="post" action="/simchas/post">Some Text</form>
Hope this helps...
You could do something like this:
echo $this->form->getDecorator('Form')->setElement($this->form)->render(false);