Changeset 1069 for trunk/lib

Show
Ignore:
Timestamp:
08/27/08 23:57:27 (3 months ago)
Author:
laurentj
Message:

jforms: fixed bug on javascript validation, and reduced generated js code size

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/jelix-www/js/jforms.js

    r1054 r1069  
    1717 
    1818// declare a form control 
    19 jForms.tControl = new jFormsControl('name', 'a label', 'datatype'); 
    20 jForms.tControl.required = true; 
    21 jForms.tControl.errInvalid=''; 
    22 jForms.tControl.errRequired=''; 
    23 jForms.tForm.addControl( gControl); 
     19var c = new jFormsControl('name', 'a label', 'datatype'); 
     20c.required = true; 
     21c.errInvalid=''; 
     22c.errRequired=''; 
     23jForms.tForm.addControl(c); 
    2424... 
    2525 
    26 // declare the form now 
     26// declare the form now. A 'submit" event handler will be attached to the corresponding form element 
    2727jForms.declareForm(jForms.tForm); 
    28  
    29 //On a form tag, you should add this onsubmit attribute : 
    30 onsubmit="return jForms.verifyForm(this)" 
    3128 
    3229*/ 
     
    3936 
    4037    tForm: null, 
    41     tControl: null, 
    42     tControl2: null, 
    4338    frmElt: null, 
    4439 
     
    611606 
    612607        var list = this.items[val]; 
     608        var valid = true; 
    613609        for(var i=0; i < list.length; i++) { 
    614610            var val2 = jfrm.getValue(jfrm.frmElt.elements[list[i].name]); 
     
    617613                if (list[i].required) { 
    618614                    jfrm.tForm.errorDecorator.addError(list[i], 1); 
     615                    valid = false; 
    619616                } 
    620617            } else if (!list[i].check(val2, jfrm)) { 
    621618                jfrm.tForm.errorDecorator.addError(list[i], 2); 
    622             } 
    623         } 
    624         return true; 
     619                valid = false; 
     620            } 
     621        } 
     622        return valid; 
    625623    }, 
    626624    activate : function (val) { 
  • trunk/lib/jelix/plugins/jforms/html/html.jformsbuilder.php

    r1067 r1069  
    158158        echo '<script type="text/javascript"> 
    159159//<![CDATA[ 
     160(function(){var c, c2; 
    160161'.$this->jsContent.' 
     162})(); 
    161163//]]> 
    162164</script>'; 
     
    192194    } 
    193195 
     196    protected function escJsStr($str) { 
     197        return '\''.str_replace(array("'","\n"),array("\\'", "\\n"), $str).'\''; 
     198    } 
     199 
    194200    protected function commonJs($ctrl) { 
    195201        if($ctrl->help){ 
    196             $this->jsContent .="jForms.tControl.help='".str_replace("'","\\'",$ctrl->help)."';\n"; 
     202            $this->jsContent .="c.help=".$this->escJsStr($ctrl->help).";\n"; 
    197203        } 
    198204 
    199205        if($ctrl->required){ 
    200             $this->jsContent .="jForms.tControl.required = true;\n"; 
     206            $this->jsContent .="c.required = true;\n"; 
    201207            if($ctrl->alertRequired){ 
    202                 $this->jsContent .="jForms.tControl.errRequired='".str_replace("'","\\'",$ctrl->alertRequired)."';\n"; 
     208                $this->jsContent .="c.errRequired=".$this->escJsStr($ctrl->alertRequired).";\n"; 
    203209            } 
    204210            else { 
    205                 $this->jsContent .="jForms.tControl.errRequired='".str_replace("'","\\'",jLocale::get('jelix~formserr.js.err.required', $ctrl->label))."';\n"; 
     211                $this->jsContent .="c.errRequired=".$this->escJsStr(jLocale::get('jelix~formserr.js.err.required', $ctrl->label)).";\n"; 
    206212            } 
    207213        } 
    208214 
    209215        if($ctrl->alertInvalid){ 
    210             $this->jsContent .="jForms.tControl.errInvalid='".str_replace("'","\\'",$ctrl->alertInvalid)."';\n"; 
     216            $this->jsContent .="c.errInvalid=".$this->escJsStr($ctrl->alertInvalid).";\n"; 
    211217        } 
    212218        else { 
    213             $this->jsContent .="jForms.tControl.errInvalid='".str_replace("'","\\'",jLocale::get('jelix~formserr.js.err.invalid', $ctrl->label))."';\n"; 
    214         } 
    215  
    216         if ($this->isRootControl) $this->jsContent .="jForms.tForm.addControl(jForms.tControl);\n"; 
     219            $this->jsContent .="c.errInvalid=".$this->escJsStr(jLocale::get('jelix~formserr.js.err.invalid', $ctrl->label)).";\n"; 
     220        } 
     221 
     222        if ($this->isRootControl) $this->jsContent .="jForms.tForm.addControl(c);\n"; 
    217223    } 
    218224 
     
    245251            $dt = 'String'; 
    246252 
    247         $this->jsContent .="jForms.tControl = new jFormsControl".$dt."('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     253        $this->jsContent .="c = new jFormsControl".$dt."('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    248254        if ($isLocale) 
    249             $this->jsContent .="jForms.tControl.lang='".$GLOBALS['gJConfig']->locale."';\n"; 
     255            $this->jsContent .="c.lang='".$GLOBALS['gJConfig']->locale."';\n"; 
    250256 
    251257        $maxl= $ctrl->datatype->getFacet('maxLength'); 
    252258        if($maxl !== null) 
    253             $this->jsContent .="jForms.tControl.maxLength = '$maxl';\n"; 
     259            $this->jsContent .="c.maxLength = '$maxl';\n"; 
    254260 
    255261        $minl= $ctrl->datatype->getFacet('minLength'); 
    256262        if($minl !== null) 
    257             $this->jsContent .="jForms.tControl.minLength = '$minl';\n"; 
     263            $this->jsContent .="c.minLength = '$minl';\n"; 
    258264 
    259265        $this->commonJs($ctrl); 
     
    273279    protected function jsCheckbox($ctrl) { 
    274280 
    275         $this->jsContent .="jForms.tControl = new jFormsControlBoolean('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     281        $this->jsContent .="c = new jFormsControlBoolean('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    276282 
    277283        $this->commonJs($ctrl); 
     
    309315    protected function jsCheckboxes($ctrl) { 
    310316 
    311         $this->jsContent .="jForms.tControl = new jFormsControlString('".$ctrl->ref."[]', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     317        $this->jsContent .="c = new jFormsControlString('".$ctrl->ref."[]', ".$this->escJsStr($ctrl->label).");\n"; 
    312318 
    313319        $this->commonJs($ctrl); 
     
    334340    protected function jsRadiobuttons($ctrl) { 
    335341 
    336         $this->jsContent .="jForms.tControl = new jFormsControlString('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     342        $this->jsContent .="c = new jFormsControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    337343 
    338344        $this->commonJs($ctrl); 
     
    359365    protected function jsMenulist($ctrl) { 
    360366 
    361         $this->jsContent .="jForms.tControl = new jFormsControlString('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     367        $this->jsContent .="c = new jFormsControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    362368 
    363369        $this->commonJs($ctrl); 
     
    402408    protected function jsListbox($ctrl) { 
    403409        if($ctrl->multiple){ 
    404             $this->jsContent .= "jForms.tControl = new jFormsControlString('".$ctrl->ref."[]', '".str_replace("'","\'",$ctrl->label)."');\n"; 
    405             $this->jsContent .= "jForms.tControl.multiple = true;\n"; 
     410            $this->jsContent .= "c = new jFormsControlString('".$ctrl->ref."[]', ".$this->escJsStr($ctrl->label).");\n"; 
     411            $this->jsContent .= "c.multiple = true;\n"; 
    406412        } else { 
    407             $this->jsContent .="jForms.tControl = new jFormsControlString('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     413            $this->jsContent .="c = new jFormsControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    408414        } 
    409415 
     
    418424 
    419425    protected function jsTextarea($ctrl) { 
    420         $this->jsContent .="jForms.tControl = new jFormsControlString('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     426        $this->jsContent .="c = new jFormsControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    421427 
    422428        $maxl= $ctrl->datatype->getFacet('maxLength'); 
    423429        if($maxl !== null) 
    424             $this->jsContent .="jForms.tControl.maxLength = '$maxl';\n"; 
     430            $this->jsContent .="c.maxLength = '$maxl';\n"; 
    425431 
    426432        $minl= $ctrl->datatype->getFacet('minLength'); 
    427433        if($minl !== null) 
    428             $this->jsContent .="jForms.tControl.minLength = '$minl';\n"; 
     434            $this->jsContent .="c.minLength = '$minl';\n"; 
    429435 
    430436        $this->commonJs($ctrl); 
     
    464470    protected function jsSecretconfirm($ctrl) { 
    465471        // we assume that a secret confirm control is just after a secret control in the list of controls 
    466         $this->jsContent .= "jForms.tControl.confirmField = new jFormsControlSecretConfirm('".$ctrl->ref."_confirm', '".str_replace("'","\\'",$ctrl->label)."');\n"; 
     472        $this->jsContent .= "c.confirmField = new jFormsControlSecretConfirm('".$ctrl->ref."_confirm', ".$this->escJsStr($ctrl->label).");\n"; 
    467473    } 
    468474 
     
    485491 
    486492    protected function jsUpload($ctrl) { 
    487         $this->jsContent .="jForms.tControl = new jFormsControlString('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     493        $this->jsContent .="c = new jFormsControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    488494 
    489495        $this->commonJs($ctrl); 
     
    557563        $id=' name="'.$ctrl->ref.'" id="'.$this->_name.'_'.$ctrl->ref.'_'; 
    558564        $this->jsChoiceInternal($ctrl); 
    559         $this->jsContent .="jForms.tControl2 = jForms.tControl;\n"; 
     565        $this->jsContent .="c2 = c;\n"; 
    560566        $this->isRootControl = false; 
    561567        foreach( $ctrl->items as $itemName=>$listctrl){ 
     
    579585                if($ro) $c->setReadOnly(true); 
    580586                echo "</span>\n"; 
    581                 $this->jsContent .="jForms.tControl2.addControl(jForms.tControl, '".str_replace("'","\\'",$itemName)."');\n"; 
     587                $this->jsContent .="c2.addControl(c, ".$this->escJsStr($itemName).");\n"; 
    582588            } 
    583589            if(!$displayedControls) { 
    584                 $this->jsContent .="jForms.tControl2.items['".str_replace("'","\\'",$itemName)."']=[];\n"; 
     590                $this->jsContent .="c2.items[".$this->escJsStr($itemName)."]=[];\n"; 
    585591            } 
    586592 
     
    600606                $value=''; 
    601607        } 
    602         $this->jsContent .= "jForms.tControl2.activate('".$value."');\n"; 
     608        $this->jsContent .= "c2.activate('".$value."');\n"; 
    603609    } 
    604610 
    605611    protected function jsChoiceInternal($ctrl) { 
    606612 
    607         $this->jsContent .="jForms.tControl = new jFormsControlChoice('".$ctrl->ref."', '".str_replace("'","\'",$ctrl->label)."');\n"; 
     613        $this->jsContent .="c = new jFormsControlChoice('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n"; 
    608614 
    609615        $this->commonJs($ctrl); 
Download in other formats: Unified Diff Zip Archive