We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

setValue only working for Submit ? | Forum

Clement Peronneaud
Clement Peronneaud Apr 5 '12
Hi all,


I'm trying to create an edit page with fields filled with the database informations by default.

My problem is that the setValue does not work except for the submit field.

So I have to use the addAttribute method to create the default value.

But with this solution, I can't choose the default value for the SelectBox and RadioBox.


Is there something I misunderstood ?

I checked the core code, but even by setting the value property to public and forcing it in my controller, the option won't be selected by default to the one I want.


Any help is welcome and it's quite urgent.

Michael I.
Michael I. Apr 9 '12
Topic was moved from Bug reports and troubleshooting.
Clement Peronneaud
Clement Peronneaud Apr 10 '12
Still no news for this problem ? 


I thought the answer would be fast :/


To make it easier :


How do I choose the selected value for a selectbox and radiobox in oxwall ?


Thanks

Clement Peronneaud
Clement Peronneaud Apr 18 '12
Up again...


I can't believe nobody has at least an answer or an explanation for this problem.


When I check the core code for the custom questions which are also selectbox, they also use the setValue method and it's working...


When I do the same in my plugin, it's not.


Is it an Oxwall core bug ? Or am I doing this ? Could someone even give me some hint on how it should be working ?

Purusothaman Ramanujam
It's not an surprise that new developers does not get response for the questions.

I am also looking for a solution for this kind of problem.

Clement Peronneaud
Clement Peronneaud Apr 23 '12
Well, the only solution at the moment is to not use their built-in class for radio and select. Just put your values and labels in an array and create the input directly in smarty...
Stupid solution but at least it works...
Tim Thaens
Tim Thaens Mar 26 '14
This is kind of an old topic. 

But I came across it searching for the same thing. And it was the only topic I found related to this.


Anyway, since I dind't find the solution in any other topic. I solved it myself.
And I think it's good to share it with you all.



What I did, was I modified the /ow_core/form_element.php class a bit.

Search for the Selectbox class, and add a private variable:


/** * Form element: Selectbox. 

   * * @author Sardar Madumarov 

      * @package ow_core 

      * @since 1.0 

*/ 

class Selectbox extends InvitationFormElement { 

/** 

   * Input options. 

   * * @var array 

*/ 

private $options = array(); 

private $selected = null;



Also make a public function that sets the selected value.

I added mine after the setOptions function:


/** * Sets field options. 

      * * @param array $options 

      * @return Selectbox 

      * @throws InvalidArgumentException 

*/ 

public function setOptions( $options ) { 

if ( $options === null || !is_array($options) ) { throw new InvalidArgumentException('Array is expected!'); } 

$this->options = $options; 

return $this; 


public function setSelected($key) {

   $this->selected = $key; 

   return $this; 



And last, modfy the render function:


/** * @see FormElement::renderInput() 

      * * @param array $params 

         * @return string 

*/ 


public function renderInput( $params = null ) 

parent::renderInput($params); 

$optionsString = '';

if ( $this->hasInvitation ) { $optionsString .= UTIL_HtmlTag::generateTag('option', array('value' => ''), true, $this->invitation); }


foreach ( $this->options as $key => $value ) {

$attrs = array();

if($this->selected == $key) { $attrs['selected'] = 'selected'; }

$attrs['value'] = $key;

$optionsString .= UTIL_HtmlTag::generateTag('option', $attrs, true, trim($value));

}


return UTIL_HtmlTag::generateTag('select', $this->attributes, true, $optionsString);

}





Now you can call the function from within your code:
$fieldDay = new Selectbox('day');

for($i=1;$i<10;$i++) { $fieldDay->addOption($i, "0".$i); }

for($i=10;$i<32;$i++) { $fieldDay->addOption($i, $i); }

$fieldDay->setLabel(OW::getLanguage()->text('myplugin', 'planform_label_date'));

$fieldDay->setRequired();

$fieldDay->setHasInvitation(false);

$fieldDay->setSelected('9');

$form->addElement($fieldDay);


The Forum post is edited by Tim Thaens Mar 26 '14
Tim Thaens
Tim Thaens Jun 19 '14
Just found a much better way, not to interfere with the core functions.


I wrote a new class in my component that is a child of the core selectbox:

(change the name to your plugin of course)

Create a file 'selectbox.php' in the folder classes under your plugin root-folder.


<?php
class <PLUGIN>_CLASS_Selectbox extends Selectbox{     private $selected = null;
   public function __construct( $name )   {       parent::__construct($name);   }      public function setSelected($key)   {      $this->selected = $key;      return $this;   }      public function renderInput( $params = null )    {      parent::renderInput($params);      $optionsString = '';
      if ( $this->hasInvitation ) { $optionsString .= UTIL_HtmlTag::generateTag('option', array('value' => ''), true, $this->invitation); }
      foreach ( $this->getOptions() as $key => $value )      {         $attrs = array();         if($this->selected == $key) { $attrs['selected'] = 'selected'; }         $attrs['value'] = $key;         $optionsString .= UTIL_HtmlTag::generateTag('option', $attrs, true, trim($value));      }      return UTIL_HtmlTag::generateTag('select', $this->attributes, true, $optionsString);   }}?>
Now you can just create such a selectbox:

        $field = new <PLUGIN>_CLASS_Selectbox('field');        $field->addOption('1', 'a');        $field->addOption('2', 'b');        $field->addOption('3', 'c');        $field->addOption('4', 'd');        $field->setHasInvitation(false);        $field->setRequired(true);        $field->setSelected('2');        $form->addElement($field);