1

I have an admin form with following field

$author = Mage::helper('blog/post')->getDefaultAuthorId();

    $fieldset->addField('post_author', 'select', array(
        'label' => Mage::helper('sprung_blog')->__('Author'),
        'name'  => 'post_author',
        'required'  => true,
        'class' => 'required-entry',
        'value' => $author,
        'values'=> Mage::getModel('sprung_blog/post_attribute_source_postauthor')->getAllOptions(true),
    ));

Postauthor.php file contains following

public function getAllOptions($withEmpty = true, $defaultValues = false){

$collection = Mage::getModel('blog/author')->getCollection();

$options = array(array('value'=>'', 'label'=>'Please Select Author'));

foreach ($collection as $cat){

    $options[] = array('value'=>$cat['entity_id'],'label'=>$cat['author_name']);
}   
return $options;

}

Here, I have one author as selected default from admin configuration. So when i add a new post , "post_author" field i need to display the default user id as selected. After setting 'value' also its not showing as selected in admin form.Please help

  • try clearing your cache – Alex Dinca Jul 2 '14 at 9:46
  • Yeah, I have done everything. But its not coming – Ann Mary Jul 2 '14 at 9:54
0

I have added following code to _prepareForm()

$formValues = Mage::registry('current_post')->getDefaultValues();
    if (!is_array($formValues)){
        $formValues = array();
    }
    if (Mage::getSingleton('adminhtml/session')->getPostData()){
        $formValues = array_merge($formValues, Mage::getSingleton('adminhtml/session')->getPostData());
        Mage::getSingleton('adminhtml/session')->setPostData(null);
    }
    elseif (Mage::registry('current_post')){
        $formValues = array_merge($formValues, Mage::registry('current_post')->getData());
    }

/* My code start here */

    $author = Mage::helper('sprung_blog/post')->getDefaultAuthorId();

    if(empty($formValues['post_author'])){

        $formValues['post_author'] = $author;;

    }

    $form->setValues($formValues);

and field changed to

 $fieldset->addField('post_author', 'select', array(
    'label' => Mage::helper('blog')->__('Author'),
    'name'  => 'post_author',
    'required'  => true,
    'class' => 'required-entry',
    'values'=> Mage::getModel('sprung_blog/post_attribute_source_postauthor')->getAllOptions(true),
));

Now it works fine..

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.