xoops forums

tonyboy

Just popping in
Posted on: 2002/6/27 14:51
tonyboy
tonyboy (Show more)
Just popping in
Posts: 1
Since: 2002/5/30
#1

Class XoopsThemeForm

Hello,

I am creating my module in which there is a form with scrollbar list.
The xoopsFormValidate function test an empty string with the operator == \"\".
how to do to test the backspace in the string ?
The xoopsFromValidate is defined inside class XoopsThemeForm.
An idea ?
Best regards

Tonyboy

onokazu

XOOPS Founder
Posted on: 2002/6/28 3:45
onokazu
onokazu (Show more)
XOOPS Founder
Posts: 617
Since: 2001/12/13
#2

Re: Class XoopsThemeForm

xoopsFormValidate function is a javascript function.
It is used when the form is actually displayed.

zer0fill

Not too shy to talk
Posted on: 2004/8/8 4:06
zer0fill
zer0fill (Show more)
Not too shy to talk
Posts: 137
Since: 2003/12/2
#3

Re: Class XoopsThemeForm

is there an option to do a server-side check than having us do our own validation?

ex
FROM
if (!empty($_POST['email']) && !empty($_POST['title']) && !empty($_POST['address']))
{
 
// save to db
}
else
{
 
$errors = array();
 if (empty(
$_POST['email']) {$errors[] = 'Missing Email<br />';}
 if (empty(
$_POST['title']) {$errors[] = 'Missing Title<br />';}
 if (empty(
$_POST['address']) {$errors[] = 'Missing Address<br />';}
 
$xoopsTpl->assign('errors',$errors);
}

TO
if ($form->validate($_POST)) // or $_GET wherever the data is coming from
{
 
// save to db
}
else
{
 
$xoopsTpl->assign('errors',$form->errors());
}


We would have all the form assignments (addElement()) at the top

skalpa

Quite a regular
Posted on: 2004/8/8 8:34
skalpa
skalpa (Show more)
Quite a regular
Posts: 300
Since: 2003/4/16
#4

Re: Class XoopsThemeForm

Not yet, but I have already redone all this layer in the CVS repository, and it will be possible in 2.1+
$form->loadValues$_POST );
if (
$form->validate()) {


Not far...

skalpa.>

zer0fill

Not too shy to talk
Posted on: 2004/8/8 9:43
zer0fill
zer0fill (Show more)
Not too shy to talk
Posts: 137
Since: 2003/12/2
#5

Re: Class XoopsThemeForm

Thanks skalpa. For those who just can't wait, I've developed my own (simplistic) validation function

Source
/**           
* @author zer0fill, Sun Aug 08 02:34:28 PDT 2004       
* @desc Validates required input fields. Returns a preformatted string if there's an error     
* @return string|null        
* @param array $required (key=name / value=displayed error)         
*/ 
function validate($required)
{
    
// prep the error message, if any    
    
$message ='';
    
// setup a loop on each required field than doing it manually line by line    
    
foreach ($required as $key=>$item)
    {
      
// find the missing required fields (javascript disabled on user's end)
      
if (empty($_POST[$key])) {$message .= $item.', ';}
    }

    
// A missing field was detected
    
if (!empty($message))
    {
      
// setup the error message and remove the trailing `, `
      
$message '<div class="errorMsg">Missing Required Field(s): '.substr($message,0,-2).'</div>';
      return 
$message;
    }
}


Example
$required = array( 'office_id'=>'Office'
                      
,'role_id'=>'Role'
                      
,'type_id'=>'Type'
                      
,'title'=>'Title'
                      
,'fname'=>'First Name'
                      
,'lname'=>'Last Name'
                      
);
    
$errors validate($required);
    if (empty(
$errors))
    {
      
// save to db
    
}
    else
    {
      
$xoopsTpl->assign('errors',$errors);
    }


Output
<div class="errorMsg">Missing Required Field(s): OfficeRoleTypeTitleFirst NameLast Name</div>


BTW, did you also make regex checks like proper phone format inputs, email, at least x chars, etc (kinda like PEAR's $form->addRule()) ?

skalpa

Quite a regular
Posted on: 2004/8/8 10:39
skalpa
skalpa (Show more)
Quite a regular
Posts: 300
Since: 2003/4/16
#6

Re: Class XoopsThemeForm

Yep...
There's already Required / Length / Regexp, and you can make your own validators.
In fact, I could say our framework will be some kind of a mix between the 2.0 classes and PEAR::QuickForm (at least feature-wise, I recoded everything as I think both overuse get/set methods, which I dont like considering we're using PHP).
- You'll be able to modify ALL the elements attributes
- There's the validators stuff implemented
- I'm thinking about adding some of the QF custom elements, although this hasn't been done yet (@ least the "Advanced checkbox", and maybe others, like the "autocomplete" input control)
- You'll be able to attach an object to a form (well, the reality is going to be a bit longer than the following sample, but I think you'll get the point)

$form->attachObject( &$source );
$form->loadValues$_REQUEST );
if ( 
$form->validate(true) ) {
    
$handler->update$source );
}


skalpa.>

gordman

Just popping in
Posted on: 2007/9/14 15:02
gordman
gordman (Show more)
Just popping in
Posts: 11
Since: 2007/9/7 1
#7

Re: Class XoopsThemeForm

Well it looks like you are handling it alright, i haven't tried it yet but I have your post as a tutorial. Thanks! You really helped.


Fake College Degree