Hi everybody,
So, I have a problem and do not know how to fix it.
I've made a form in which I want some elements to be added when the user
click on some button. It's an user resume form, and, for example, I want the
user to be able to add as many as languages as he want, just by clicking the
"add language" button.
On the client side, every thing seems to work fine, and I receive what I
want in the $_POST.
Then I use this $_POST when received to recreate every elements to redisplay
the form. Every added elements are presents, but not their values. This
leads me to think that I've a problem with the isValid method.
Following the codes that make the trouble:
1. the method that create the form (attached to the Resume (Cv in
french) modell):
Code:

class Cv {
public function getEditForm($val2display = null,$val2exclude =
null,$valSubmitted = null) {
    $form = parent::getEditForm($val2display,$val2exclude);
    $elements[] = $form ->createElement('text','exp')
                        ->setLabel('Expérience');

    $languages = new Cwl_Form_AppendableElement($form,'lang');
    $languages->elem = $form->createElement('text','language')
                            ->setLabel('Langue')
                            ->setBelongsTo('lang');
    $languages->append($valSubmitted['lang']);
    $languages->appendAddButton();
    $elements = array_merge($elements,$languages->elems);

    foreach ($elements as $element) {
      $form->addElement($element);
    }
    return $form;
  }
}

2.the controller's code that call the form
Code:

  function createAction() {
    $form = Cv::getEditForm(null,array('comment','parent'),$_POST);
    $this->view->form = $form;
    $form->addValidateButton();
    if ($this->_request->isPost()) {
      if ($form->isValid($_POST)) {
      }
    }
  }

3.the Cwl_Form_AppendableElement class used to generate appendable elements:
Code:

class Cwl_Form_AppendableElement {
  public $form;
  public $array;
  public $elems = array();

  public function Cwl_Form_AppendableElement($form,$array) {
    $this->form = $form;
    $this->array = $array;
    return $this;
  }
  public function append($numberOrValSubmitted = 1) {
    if (is_null($numberOrValSubmitted)) $numberOrValSubmitted = 1;
    if (is_int($numberOrValSubmitted)) {
      for ($i=0;$i<=$number;$i++) {
        $elem = clone $this->elem;
        $this->currentElemId++;
        $elem->setLabel($elem->getLabel().' '.$this->currentElemId);
        $elem->setName($elem->getName().$this->currentElemId);
        $this->elems[] = $elem;
      }
    }
    if (is_array($numberOrValSubmitted)) {
      foreach ($numberOrValSubmitted as $key => $val) {
        $elem = clone $this->elem;
        $this->currentElemId++;
        $elem->setLabel($elem->getLabel().' '.$this->currentElemId);
        $elem->setName($key);
        $this->elems[] = $elem;
      }
    }
    return $this;
  }
  public function appendAddButton($label = null) {
    $button = $this->form->createElement('button','AddButton');
    $button->setLabel('Ajouter')->setBelongsTo($this->array);
    $receiverId = $this->array.'receiverid';
    $button->addDecorator('HtmlTag',array('tag' =>
'div','id'=>$receiverId,'placement'=>'prepend'));
    $elem = clone $this->elem;
    $elem->setName($elem->getName()."cwlcnt");
    $elem->setLabel($elem->getLabel()." cwlcnt");
    $html = $elem->__toString();
    $html = str_replace('"','"+String.fromCharCode(34)+"',$html);
    $html = str_replace(CHR(13),"",$html);
    $html = str_replace(CHR(10),"",$html);
    $button->setAttrib('onclick','if
(document.'.$this->array.'Count){document.'.$this->array.'Count++;}else{document.'.$this->array.'Count='.(count($this->elems)+1).';};t="'.$html.'";while
(t.match("cwlcnt"))
{t=t.replace("cwlcnt",document.'.$this->array.'Count);};r=document.getElementById("'.$receiverId.'");r.innerHTML
= r.innerHTML+t;');
    $this->elems[] = $button;
  }
}
The main idear is to copy the html code of one form element in a js function
that will reproduice it, incrementing what needs to be, when the user click
on the button ; all to respect the code that Zend would have produice. Then,
when the form is submitted, to analyze the sent datas to recreate all
elements added by the user in a Zend_Form, so it could be validate.

Any idears?

Best regards,

Stephane Mourey

Reply via email to