Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Trouble Functionalizing SmartyValidate

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
bkosick
Smarty n00b


Joined: 25 Aug 2006
Posts: 1

PostPosted: Fri Aug 25, 2006 2:18 pm    Post subject: Trouble Functionalizing SmartyValidate Reply with quote

Hi,

I'm having some problems make a smarty function to handle form validation. Basically if I write the SmartyValidate code directly in my php file, it works, but if I try to functionize it, it stops working, and I can't seem to figure it out....

Here's my php file with the code inline.... this works...
Code:

$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
switch($_action) {
    case 'addproduct':
        if (empty($_POST)) {
                SmartyValidate::connect($rmanagement->tpl, 'true');
                SmartyValidate::register_form('addproduct');
                SmartyValidate::register_validator('category_name', 'category_name', 'notEmpty', false, false, 'trim', 'addproduct');
                SmartyValidate::register_validator('version', 'version', 'notEmpty', false, false, 'trim', 'addproduct');
                SmartyValidate::register_validator('qa_version', 'qa_version', 'notEmpty', true, false, 'trim', 'addproduct');
                SmartyValidate::register_validator('code_name', 'code_name', 'notEmpty', false, false, 'trim', 'addproduct');

                //$rmanagement->ValidateFormSetup("$_action");
                $rmanagement->displayProductForm();
        } else {
                SmartyValidate::connect($rmanagement->tpl);
                SmartyValidate::register_form('addproduct');
                if(SmartyValidate::is_valid($_POST, 'addproduct')) {
                        //form is valid
                        $rmanagement->addProductEntry($_POST);
                        $rmanagement->displayProductForm();
                        SmartyValidate::disconnect();
                } else {
                        $rmanagement->displayProductForm();
                }
        }
        break;
    default:
        // viewing the rmanagement
        $rmanagement->displayAdmin();
        break;
}


Here's my tpl file:
Code:

<table border="1" align="center">
    <tr>
        <th colspan="2" bgcolor="#F0F0F0">Add a Product</th>
    </tr>
    <tr>
        <td>{validate id="category_name" form="addproduct" message="How'd you trigger this? - "}Product Category:</td>
        <td><select name=category_name>
                {html_options values=$categories output=$categories}
            </select>
        </td>
    </tr>
    <tr>
        <td valign="top"><small><font color="red">{validate id="version" form="addproduct" message="Please Enter: - "}</font></small>Version:</td>
        <td><input type="text" name="version">{$version|escape}</td>
    </tr>
    <tr>
        <td valign="top"><small><font color="red">{validate id="qa_version" form="addproduct" message="Allowed to be Empty - "}</font></small>QA Version:</td>
        <td><input type="text" name="qa_version">{$qa_version|escape}</td>
    </tr>
    <tr>
        <td valign="top"><small><font color="red">{validate id="code_name" form="addproduct" message="Please enter: - "}</font></small>Code Name:</td>
        <td><input type="text" name="code_name">{$code_name|escape}</td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" value="Submit"></td>
    </tr>
</table>


Here's my function library
Code:

    function ValidateFormSetup($form) {
        SmartyValidate::connect($this->tpl, 'true');
        SmartyValidate::register_form("$form");
        switch($form) {
        case 'addproduct':
                SmartyValidate::register_validator('category_name', 'category_name', 'notEmpty', false, false, 'trim', $form);
                SmartyValidate::register_validator('version', 'version', 'notEmpty', false, false, 'trim', $form);
                SmartyValidate::register_validator('qa_version', 'qa_version', 'notEmpty', true, false, 'trim', $form);
                SmartyValidate::register_validator('code_name', 'code_name', 'notEmpty', false, false, 'trim', $form);
                $return = 1;
        default:
                SmartyValidate::register_validator('category_name', 'category_name', 'notEmpty', false, false, 'trim', $form);
                SmartyValidate::register_validator('version', 'version', 'notEmpty', false, false, 'trim', $form);
                SmartyValidate::register_validator('qa_version', 'qa_version', 'notEmpty', true, false, 'trim', $form);
                SmartyValidate::register_validator('code_name', 'code_name', 'notEmpty', false, false, 'trim', $form);
                $return = 1;
        }
        return $return;
    }

    function ValidateForm($_POST, $form) {
        SmartyValidate::connect($this->tpl);
        if(SmartyValidate::is_valid($_POST, "$form")) {
                //form is valid
                SmartyValidate::disconnect();
                $return = 1;
        } else {
                // error, redraw the form
                $this->tpl->assign($_POST);
                $return = 0;
        }
        return $return;
    }
    function displayProductForm() {

        $this->sql->query(
                "select category_name from category_prod order by category_name ASC",
                SQL_ALL,
                SQL_ASSOC
                );
        foreach($this->sql->record as $value)
                $new_array[$value['category_name']] = $value['category_name'];

        // assign errors
        $this->tpl->assign('error', $this->error);
        $this->tpl->assign('categories', $new_array);
        $this->tpl->display('admin_addproduct.tpl');
    }


So, If I use the ValidateFormSetup function all I get is:
Code:

[client 10.30.0.168] PHP Notice:  Undefined index:  code_name in /var/www/html/rmanagement/templates_c/%%69^69D^69D6A912%%admin_addproduct.tpl.php on line 48, referer: http://qapxe.corp.mxlogic.com/rmanagement/admin.php?action=addproduct
[client 10.30.0.168] PHP Notice:  Undefined index:  version in /var/www/html/rmanagement/templates_c/%%69^69D^69D6A912%%admin_addproduct.tpl.php on line 36, referer: http://qapxe.corp.mxlogic.com/rmanagement/admin.php?action=addproduct
[client 10.30.0.168] PHP Notice:  Undefined index:  qa_version in /var/www/html/rmanagement/templates_c/%%69^69D^69D6A912%%admin_addproduct.tpl.php on line 42, referer: http://qapxe.corp.mxlogic.com/rmanagement/admin.php?action=addproduct



Any Ideas?

Edit note: I accidently copy and pasted my functions twice...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP