cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to append a template to an admin form?

Is it possible to append a template to an admin form?

I've been very hard to get this working but I have no idea if it's even possible. It feels as though it should be though.

 

I have created a block that extends widget form and renders a form in the admin panel.

 

The issue that I'm having is that I have a section of this form that's been created in a custom template file. Rather than creating forcing the entire form to be a template file, is there a way for me to append this data within the form itself?

 

I am easily able to add the template to the page, the only issue is that it's not contained within the `<form></form>` tags. Causing it to not submit the data that's in that template.

 

Here's my block class:

<?php

class Darkhorse_Onsite_Block_Adminhtml_PurchaseOrder_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
            'id'        => 'edit_form',
            'action'    => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method'    => 'post',
            'enctype'   => 'multipart/form-data'
        ));

        $purchaseOrder = Mage::registry('current_purchaseOrder');

        if ($purchaseOrder->getId()) {
            $form->addField('id', 'hidden', array(
                'name' => 'id',
            ));
        }

        $form->addField('job_id', 'hidden', array(
            'name'  => 'job_id',
        ));

        $fieldset = $form->addFieldset('base_fieldset', array('legend' => Mage::helper('onsite')->__('Purchase Order Settings')));

        $fieldset->addField('order_number', 'text', array(
            'label'     => Mage::helper('onsite')->__('Order Number'),
            'title'     => Mage::helper('onsite')->__('Order Number'),
            'name'      => 'order_number',
            'required'  => true,
        ));

        $fieldset->addField('vendor_id', 'select', array(
            'label'     => Mage::helper('onsite')->__('Vendor'),
            'title'     => Mage::helper('onsite')->__('Vendor'),
            'name'      => 'vendor_id',
            'values'    => Mage::getModel('onsite/vendor')->getCollection()->toOptionArray()
        ));

        $fieldset->addField('tracking', 'text', array(
            'label'     => Mage::helper('onsite')->__('Tracking Number'),
            'title'     => Mage::helper('onsite')->__('Tracking Number'),
            'name'      => 'tracking',
        ));

        $fieldset->addField('status_id', 'select', array(
            'label'     => Mage::helper('onsite')->__('Status'),
            'title'     => Mage::helper('onsite')->__('Status'),
            'name'      => 'status_id',
            'values'    => Mage::getModel('onsite/purchaseOrder_status')->getCollection()->toOptionArray(),
        ));

        $form->setUseContainer(true);
        $form->addValues($purchaseOrder->getData());
        $this->setForm($form);
        return parent::_prepareForm();
    }

    protected function _prepareLayout()
    {
        $items = $this->getLayout()->createBlock('onsite/adminhtml_purchaseOrder_item');
        $this->setChild('items', $items);
        return parent::_prepareLayout();
    }

    public function getFormHtml()
    {
        return parent::getFormHtml().$this->getChildHtml('items');
    }

}

And  the template:

<?php 
$items = Mage::getModel('onsite/purchaseOrder_item')->getCollection();
$items->addFieldToFilter('purchaseOrder_id', Mage::registry('current_purchaseOrder')->getId());
?>
<style>
.form-list tr .value input.small-input {width:50px;}
</style>
<div class="entry-edit">
    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend">Purchase Order Items</h4>
        <button class="save" style="float:right;" onclick="addItem()">Add Item +</button>
    </div>
    <div class="fieldset">
        <table class="form-list">
            <tbody id="testing123">
                <tr>
                    <td class="value"><strong>Part Number</strong></td>
                    <td class="value"><strong>Description</strong></td>
                    <td class="value"><strong>Quantity</strong></td>
                    <td class="value"><strong>Status</strong></td>
                    <td class="value"><strong>Price</strong></td>
                    <td class="value"><strong>Line Total</strong></td>
                </tr>
                <?php foreach ($items as $item): ?>
                    <tr>
                        <td class="value">
                            <input type="text" class="input-text" name="item[<?= $item->getId() ?>][sku]" value="<?= $item->getSku() ?>">
                        </td>
                        <td class="value">
                            <input type="text" class="input-text" name="item[<?= $item->getId() ?>][description]" value="<?= $item->getDescription() ?>">
                        </td>
                        <td class="value">
                            <input type="text" class="input-text small-input" name="item[<?= $item->getId() ?>][qty]" value="<?= ($item->getQty() ? $item->getQty() : '1') ?>">
                        </td>
                        <td class="value">
                            <select class="select" name="item[<?= $item->getId() ?>][status]">
                                <option>-- Please Select --</option>
                                <?= Mage::getModel('onsite/purchaseOrder_status')->getCollection()->toOptionsHtml($item->getStatusId()); ?>
                            </select>
                        <td class="value">
                            <input type="text" class="input-text small-input price-input" name="item[<?= $item->getId() ?>][price]" value="<?= $item->getPrice() ?>">
                        </td>
                        <td class="value" style="text-align:right">
                            <strong><?= Mage::helper('core')->currency(($item->getQty() ? $item->getPrice()*$item->getQty() : $item->getPrice())) ?></strong>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <div class="clear" style="border-top:1px solid #000; margin:5px;"></div>
        <div style="margin-left:5px">
            <h3>Total</h3>
            <h3 id="total"></h3>
        </div>
    </div>
</div>
<script>
var i = 0;
var addItem = function()
{
    var html = '<tr>' +
    '<td class="value">' +
    '<input type="text" class="input-text" name="item[add][' + i + '][sku]" value="">' +
    '</td>' +
    '<td class="value">' +
    '<input type="text" class="input-text" name="item[add][' + i + '][description]" value="">' +
    '</td>' +
    '<td class="value">' +
    '<input type="text" class="input-text small-input" name="item[add][' + i + '][qty]" value="">' +
    '</td>' +
    '<td class="value">' +
    '<select name="item[add][' + i + '][status]">' +
    '<option>-- Please Select --</option>' + 
    '<?= Mage::getModel('onsite/purchaseOrder_status')->getCollection()->toOptionsHtml(); ?>' +
    '</select>' +
    '<td class="value">' +
    '<input type="text" class="input-text small-input" name="item[add][' + i + '][price]" value="">' +
    '</td>' +
    '<td class="value">' +
    '</td>' +
    '</tr>';
    jQuery('#testing123').append(html);
    i++;
}

jQuery(document).ready(function(){
    var total = 0;
    jQuery('.price-input').each(function(){
        total += +jQuery(this).val();
    });
    jQuery('#total').html(total.toFixed(2));
});
</script>