1
irmtfan
How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/2/2 14:59

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


In the middle of working on a new newbb block system I stuck in a simple obstacle:
I need to add a js statement to onsubmit event of blockform.
I try to add something like this:
$statusEleName = $statusEle->getName();
$statusEle->customValidationCode[] = "if ( myform.{$statusEleName}.value == '' ) { myform.{$statusEleName}.value = 'all';}";
but it never rendered.
there is a new blockform class in 2.6 but it failed to render js codes too.
see in XOOPS26/class/xoopsform/blockform.php
<?php
/*
 You may not change or alter any portion of this comment or credits
 of supporting developers from this source code or any supporting source code
 which is considered copyrighted (c) material of the original comment or credit authors.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

/**
 * XOOPS Block Form
 *
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package         class
 * @subpackage      xoopsform
 * @since           2.6.0
 * @author          trabis <lusopoemas@gmail.com>
 * @version         $Id: blockform.php 10942 2013-01-29 11:58:33Z dugris $
 */

defined('XOOPS_ROOT_PATH') or die('Restricted access');


/**
 * Form that will output formatted as a HTML table
 *
 * No styles and no JavaScript to check for required fields.
 */
class XoopsBlockForm extends XoopsForm
{
    public function 
__construct()
    {
        
parent::__construct('''''');
    }

    
/**
     * @return string
     */
    
public function render()
    {
        
$ret '';
        
/* @var $ele XoopsFormElement */
        
foreach ($this->getElements() as $ele) {
            if (!
$ele->isHidden()) {
                
$ret .= "<strong>" $ele->getCaption()."</strong>";
                
$ret .= "&nbsp;&nbsp;" $ele->render() . "";
                
$ret .= "&nbsp;&nbsp;<i>" $ele->getDescription() . "</i><br /><br />";
            } else {
                
$ret .= $ele->render();
            }
        }
        
$ret .= '';
        return 
$ret;
    }
}

If i use a separate nest form inside blockform it show codes but it seems it is useless in a nested form.
see:
<script type='text/javascript'>
<!--
//
function xoopsFormValidate_list_block() { var myform window.document.list_block; if ( myform.options[0].value == '' ) { myform.options[0].value 'all';}return true;
}
//--></script>


It seems there is no easy way to add js in a block form.

2
irmtfan
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/2/3 7:59

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


I found that it is a serious bug in both xoops core and modules multi-select elements in forms.
There is not any validation to prevent the form return on submit when user dont select any option (the value is null).
The issue is in XoopsFormSelect class when multiple selections is allowed.
Also i find it in some recent modules like publisher blocks in category select box element.
It is a serious issue.
Anybody have the best solution for it?

3
irmtfan
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/2/4 2:48

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


ok I end up with a js solution.

$statusEle->setExtra("onchange = "if ( this.value == '' ) {this.value 'all';}";"); // if user dont select any it select "all"


It will prevent user to select nothing.
IMO this solution is good right now.

But what we can do for a "XoopsFormCheckBox"? I mean when user dont select any option.


4
irmtfan
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/2/5 7:11

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


I enhance this solution and used it in newbb.
/**
 * Newbb Javascript Validation functions
 *
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @module             newbb
 * @since           4.3
 * @author          irmtfan
 * @version         $Id$
 */

/**
* Function for validation of xoops forms: prevent user select nothing or disable some options
* @param elName : elements name
* @param elType : element type eg: select, checkbox
* @param prevent: prevent user select nothing: true or false
* @param disablecat: disable categories in forum select box: true or false
* @param elMsg: the message
*/


