Changeset 504

Show
Ignore:
Timestamp:
07/20/07 18:13:35 (1 year ago)
Author:
laurentj
Message:

worked on ticket #182, jforms: improved support of checkboxes and listboxes, added support of defaultvalue attribute, added new method in jFormsBase : saveControlToDao and initControlToDao for controls which are containers
added new method in jDaoBase : deleteBy()
added new method in jDaoRecordBase : getPrimaryKeyNames()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/manifests/testapp.mn

    r483 r504  
    9090cd testapp/modules/jelix_tests/daos 
    9191  products.dao.xml 
     92  product_tags.dao.xml 
    9293  description.dao.xml 
    9394cd testapp/modules/jelix_tests/forms 
  • trunk/build/manifests/testapp.mn

    r483 r504  
    9090cd testapp/modules/jelix_tests/daos 
    9191  products.dao.xml 
     92  product_tags.dao.xml 
    9293  description.dao.xml 
    9394cd testapp/modules/jelix_tests/forms 
  • trunk/lib/jelix-modules/junittests/classes/jhtmlrespreporter.class.php

    r462 r504  
    8989 
    9090   function paintDiff($stringA, $stringB){ 
     91$this->_response->body->append('MAIN','<!--A:'.$stringA.'-->'); 
     92$this->_response->body->append('MAIN','<!--B:'.$stringB.'-->'); 
    9193        $diff = new Diff(explode("\n",$stringA),explode("\n",$stringB)); 
    9294        if($diff->isEmpty()) { 
  • trunk/lib/jelix-modules/junittests/classes/jhtmlrespreporter.class.php

    r462 r504  
    8989 
    9090   function paintDiff($stringA, $stringB){ 
     91$this->_response->body->append('MAIN','<!--A:'.$stringA.'-->'); 
     92$this->_response->body->append('MAIN','<!--B:'.$stringB.'-->'); 
    9193        $diff = new Diff(explode("\n",$stringA),explode("\n",$stringB)); 
    9294        if($diff->isEmpty()) { 
  • trunk/lib/jelix/core-modules/jelix/locales/en_EN/formserr.ISO-8859-1.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
    912js.err.required= "%s" field is required 
     13 
  • trunk/lib/jelix/core-modules/jelix/locales/en_EN/formserr.ISO-8859-1.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
    912js.err.required= "%s" field is required 
     13 
  • trunk/lib/jelix/core-modules/jelix/locales/en_EN/formserr.UTF-8.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
  • trunk/lib/jelix/core-modules/jelix/locales/en_EN/formserr.UTF-8.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
  • trunk/lib/jelix/core-modules/jelix/locales/en_US/formserr.ISO-8859-1.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
  • trunk/lib/jelix/core-modules/jelix/locales/en_US/formserr.ISO-8859-1.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
  • trunk/lib/jelix/core-modules/jelix/locales/en_US/formserr.UTF-8.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
  • trunk/lib/jelix/core-modules/jelix/locales/en_US/formserr.UTF-8.properties

    r473 r504  
    55datatype.unknow=(804) unknow datatype %1$s  on %2$s (file %3$s) 
    66invalid.xml.file=(805) invalid xml syntax in jforms file (file %s) 
     7selected.attribute.not.allowed=(806)"selected" attribute on <item> is not allowed when using defaultvalue attribute or <defaultvalues> (file %s) 
     8defaultvalues.not.allowed=(807) <defaultvalues> is not allowed in a <listbox multiple="false"> element (file %s) 
     9multiple.selected.not.allowed=(808)Only one selected item is allowed in <listbox multiple="false">, <radiobuttons> and <menulist> elements (file %s) 
    710 
    811js.err.invalid= "%s" field is invalid 
  • trunk/lib/jelix/core-modules/jelix/locales/fr_FR/formserr.ISO-8859-1.properties

    r473 r504  
    55datatype.unknow=(804) datatype inconnu %1$s sur %2$s (fichier %3$s) 
    66invalid.xml.file=(805) erreur de syntaxe xml dans un fichier jforms (fichier %s) 
     7selected.attribute.not.allowed=(806)L'utilisation de l'attribut selected sur <item> n'est pas autoris�uand il y a aussi l'attribut defaultvalue ou l'element <defaultvalues> (fichier %s) 
     8defaultvalues.not.allowed=(807)La balise <defaultvalues> n'est pas autoris�ans un element <listbox multiple="false"> (fichier %s) 
     9multiple.selected.not.allowed=(808)Dans les element <listbox multiple="false"> <radiobuttons> et <menulist>, un seul item s�ctionn�st autoris�fichier %s) 
    710 
    811js.err.invalid=La saisie de "%s" est invalide 
  • trunk/lib/jelix/core-modules/jelix/locales/fr_FR/formserr.ISO-8859-1.properties

    r473 r504  
    55datatype.unknow=(804) datatype inconnu %1$s sur %2$s (fichier %3$s) 
    66invalid.xml.file=(805) erreur de syntaxe xml dans un fichier jforms (fichier %s) 
     7selected.attribute.not.allowed=(806)L'utilisation de l'attribut selected sur <item> n'est pas autoris�uand il y a aussi l'attribut defaultvalue ou l'element <defaultvalues> (fichier %s) 
     8defaultvalues.not.allowed=(807)La balise <defaultvalues> n'est pas autoris�ans un element <listbox multiple="false"> (fichier %s) 
     9multiple.selected.not.allowed=(808)Dans les element <listbox multiple="false"> <radiobuttons> et <menulist>, un seul item s�ctionn�st autoris�fichier %s) 
    710 
    811js.err.invalid=La saisie de "%s" est invalide 
  • trunk/lib/jelix/core-modules/jelix/locales/fr_FR/formserr.UTF-8.properties

    r473 r504  
    55datatype.unknow=(804) datatype inconnu %1$s sur %2$s (fichier %3$s) 
    66invalid.xml.file=(805) erreur de syntaxe xml dans un fichier jforms (fichier %s) 
     7selected.attribute.not.allowed=(806)L'utilisation de l'attribut selected sur <item> n'est pas autorisé quand il y a aussi l'attribut defaultvalue ou l'element <defaultvalues> (fichier %s) 
     8defaultvalues.not.allowed=(807)La balise <defaultvalues> n'est autorisé que dans les elements <listbox multiple="true"> et <checkboxes> (fichier %s) 
     9multiple.selected.not.allowed=(808)Dans les element <listbox multiple="false"> <radiobuttons> et <menulist>, un seul item sélectionné est autorisé (fichier %s) 
    710 
    811js.err.invalid=La saisie de "%s" est invalide 
  • trunk/lib/jelix/core-modules/jelix/locales/fr_FR/formserr.UTF-8.properties

    r473 r504  
    55datatype.unknow=(804) datatype inconnu %1$s sur %2$s (fichier %3$s) 
    66invalid.xml.file=(805) erreur de syntaxe xml dans un fichier jforms (fichier %s) 
     7selected.attribute.not.allowed=(806)L'utilisation de l'attribut selected sur <item> n'est pas autorisé quand il y a aussi l'attribut defaultvalue ou l'element <defaultvalues> (fichier %s) 
     8defaultvalues.not.allowed=(807)La balise <defaultvalues> n'est autorisé que dans les elements <listbox multiple="true"> et <checkboxes> (fichier %s) 
     9multiple.selected.not.allowed=(808)Dans les element <listbox multiple="false"> <radiobuttons> et <menulist>, un seul item sélectionné est autorisé (fichier %s) 
    710 
    811js.err.invalid=La saisie de "%s" est invalide 
  • trunk/lib/jelix/dao/jDaoBase.class.php

    r483 r504  
    6363 
    6464    /** 
     65     * @return array list of properties name which contains primary keys 
     66     */ 
     67    public function getPrimaryKeyNames(){ return $this->_pkFields; } 
     68 
     69    /** 
    6570     * check values in the properties of the record, according on the dao definition 
    6671     * @return array list of errors 
     
    341346    } 
    342347     
     348 
     349    /** 
     350     * delete all record corresponding to the conditions stored into the 
     351     * jDaoConditions object. 
     352     * @param jDaoConditions $searchcond 
     353     * @return 
     354     */ 
     355    public function deleteBy ($searchcond){ 
     356        if ($searchcond->isEmpty ()){ 
     357            return; 
     358        } 
     359        $query = 'DELETE FROM '.$this->_tables[$this->_primaryTable]['realname'].' WHERE '; 
     360        $query .= $this->_createConditionsClause($searchcond, false); 
     361 
     362        return $this->_conn->exec($query); 
     363    } 
     364 
     365 
    343366    /** 
    344367     * create a WHERE clause with conditions on primary keys with given value. This method 
     
    361384    * @internal 
    362385    */ 
    363     protected function _createConditionsClause($daocond){ 
     386    protected function _createConditionsClause($daocond, $withOrder=true){ 
    364387 
    365388        $c = $this->_DaoRecordClassName; 
     
    369392        $sql = $this->_generateCondition ($daocond->condition, $fields, true); 
    370393 
    371         $order = array (); 
    372         foreach ($daocond->order as $name => $way){ 
    373             if (isset($fields[$name])){ 
    374                 $order[] = $name.' '.$way; 
    375             } 
    376         } 
    377         if(count ($order) > 0){ 
    378             if(trim($sql) =='') { 
    379                 $sql.= ' 1=1 '; 
    380             } 
    381             $sql.=' ORDER BY '.implode (', ', $order); 
     394        if($withOrder){ 
     395            $order = array (); 
     396            foreach ($daocond->order as $name => $way){ 
     397                if (isset($fields[$name])){ 
     398                    $order[] = $name.' '.$way; 
     399                } 
     400            } 
     401            if(count ($order) > 0){ 
     402                if(trim($sql) =='') { 
     403                    $sql.= ' 1=1 '; 
     404                } 
     405                $sql.=' ORDER BY '.implode (', ', $order); 
     406            } 
    382407        } 
    383408        return $sql; 
  • trunk/lib/jelix/dao/jDaoBase.class.php

    r483 r504  
    6363 
    6464    /** 
     65     * @return array list of properties name which contains primary keys 
     66     */ 
     67    public function getPrimaryKeyNames(){ return $this->_pkFields; } 
     68 
     69    /** 
    6570     * check values in the properties of the record, according on the dao definition 
    6671     * @return array list of errors 
     
    341346    } 
    342347     
     348 
     349    /** 
     350     * delete all record corresponding to the conditions stored into the 
     351     * jDaoConditions object. 
     352     * @param jDaoConditions $searchcond 
     353     * @return 
     354     */ 
     355    public function deleteBy ($searchcond){ 
     356        if ($searchcond->isEmpty ()){ 
     357            return; 
     358        } 
     359        $query = 'DELETE FROM '.$this->_tables[$this->_primaryTable]['realname'].' WHERE '; 
     360        $query .= $this->_createConditionsClause($searchcond, false); 
     361 
     362        return $this->_conn->exec($query); 
     363    } 
     364 
     365 
    343366    /** 
    344367     * create a WHERE clause with conditions on primary keys with given value. This method 
     
    361384    * @internal 
    362385    */ 
    363     protected function _createConditionsClause($daocond){ 
     386    protected function _createConditionsClause($daocond, $withOrder=true){ 
    364387 
    365388        $c = $this->_DaoRecordClassName; 
     
    369392        $sql = $this->_generateCondition ($daocond->condition, $fields, true); 
    370393 
    371         $order = array (); 
    372         foreach ($daocond->order as $name => $way){ 
    373             if (isset($fields[$name])){ 
    374                 $order[] = $name.' '.$way; 
    375             } 
    376         } 
    377         if(count ($order) > 0){ 
    378             if(trim($sql) =='') { 
    379                 $sql.= ' 1=1 '; 
    380             } 
    381             $sql.=' ORDER BY '.implode (', ', $order); 
     394        if($withOrder){ 
     395            $order = array (); 
     396            foreach ($daocond->order as $name => $way){ 
     397                if (isset($fields[$name])){ 
     398                    $order[] = $name.' '.$way; 
     399                } 
     400            } 
     401            if(count ($order) > 0){ 
     402                if(trim($sql) =='') { 
     403                    $sql.= ' 1=1 '; 
     404                } 
     405                $sql.=' ORDER BY '.implode (', ', $order); 
     406            } 
    382407        } 
    383408        return $sql; 
  • trunk/lib/jelix/docs/ns/jforms-controls.rng

    r493 r504  
    3333      <optional><attribute name="required"><choice><value>true</value><value>false</value></choice></attribute></optional> 
    3434  </define> 
     35  <define name="UI.attr.defaultvalue"> 
     36      <optional><attribute name="defaultvalue" /></optional> 
     37  </define> 
     38   
    3539   
    3640  <define name="List.UI.Common"> 
     
    4347        <ref name="daodatasource" /> 
    4448    </choice> 
     49    <optional> 
     50        <choice> 
     51            <attribute name="selectedvalue" /> 
     52            <element name="selectedvalues"> 
     53                <oneOrMore> 
     54                <element name="value"><text/></element> 
     55                </oneOrMore> 
     56            </element> 
     57        </choice> 
     58    </optional> 
    4559  </define> 
    4660   
     
    5367             </choice> 
    5468         </optional> 
     69         <optional><attribute name="selected"><choice><value>true</value><value>false</value></choice></attribute></optional> 
    5570         <attribute name="value" /> 
    5671     </element> 
     
    100115      </attribute> 
    101116  </define> 
    102    
    103  
    104  
    105117 
    106118  <!--  Controls       --> 
     
    133145       <ref name="UI.Common" /> 
    134146       <ref name="UI.Common.data" /> 
     147       <ref name="UI.attr.defaultvalue" /> 
    135148     </element> 
    136149  </define> 
     
    141154       <ref name="UI.attr.readonly" /> 
    142155       <ref name="UI.attr.required" /> 
     156       <ref name="UI.attr.defaultvalue" /> 
    143157     </element> 
    144158  </define> 
     
    172186       <ref name="UI.Common" /> 
    173187       <ref name="UI.attr.readonly" /> 
     188       <ref name="UI.attr.defaultvalue" /> 
    174189     </element> 
    175190  </define> 
     
    244259     <element name="output"> 
    245260       <ref name="UI.Common" /> 
     261       <ref name="UI.attr.defaultvalue" /> 
    246262       <!--<optional><attribute name="value"/></optional>--> 
    247263     </element> 
  • trunk/lib/jelix/docs/ns/jforms-controls.rng

    r493 r504  
    3333      <optional><attribute name="required"><choice><value>true</value><value>false</value></choice></attribute></optional> 
    3434  </define> 
     35  <define name="UI.attr.defaultvalue"> 
     36      <optional><attribute name="defaultvalue" /></optional> 
     37  </define> 
     38   
    3539   
    3640  <define name="List.UI.Common"> 
     
    4347        <ref name="daodatasource" /> 
    4448    </choice> 
     49    <optional> 
     50        <choice> 
     51            <attribute name="selectedvalue" /> 
     52            <element name="selectedvalues"> 
     53                <oneOrMore> 
     54                <element name="value"><text/></element> 
     55                </oneOrMore> 
     56            </element> 
     57        </choice> 
     58    </optional> 
    4559  </define> 
    4660   
     
    5367             </choice> 
    5468         </optional> 
     69         <optional><attribute name="selected"><choice><value>true</value><value>false</value></choice></attribute></optional> 
    5570         <attribute name="value" /> 
    5671     </element> 
     
    100115      </attribute> 
    101116  </define> 
    102    
    103  
    104  
    105117 
    106118  <!--  Controls       --> 
     
    133145       <ref name="UI.Common" /> 
    134146       <ref name="UI.Common.data" /> 
     147       <ref name="UI.attr.defaultvalue" /> 
    135148     </element> 
    136149  </define> 
     
    141154       <ref name="UI.attr.readonly" /> 
    142155       <ref name="UI.attr.required" /> 
     156       <ref name="UI.attr.defaultvalue" /> 
    143157     </element> 
    144158  </define> 
     
    172186       <ref name="UI.Common" /> 
    173187       <ref name="UI.attr.readonly" /> 
     188       <ref name="UI.attr.defaultvalue" /> 
    174189     </element> 
    175190  </define> 
     
    244259     <element name="output"> 
    245260       <ref name="UI.Common" /> 
     261       <ref name="UI.attr.defaultvalue" /> 
    246262       <!--<optional><attribute name="value"/></optional>--> 
    247263     </element> 
  • trunk/lib/jelix/forms/jFormsBase.class.php

    r483 r504  
    6969        foreach($this->_controls as $name=>$ctrl){ 
    7070            $value = $req->getParam($name); 
    71             //if($value !== null) on commente pour le moment, 
    7271            //@todo à prevoir un meilleur test, pour les formulaires sur plusieurs pages 
    7372            if($value === null) $value=''; 
     
    7574        } 
    7675    } 
    77  
    7876 
    7977    /** 
     
    9896            if($value === null && $ctrl->required){ 
    9997                $this->_container->errors[$name]=JFORM_ERRDATA_REQUIRED; 
    100             }elseif(!$ctrl->datatype->check($value)){ 
    101                 $this->_container->errors[$name]=JFORM_ERRDATA_INVALID; 
     98            }else{ 
     99                if(is_array($value)){ 
     100                    foreach($value as $v){ 
     101                        if(!$ctrl->datatype->check($v)){ 
     102                            $this->_container->errors[$name]=JFORM_ERRDATA_INVALID; 
     103                            break; 
     104                        } 
     105                    } 
     106                }elseif(!$ctrl->datatype->check($value)){ 
     107                    $this->_container->errors[$name]=JFORM_ERRDATA_INVALID; 
     108                } 
    102109            } 
    103110        } 
     
    116123            return new Exception('formId is invalid, couldn\'t get the corresponding dao object'); 
    117124        foreach($this->_controls as $name=>$ctrl){ 
    118             $this->_container->datas[$name] = $daorec->$name; 
     125            if(isset($daorec->$name)) 
     126                $this->_container->datas[$name] = $daorec->$name; 
    119127        } 
    120128    } 
     
    131139        $daorec = jDao::createRecord($daoSelector); 
    132140        foreach($this->_controls as $name=>$ctrl){ 
    133             $daorec->$name = $this->_container->datas[$name]; 
     141            if(is_array($this->_container->datas[$name])){ 
     142                if( count ($this->_container->datas[$name]) ==1){ 
     143                    $daorec->$name = $this->_container->datas[$name][0]; 
     144                }else{ 
     145                    // do nothing for arrays ? 
     146                } 
     147            }else 
     148                $daorec->$name = $this->_container->datas[$name]; 
    134149        } 
    135150        if($this->_container->formId){ 
     
    137152            $dao->update($daorec); 
    138153        }else{ 
     154            // todo : what about updating the formId with the Pk. 
    139155            $dao->insert($daorec); 
    140156        } 
    141157        return $daorec->getPk(); 
     158    } 
     159 
     160    /** 
     161     * set datas from a DAO, in a control 
     162     *  
     163     * The control must be a container like checkboxes or listbox with multiple attribute. 
     164     * The form should contain a formId 
     165     * 
     166     * The Dao should map to an "association table" : its primary key should be 
     167     * the primary key stored in the formId + the field which will contain one of  
     168     * the values of the control. If this order is not the same as defined into the dao, 
     169     * you should provide the list of property names which corresponds to the primary key 
     170     * in this order : properties for the formId, followed by the property which contains  
     171     * the value. 
     172     * @param string $controlName  the name of the control 
     173     * @param string $daoSelector the selector of a dao file 
     174     * @param mixed  $primaryKeyNames list of field corresponding to primary keys (optional) 
     175     * @see jDao 
     176     */ 
     177    public function initControlFromDao($controlName, $daoSelector, $primaryKeyNames=null){ 
     178 
     179        if(!$this->_controls[$controlName]->isContainer()){ 
     180            throw new Exception("jFormsBase::initControlFromDao: the control $controlName is not a container"); 
     181        } 
     182 
     183        if(!$this->_container->formId) 
     184            throw new Exception("jFormsBase::initControlFromDao: no primary key"); 
     185 
     186        $primaryKey = $this->_container->formId; 
     187 
     188        if(!is_array($primaryKey)) 
     189            $primaryKey =array($primaryKey); 
     190 
     191        $dao = jDao::create($daoSelector); 
     192        $daorec = jDao::createRecord($daoSelector); 
     193 
     194        $conditions = jDao::createConditions(); 
     195        if($primaryKeyNames) 
     196            $pkNamelist = $primaryKeyNames; 
     197        else 
     198            $pkNamelist = $daorec->getPrimaryKeyNames(); 
     199 
     200        foreach($primaryKey as $k=>$pk){ 
     201            $conditions->addCondition ($pkNamelist[$k], '=', $pk); 
     202        } 
     203 
     204        $results = $dao->findBy($conditions); 
     205        $valuefield = $pkNamelist[$k+1]; 
     206        $val = array(); 
     207        foreach($results as $res){ 
     208            $val[]=$res->$valuefield; 
     209        } 
     210        $this->_container->datas[$controlName]=$val; 
     211    } 
     212 
     213 
     214    /** 
     215     * save datas of a control using a dao. 
     216     * 
     217     * The control must be a container like checkboxes or listbox with multiple attribute. 
     218     * If the form contain a new record (no formId), you should call saveToDao before 
     219     * in order to get a new id (the primary key of the new record), or you should get a new id  
     220     * by an other way. then you must pass this primary key in the third argument. 
     221     * If the form have already a formId, then it will be used as a primary key, unless 
     222     * you give one in the third argument. 
     223     * 
     224     * The Dao should map to an "association table" : its primary key should be 
     225     * the primary key stored in the formId + the field which will contain one of  
     226     * the values of the control. If this order is not the same as defined into the dao, 
     227     * you should provide the list of property names which corresponds to the primary key 
     228     * in this order : properties for the formId, followed by the property which contains  
     229     * the value. 
     230     * All existing records which have the formid in their keys are deleted 
     231     * before to insert new values. 
     232     * 
     233     * @param string $controlName  the name of the control 
     234     * @param string $daoSelector the selector of a dao file 
     235     * @param mixed  $primaryKey the primary key if the form have no id. (optional) 
     236     * @param mixed  $primaryKeyNames list of field corresponding to primary keys (optional) 
     237     * @see jDao 
     238     */ 
     239    public function saveControlToDao($controlName, $daoSelector, $primaryKey = null, $primaryKeyNames=null){ 
     240 
     241        if(!$this->_controls[$controlName]->isContainer()){ 
     242            throw new Exception("jFormsBase::saveControlToDao: the control $controlName is not a container"); 
     243        } 
     244 
     245        $values = $this->_container->datas[$controlName]; 
     246        if(!is_array($values)) 
     247            throw new Exception("jFormsBase::saveControlToDao: value of the control $controlName is not an array !"); 
     248 
     249        if(!$this->_container->formId && !$primaryKey) 
     250            throw new Exception("jFormsBase::saveControlToDao: no primary key"); 
     251 
     252        if(!$primaryKey) 
     253            $primaryKey = $this->_container->formId; 
     254 
     255        if(!is_array($primaryKey)) 
     256            $primaryKey =array($primaryKey); 
     257 
     258        $dao = jDao::create($daoSelector); 
     259        $daorec = jDao::createRecord($daoSelector); 
     260 
     261        $conditions = jDao::createConditions(); 
     262        if($primaryKeyNames) 
     263            $pkNamelist = $primaryKeyNames; 
     264        else 
     265            $pkNamelist = $daorec->getPrimaryKeyNames(); 
     266 
     267        foreach($primaryKey as $k=>$pk){ 
     268            $conditions->addCondition ($pkNamelist[$k], '=', $pk); 
     269            $daorec->{$pkNamelist[$k]} = $pk; 
     270        } 
     271 
     272        $dao->deleteBy($conditions); 
     273 
     274        $valuefield = $pkNamelist[$k+1]; 
     275        foreach($values as $value){ 
     276            $daorec->$valuefield = $value; 
     277            $dao->insert($daorec); 
     278        } 
    142279    } 
    143280 
  • trunk/lib/jelix/forms/jFormsBase.class.php

    r483 r504  
    6969        foreach($this->_controls as $name=>$ctrl){ 
    7070            $value = $req->getParam($name); 
    71             //if($value !== null) on commente pour le moment, 
    7271            //@todo à prevoir un meilleur test, pour les formulaires sur plusieurs pages 
    7372            if($value === null) $value=''; 
     
    7574        } 
    7675    } 
    77  
    7876 
    7977    /** 
     
    9896            if($value === null && $ctrl->required){ 
    9997                $this->_container->errors[$name]=JFORM_ERRDATA_REQUIRED; 
    100             }elseif(!$ctrl->datatype->check($value)){ 
    101                 $this->_container->errors[$name]=JFORM_ERRDATA_INVALID; 
     98            }else{ 
     99                if(is_array($value)){ 
     100                    foreach($value as $v){ 
     101                        if(!$ctrl->datatype->check($v)){ 
     102                            $this->_container->errors[$name]=JFORM_ERRDATA_INVALID; 
     103                            break; 
     104                        } 
     105                    } 
     106                }elseif(!$ctrl->datatype->check($value)){ 
     107                    $this->_container->errors[$name]=JFORM_ERRDATA_INVALID; 
     108                } 
    102109            } 
    103110        } 
     
    116123            return new Exception('formId is invalid, couldn\'t get the corresponding dao object'); 
    117124        foreach($this->_controls as $name=>$ctrl){ 
    118             $this->_container->datas[$name] = $daorec->$name; 
     125            if(isset($daorec->$name)) 
     126                $this->_container->datas[$name] = $daorec->$name; 
    119127        } 
    120128    } 
     
    131139        $daorec = jDao::createRecord($daoSelector); 
    132140        foreach($this->_controls as $name=>$ctrl){ 
    133             $daorec->$name = $this->_container->datas[$name]; 
     141            if(is_array($this->_container->datas[$name])){ 
     142                if( count ($this->_container->datas[$name]) ==1){ 
     143                    $daorec->$name = $this->_container->datas[$name][0]; 
     144                }else{ 
     145                    // do nothing for arrays ? 
     146                } 
     147            }else 
     148                $daorec->$name = $this->_container->datas[$name]; 
    134149        } 
    135150        if($this->_container->formId){ 
     
    137152            $dao->update($daorec); 
    138153        }else{ 
     154            // todo : what about updating the formId with the Pk. 
    139155            $dao->insert($daorec); 
    140156        } 
    141157        return $daorec->getPk(); 
     158    } 
     159 
     160    /** 
     161     * set datas from a DAO, in a control 
     162     *  
     163     * The control must be a container like checkboxes or listbox with multiple attribute. 
     164     * The form should contain a formId 
     165     * 
     166     * The Dao should map to an "association table" : its primary key should be 
     167     * the primary key stored in the formId + the field which will contain one of  
     168     * the values of the control. If this order is not the same as defined into the dao, 
     169     * you should provide the list of property names which corresponds to the primary key 
     170     * in this order : properties for the formId, followed by the property which contains  
     171     * the value. 
     172     * @param string $controlName  the name of the control 
     173     * @param string $daoSelector the selector of a dao file 
     174     * @param mixed  $primaryKeyNames list of field corresponding to primary keys (optional) 
     175     * @see jDao 
     176     */ 
     177    public function initControlFromDao($controlName, $daoSelector, $primaryKeyNames=null){ 
     178 
     179        if(!$this->_controls[$controlName]->isContainer()){ 
     180            throw new Exception("jFormsBase::initControlFromDao: the control $controlName is not a container"); 
     181        } 
     182 
     183        if(!$this->_container->formId) 
     184            throw new Exception("jFormsBase::initControlFromDao: no primary key"); 
     185 
     186        $primaryKey = $this->_container->formId; 
     187 
     188        if(!is_array($primaryKey)) 
     189            $primaryKey =array($primaryKey); 
     190 
     191        $dao = jDao::create($daoSelector); 
     192        $daorec = jDao::createRecord($daoSelector); 
     193 
     194        $conditions = jDao::createConditions(); 
     195        if($primaryKeyNames) 
     196            $pkNamelist = $primaryKeyNames; 
     197        else 
     198            $pkNamelist = $daorec->getPrimaryKeyNames(); 
     199 
     200        foreach($primaryKey as $k=>$pk){ 
     201            $conditions->addCondition ($pkNamelist[$k], '=', $pk); 
     202        } 
     203 
     204        $results = $dao->findBy($conditions); 
     205        $valuefield = $pkNamelist[$k+1]; 
     206