function validate(elNameelType preventdisablecatelMsg)
{
    var 
0;
    var 
el=document.getElementsByName(elName);
    var 
is_valid true;
    switch (
elType) {
        case 
'checkbox':
            var 
hasChecked false;
            if (
el.length) {
                for (
0el.lengthi++) {
                    if (
el[i].checked == true) {
                        
hasChecked true;
                        break;
                    }
                }
            } else {
                if (
el.checked == true) {
                    
hasChecked true;
                }
            }
            if (!
hasChecked) {
                if (
el.length) {
                    if (
prevent) {el[0].checked true;}
                    
el[0].focus();
                }else{
                    if (
prevent) {el.checked true;}
                    
el.focus();            
                }
                
is_valid false;
            }
            break;
        case 
'select':
            
el el[0];
            if (
disablecat) {
                for (
0el.options.lengthi++ ) {
                    if (
el.options[i].value 0) {
                        
el.options[i].disabled  true;
                        
el.options[i].value '';                        
                    }
                }                
            }
            
            if (
el.value == '') {
                
is_valid false;
                if(
prevent) {
                    for (
0el.options.lengthi++ ) {
                        if (
el.options[i].value != '') {
                            
el.value el.options[i].value;
                            break; 
// loop exit
                        
}
                    }
                }
            }
            break;
    }
    return 
is_valid;
}

but it need to be addressed iby the core team in 2.6

The main issue is:
we cannot add customValidationCode to xoopsFormValidate_blockform

Also we need to enhance the xoopsform class to accept all properties.

eg we need this function for set and get in selectbox form.

set function:
function setExtraInOption($extra$replace false)
    {
        if (
$replace) {
            
$this->_extraoption = array(trim($extra));
        } else {
            
$this->_extraoption[] = trim($extra);
        }
        return 
$this->_extraoption;
    }

get function:
function getExtraInOption($encode false)
    {
        if (!
$encode) {
            return 
' ' implode(' '$this->_extraoption);
        }
        
$value = array();
        foreach (
$this->_extraoption as $val) {
            
$value[] = str_replace('>''>'str_replace('<''<'$val));
        }
        return empty(
$value) ? '' ' ' implode(' '$value);
    }


then in render function we have this:
function render()
    {
        
$ele_name $this->getName();
        
$ele_title $this->getTitle();
        
$ele_value $this->getValue();
        
$ele_options $this->getOptions();
        
$ret '<select size="' $this->getSize() . '"' $this->getExtra();
        if (
$this->isMultiple() != false) {
            
$ret .= ' name="' $ele_name '[]" id="' $ele_name '" title="'$ele_title'" multiple="multiple">' ;
        } else {
            
$ret .= ' name="' $ele_name '" id="' $ele_name '" title="'$ele_title'">' ;
        }
        foreach(
$ele_options as $value => $name) {
            
$ret .= '<option value="' htmlspecialchars($valueENT_QUOTES) . '"';
            
$ret .= $this->getExtraInOption(); // irmtfan
            
if (count($ele_value) > && in_array($value$ele_value)) {
                
$ret .= ' selected="selected"';
            }
            
$ret .= '>' $name '</option>' ;
        }
        
$ret .= '</select>';
        return 
$ret;
    }


then we can use it like this:
// type element
    
$typeEle = new XoopsFormSelect(_MD_NEWBB_TYPE'options[2]'$options[2]);
    
$typeEle->addOption(0_NONE);
    
$typeEle->setExtraInOption("disabled = true"true);


5
Mamba
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/2/5 7:45

  • Mamba

  • Moderator

  • Posts: 11366

  • Since: 2004/4/23


Quote:
but it need to be addressed iby the core team in 2.6

Can you please add it to Bugs or Feature Requests in SourceForge, so we can keep track of this?

As always, please post there the link to this post, and then post the link to the request here, so it's easier to follow-up.
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

6
irmtfan
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/2/6 1:54

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


Quote:
please add it to Bugs or Feature Requests in SourceForge


Yes. It was my intention too. but I always prefer to discuss here first because it will bright up the issue more.
actually there is one bug and at least one feature to add.
The bug is serious.
XoopsForm classes cannot add anything to blockform
It means a module developer cannot add anything to blockform.

also the feature request.
More set and get functions in XoopsForm Classes

maybe we can have another feature request for accepting more functionality and pre-defined js codes in XoopsForm for validation? anybody have any suggestion?

7
alain91
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/4/6 19:14

  • alain91

  • Just popping in

  • Posts: 8

  • Since: 2012/11/11


@irmtfan

could you test the following modification in your configuration ?

Just add the 2 lines starting with @

Indexblockform.php
===================================================================
--- 
blockform.php    (revision 624)
+++ 
blockform.php    (working copy)
@@ -
51,+51,@@
             }
         }
         
$ret .= '';
    @+ 
// next line added
        
@+  $ret .= $this->renderValidationJS(true);
         return 
$ret;
     }
 }

then it would be possible to use for each element added to blockform either customValidationCode[] or either isRequired()


8
irmtfan
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/4/7 4:55

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


the root of this problem is we cannot add any element to $form defined in system module.
see in
xoops26\modules\system\class\form\block.php
and
xoops255\modules\system\class\block.php
in function getForm around line 138 ( in xoops255)
if ( $this->getOptions() != false ) {
                
$form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_OPTIONS$this->getOptions()));
            } else {
                
$form->addElement(new XoopsFormHidden('options'$this->getVar('options')));
            }


so it used getOptions function and a nested form is created.
i still cannot find any good solution to access to $form

9
alain91
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/4/7 13:08

  • alain91

  • Just popping in

  • Posts: 8

  • Since: 2012/11/11


I don't think it is the final solution, but at the moment, in 2.6.0 alpha2 there is a file named modules/system/blocks/system_blocks.php
Inside there are 2 kind of specific functions b_xxx_show and b_xxx_edit. The former is for display specific content of a block and the later is for editing.

I have modified the b_system_info_edit

function b_system_info_edit($options)
{
    
$block_form = new XoopsBlockForm();
    
$block_form->addElement( new XoopsFormText(_MB_SYSTEM_PWWIDTH'options[0]'13$options[0]), true);
    
$block_form->addElement( new XoopsFormText(_MB_SYSTEM_PWHEIGHT'options[1]'13$options[1]), true);
    
$block_form->addElement( new XoopsFormText(sprintf(_MB_SYSTEM_LOGOXOOPS_URL "/images/"), 'options[2]'5100$options[2]), true);
    
$block_form->addElement(new XoopsFormRadioYN(_MB_SYSTEM_SADMIN'options[3]'$options[3]));
    return 
$block_form->render();
}
which is the code found in main.php block


As You see we can add specific elements and with the parameter 'true' in addElement we can set Rquired to true.

Then with my modification in the render method in blockform.php. The specific fields are displayed and in the html file there are javascript scripts to check if the fields are empty and display an erreur message, in the option part of the form.

I hope this could help.


10
irmtfan
Re: How to add a customValidationCode to xoopsFormValidate_blockform
  • 2013/4/8 4:38

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


Sorry my bad.
I didnt test "is required" in 2.6 because my main problem was adding some js codes as customValidationCode.

Now i see you are right and required is working in 2.6
I still cannot find why it is working in 2.6 but not in 2.5.5
for example see publisher
In 2.6 "is required" in publisher blocks worked
In 2.5.5 publisher module by using the same form for blocks "is required" is not working.

but it is not important for me.
the main bug still remain.
adding a customValidationCode to xoopsFormValidate_blockform is not possible.
try to change your code by this:
function b_system_info_edit($options)
{
    
$block_form = new XoopsBlockForm();
    
$dispEle = new XoopsFormText(SystemLocale::POPUP_WINDOW_WIDTH'options[0]'13$options[0]);
    
$dispEle->customValidationCode[]="alert('this is a test');";
    
$block_form->addElement($dispEle true);
    
$block_form->addElement( new XoopsFormText(SystemLocale::POPUP_WINDOW_HEIGHT'options[1]'13$options[1]), true);
    
$block_form->addElement( new XoopsFormText(sprintf(SystemLocale::F_LOGO_IMAGE_FILE_IS_LOCATED_UNDERXOOPS_URL "/images/"), 'options[2]'5100$options[2]), true);
    
$block_form->addElement(new XoopsFormRadioYN(SystemLocale::SHOW_ADMIN_GROUPS'options[3]'$options[3]));
    return 
$block_form->render();
}


js codes will not add to function xoopsFormValidate_blockform()

edit:
search function xoopsFormValidate in the source code page and you can see these codes will be added to
function xoopsFormValidate_() which is the main bug because this is the nested form.(another form inside the blockform)
I hope you understand me.

Login

Who's Online

157 user(s) are online (95 user(s) are browsing Support Forums)


Members: 0


Guests: 157


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits