Notice: Validate::regex Unspecified rule: phone in admin/lib/eleontev/Validator.php

Discussion in 'Programming/Scripts' started by saigopal, Oct 25, 2007.

  1. saigopal

    saigopal New Member

    getting this error in Notice: Validate::regex Unspecified rule: phone in admin/lib/eleontev/Validator.php on line 218


    PHP:
    <?php
    class Validator 
    {
        var 
    $msg "Error(s) occur!";
        var 
    $errors = array();
        var 
    $values = array();
        var 
    $is_error false;
        var 
    $display_all true;
    var 
    $js;
        var 
    $use_jscript true;
            function 
    regexnumb($msg$rule$val_key$required true) {

            if(!
    Validate::regex($rule$this->values[$val_key], $required)) {
                
    $this->setError($msg$val_key$rule);
            }
            
            
    $this->js->regex($msg$rule$val_key$required);
        }
            function 
    required($msg$val_key) {
            
            if(!
    is_array($val_key))  { $val_key = array($val_key); }
            foreach(
    $val_key as $k => $v) {
                if(!
    Validate::required($this->values[$v])) {
                    
    $this->setError($msg$v'required');
                    break;
                }    
            }
            
            
    $this->js->required($msg$val_key);
        }
            
        
        
        function 
    setError($msg$field$rule false) {
            if(
    $this->display_all != true && $this->is_error) { return; } 
            
            
    $this->is_error true;
            
    $this->errors[] = array('msg'  => $msg,
                                    
    'field'=> $field,
                                    
    'rule' => $rule
                                     
    );
        }
        function 
    setDisplayMethod($method) {
            
    $this->display_all $method;
        }
        function 
    setValues(&$values) {

            
    $this->values =& $values;
        }
        function 
    getErrors() {
            return 
    $this->errors;
        }
        function 
    getJscript() {
            return 
    $this->js->getScript();
        }
        function 
    getHtml() {
            
            
    $ret[] = sprintf("<b>%s</b><ul>"$this->msg);
            foreach(
    $this->errors as $k => $v) {
                
    $ret[] = sprintf("<li>%s</li>"$v['msg']);
            }
            
    $ret[] = "</ul>";
            
            return 
    implode("\n"$ret);
        }
        function 
    display() {
            echo 
    $this->getHtml();
        }
    }
    class 
    Validate
    {
        function 
    Validate() {
        }
    function 
    email($email$required true){
            return 
    Validate::regex('email'$email$required);
        }
        function 
    phone($phone$required true)
        {
            return 
    Validate::regexnumb('phone'$phone,$required);
        }
        function 
    getRegex($rule) {
            
    $regex = array('email'         => '/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/',
                           
    'lettersonly'   => '/^[a-zA-Z]+$/',
                           
    'alphanumeric'  => '/^[a-zA-Z0-9]+$/',
                           
    'numeric'       => '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/',
                           
    'nopunctuation' => '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/',
                           
    'nonzero'       => '/^-?[1-9][0-9]*/'
                           
    );

            if(!isset(
    $regex[$rule])) {
                
    trigger_error(ucfirst(__CLASS__) ."::regex  Unspecified rule: {$rule}");
                return 
    false;
            } else {
                return 
    $regex[$rule];
            }
        }
        function 
    getRegexnumber($rule) {
            
    $regex = array('phone'         => '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/'
                               
    );
            if(!isset(
    $regex[$rule])) {
                
    trigger_error(ucfirst(__CLASS__) ."::regex  Unspecified rule: {$rule}");
                return 
    false;
            } else {
                return 
    $regex[$rule];
            }
        }
        function 
    regex($rule$val$required true) {
            
            
    $val trim($val);

            
    $regex Validate::getRegex($rule);

            if(!
    $regex) { return false; }
            
            if(!
    $required && empty($val)) {
                return 
    true;

            } else {

                return (bool) (
    preg_match($regex$val));
            }
            

            
        }
    function 
    regexnumb($rule$val$required true) {
            
            
    $val trim($val);

            
    $regex Validate::getRegex($rule);

            if(!
    $regex) { return false; }
            
            if(!
    $required && empty($val)) {
                return 
    true;

            } else {

                return (bool) (
    preg_match($regex$val));
            }
            

            
        }
        
    // return true if ok
         
    function between($val$min$max false$required true) {
            
            
    $val = (is_numeric($val)) ? (float) $val strlen(trim($val));
            
            if(!
    $required && empty($val)) {
                return 
    true;
            } else {
                if(
    $max && $max) { return (bool) ($val >= $min && $val <= $max); } 
                elseif(
    $min)     { return (bool) ($val >= $min); } 
                elseif(
    $max)     { return (bool) ($val <= $max); } 
                else             { return 
    false; }
            }
        }
        
        function 
    requiredArray($required$values) {
            if(!
    is_array($values))   { $values = array($values); }
            if(!
    is_array($required)) { $required = array($required); }    
            
            
    $ret true;
            foreach(
    $required as $v) {
                if(!
    Validate::required(@$values[$v])) {
                    
    $ret false;
                    break;
                }
            }
            
            return 
    $ret;
        }
        
        function 
    required($value) {

            return (bool) (
    trim(!empty($value)));
        }
        
        function 
    compare($check_val$with_val$operator '==') {
            
            if (
    '==' != $operator && '!=' != $operator) {
                
    $compareFn create_function('$a, $b''return floatval($a) ' $operator ' floatval($b);');
            } else {
                
    $compareFn create_function('$a, $b''return $a ' $operator ' $b;');
            }
            
            return (bool) 
    $compareFn($check_val$with_val);
        }
    }



    class 
    ValidatorJscript
    {

        var 
    $script_box;
        var 
    $script = array();
        var 
    $use_script_box true;
        
        
        function 
    ValidatorJscript() {
            
    $this->script_box GetJscript::getScriptBox();
        }

    function 
    regex3($msg,$rule,$val_key,$required true)
    {
                    @
    $reg GetJscript::getRegex($rule);
            if(
    $rule == 'phone') {

                
    $this->phone($msg$val_key$required);
                
            } elseif(!
    $reg) {
                return;
                
            } else {
                
    $html[] = GetJscript::getElement('f'$val_key);
                
    $html[] = 're = new RegExp("' $reg '");';
                
    $html[] = 'r = re.test(f.value);';
                
    $html[] = ($required) ? 'if(!r)' 'if(f.value && !r)';
                
    $html[] = GetJscript::getErrorRoutine('f'$msg);
                
                
    $this->setScript($html);            
            }

    }    
        
        function 
    regex($msg$rule$val_key$required true) {
            
            @
    $reg GetJscript::getRegex($rule);
            
        
            if(
    $rule == 'email') {
                
    $this->email($msg$val_key$required);
                
            } elseif(!
    $reg) {
                return;
                
            } else {
                
    $html[] = GetJscript::getElement('f'$val_key);
                
    $html[] = 're = new RegExp("' $reg '");';
                
    $html[] = 'r = re.test(f.value);';
                
    $html[] = ($required) ? 'if(!r)' 'if(f.value && !r)';
                
    $html[] = GetJscript::getErrorRoutine('f'$msg);
                
                
    $this->setScript($html);            
            }


        }
        function 
    regexnumb($msg$rule$val_key$required true) {
            
            @
    $reg GetJscript::getRegex($rule);
            
        
            if(
    $rule == 'phone') {
                
    $this->phone($msg$val_key$required);
                
            } elseif(!
    $reg) {
                return;
                
            } else {
                
    $html[] = GetJscript::getElement('f'$val_key);
                
    $html[] = 're = new RegExp("' $reg '");';
                
    $html[] = 'r = re.test(f.value);';
                
    $html[] = ($required) ? 'if(!r)' 'if(f.value && !r)';
                
    $html[] = GetJscript::getErrorRoutine('f'$msg);
                
                
    $this->setScript($html);            
            }


        }
        
        function 
    required($msg$val_key) {
            
            if(!
    is_array($val_key))  { $val_key = array($val_key); }
            
            foreach(
    $val_key as $k => $v) {
                
    $html[] = GetJscript::getElement('f'$v);
                
    $html[] = 'if(isBlank(f.value))';
                
    $html[] = GetJscript::getErrorRoutine('f'$msg);
            }

            
    $this->setScript($html);
        }
        
        
        function 
    email($msg$val_key$required true) {    
            
            
    $html[] = GetJscript::getElement('f'$val_key);
            
    $html[] = ($required) ? 'if(!isValidEmailStrict(f.value))'
                                  
    'if(f.value && !isValidEmailStrict(f.value))';
            
    $html[] = GetJscript::getErrorRoutine('f'$msg);
                
            
    $this->setScript($html);
        }
        
        
        function 
    between($msg$val_key$min$max false$required true) {
            
            
    $html[] = GetJscript::getElement('f'$val_key);
            
            if(
    $required) {
                
    $str sprintf("if(!isBetween(f.value, '%s', '%s'))"$min$max);
            } else {
                
    $str sprintf("if(f.value && !isBetween(f.value, '%s', '%s'))"$min$max);
            }
            
            
    $html[] = $str;
            
    $html[] = GetJscript::getErrorRoutine('f'$msg);
                
            
    $this->setScript($html);        
        }
        
        
        function 
    compare($msg$key_check_val$with_val$operator '==') {
             
             
    $html[] = GetJscript::getElement('f'$key_check_val);
             
    $html[] = GetJscript::getElement('f1'$with_val);
            
            if (
    '==' != $operator && '!=' != $operator) {
                
                
    $html[] = 'checked_value = parseFloat(f.value);';
                
    $html[] = 'checker_value = parseFloat(f1.value);';
            
            } else {
            
                
    $html[] = 'checked_value = f.value;';
                
    $html[] = 'checker_value = f1.value;';
            }
            
            
    $html[] = sprintf('r = (checked_value %s checker_value);'$operator);        
            
    $html[] = 'if(r == false)';
            
    $html[] = GetJscript::getErrorRoutine('f1'$msg);
                
            
    $this->setScript($html);
        }
        
        
        function 
    setScript($array) {
            
    $this->script[] = (is_array($array)) ? implode("\n"$array) : $array
        }
        
        
        function 
    getScript() {
            
            
    $script implode("\n"$this->script);
            
            if(
    $this->use_script_box) {
                
    $script str_replace('{script}'$script$this->script_box);
            }
            
            return 
    $script;
        }
    }





    class 
    GetJscript
    {

        function 
    getScriptBox() {
            
    $html 
            
    '<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
            <!--
            function Validate(form) {
            
                {script}
            
            return true;
            }
            //-->
            </SCRIPT>'
    ;
            
            return 
    $html;
        }
        
        
        function 
    getElement($var$val) {
            
    $html[] = sprintf('%s = document.getElementById(\'%s\');'$var$val);
            
    $html[] = sprintf('if(!%s) { %s = form.%s };'$var$var$val);
            return 
    implode("\n"$html);
        }    

        
        function 
    getErrorRoutine($field$msg) {
            
    $html '
            {        
                alert(\'%s\');            
                %s.focus();
                %s.select();
                return false;
            }'
    ;        
            
            return 
    sprintf($html$msg$field$field);
        }
        
        
        function 
    getRegex($rule) {
            
    $regex = array('email'         => '^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$',
                           
    'lettersonly'   => '^[a-zA-Z]+$',
                           
    'alphanumeric'  => '^[a-zA-Z0-9]+$',
                        
                           
    'nonzero'       => '^-?[1-9][0-9]*'
                           
    );
                           
            return 
    $regex[$rule];
        }
    }
    ?>
    please solve my problem

    i am trying to add the functionality in kbpublisher with phone number number field validation and required field

    thanks in advance
     
  2. Ben

    Ben Active Member Moderator

    What exactly is your line 218, and how do you call the code?
    I guess you instantiate the Validator class and call the method phone(...) then?

    As far as I understand the code, after validating the rule it tries to get the regular expression via the method getRegex($rule). There just an array is defined, where the key has to match the value of $rule, but there is no array element defining the regular expression for the phone number....
    so the regex array should look sth like this

    PHP:
    $regex = array('email'         => '^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$',
                           
    'lettersonly'   => '^[a-zA-Z]+$',
                           
    'alphanumeric'  => '^[a-zA-Z0-9]+$',
                        
                           
    'nonzero'       => '^-?[1-9][0-9]*',
                           
    'phone'          => 'YOURREGEXHERE'
                           
    );
     
  3. saigopal

    saigopal New Member

    Hi Ben

    The above error is resolved because i am not calling the function validation_js.php that is showing error. i have activated javascript function in using {literal}{/literal} variables now i am facing new problem is it is displaying {/literal} in the screen how to get rid of this

    see my register_form.html from client/skin/view_default/default/register_form.html

    HTML:
    
    
             <tr>
                <td height="5"></td>
              </tr>
              <tr>
                <td height="5" align="center">
    			{literal}
    			<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    <!--
    function Validateform() {
    	
    var frm=document.form; 
    if(frm.first_name.value=="")
    {
    	alert("Please Enter First Name");
    	frm.first_name.focus();
    	return false;
    }
    if(frm.last_name.value =="")
    {
    	alert("Please Enter Last Name");
    	frm.last_name.focus();
    	return false;
    }
    
    if(frm.email.value=="")
    {
    	alert("Please Enter Email");
    	frm.email.focus();
    	return false;
    }
    
    if(checkingEmail(frm.email.value)==false)
    {
    	alert("Please enter valid Email Address");
    	frm.email.focus();
    	return false;
    }
    if(frm.phone.value =="")
    {
    	alert("Please Enter Phone");
    	frm.phone.focus();
    	return false;
    }
    if(isNaN(frm.phone.value))
    {
    alert('Valid No of phone numbers are [0-9]');
    frm.phone.value ="";
    frm.phone.focus();
    return false;
    }	
    if(frm.username.value == "")
    {
    	alert("Please Enter Username ");
    	frm.username.focus();
    	return false;
    }
    if(frm.password.value=="")
    {
    	alert("Please Enter Password");
    	frm.password.focus();
    	return false;
    }
    if(frm.password_2.value =="")
    {
    	alert("Please Enter Confirm Password");
    	frm.password_2.focus();
    	return false;
    	
    }
    if(frm.subscription.value =="0")
    {
    	alert("Please select subscription");
    	frm.subscription.focus();
    	return false;
    	
    }	
    if(frm.captcha.value=="")
    {
    	alert("Please Enter Security Code");
    	frm.captcha.focus();
    	return false;
    }
    
    
    
    
    }
    	
    function setPasswordDisabled() {
    
    	if(!document.getElementById('not_change_pass')) {
    		return;
    	} 
    	
    	ch = document.getElementById('not_change_pass');
    	p = document.getElementById('password');
    	p2 = document.getElementById('password_2');
    	
    	if(ch.checked == true) {
    		p.disabled = true;
    		p2.disabled = true;
    	} else {
    		p.disabled = false;
    		p2.disabled = false;
    	}
    	
    }
    
    
    function checkingEmail(emailStr){
    	var emailPat=/^(.+)@(.+)$/
    	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    	var validChars="\[^\\s" + specialChars + "\]"
    	var quotedUser="(\"[^\"]*\")"
    	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    	var atom=validChars + '+'
    	var word="(" + atom + "|" + quotedUser + ")"
    	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
    
    	var matchArray=emailStr.match(emailPat)
    	if (matchArray==null) {
    		return false
    	}
    	var user=matchArray[1]
    	var domain=matchArray[2]
    	if (user.match(userPat)==null) {
    	    return false
    	}
    	var IPArray=domain.match(ipDomainPat)
    	if (IPArray!=null) {
    		  for (var i=1;i<=4;i++) {
    		    if (IPArray[i]>255) {
    				return false
    		    }
    	    }
    	    return true
    	}
    	var domainArray=domain.match(domainPat)
    	if (domainArray==null) {
    		return false
    	}
    	var atomPat=new RegExp(atom,"g")
    	var domArr=domain.match(atomPat)
    	var len=domArr.length
    	if (domArr[domArr.length-1].length<2 ||
    	    domArr[domArr.length-1].length>4) {
    		return false
    	}
    	if (len<2) {
    	   return false
    	}
    	return true;
    }
    
    //-->
    </SCRIPT>
    {/literal}
    			
    			<form action="{action_link}" method="post" name="form" id="" onSubmit="return Validateform();">
    <input type="hidden" name="id" id="id" value="{id}" />
    <input type="hidden" name="date_registered" id="date_registered" value="{date_registered}" />
    
    
    <table width="50%" border="0" align="center" cellpadding="2" cellspacing="2">
    <tr >
    	<td class="page_heading" colspan="2">{title_msg}</td>
    </tr>
    <tr class="trForm">
    	<td width="33%" class="lable1">{first_name_msg} {required_sign}</td>
    	<td width="67%"><input name="first_name" type="text" class="text_box" id="first_name" value="{first_name}" maxlength="25"/></td>
    </tr>
    <tr class="trForm">
    	<td class="lable1">{last_name_msg} {required_sign}</td>
    	<td><input name="last_name" id="last_name" type="text" class="text_box" value="{last_name}" maxlength="25"/></td>
    </tr>
    <tr class="trForm">
    	<td class="lable1">{middle_name_msg}</td>
    	<td><input name="middle_name" id="middle_name" type="text" class="text_box" value="{middle_name}" maxlength="25" /></td>
    </tr>
    <tr class="trForm">
    	<td class="lable1">{email_msg} {required_sign}</td>
    	<td><input name="email" id="email" type="text" value="{email}" class="text_box" maxlength="65" /></td>
    </tr>
    
    <tr class="trForm">
    	<td class="lable1">{phone_msg} {required_sign}</td>
    	<td><input name="phone" id="phone" type="text" value="{phone}" class="text_box" /></td>
    </tr>
    
    <tr class="trForm">
    	<td class="lable1">{username_msg} {required_sign}</td>
    	<td valign="top"><input name="username" id="username" type="text" value="{username}" class="text_box" /></td>
    </tr>
    
    <tr class="trForm">
    	<td class="lable1">{password_msg} {required_sign}</td>
    	<td valign="top">
    	<input type="password" name="password" id="password" class="text_box" />
    	<tmpl:update>
    	<input type="checkbox" name="not_change_pass" id="not_change_pass" value="1" 
    	onchange="setPasswordDisabled();" {pass_change_checked} /> {not_change_pass_msg}	</tmpl:update>	</td>
    </tr>
    <tr class="trForm"> 
    	<td class="lable1">{password_repeat_msg} {required_sign}</td>
    	<td valign="top"><input type="password" name="password_2" id="password_2" class="text_box" /></td>
    </tr>
     
    <tmpl:captcha>
     <tr  valign="top">
                          <td align="left" class="lable1">Subscription {required_sign}</td>
                          <td align="left" >{category_list}</td>
                </tr>
    <tr class="trForm" valign="top">
    	<td class="lable1"></td>
    	<td><br /><img src="{captcha_src}" alt="code" /></td>
    </tr>
    
    <tr class="trForm" valign="top">
    	<td valign="top" class="lable1">{captcha_msg} {required_sign}</td>
    	<td valign="top" class="lable1">
    		<input type="text" name="captcha" id="captcha" value="" class="text_box" /><br /><br style="line-height:5px;"/>
     
    		{captcha_comment_msg}</td>
    </tr>
    </tmpl:captcha>
    
    <tr>
      <td colspan="2" height="5"></td>
      </tr>
    <tr> 
    	<td>
    		<tmpl:update>
    		  <input type="hidden" name="remember" id="remember" value="0" />
    		<input type="checkbox" name="remember" id="remember" value="1" {remember_option} /> {remember_msg}		</tmpl:update>	</td>
    	<td><input type="submit" name="submit" id="submit" value="Register" class="button" />
    		<!-- <input type="button" value="{cancel_msg}" onclick="javascript:history.back(1)" class="button" /> -->
    		<input type="button" value="{cancel_msg}" onClick="location.href='{cancel_link}'" class="button" />	</td>
    </tr>
    </table>
    </form>
    
    <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    <!--
    setPasswordDisabled();
    //-->
    </SCRIPT></td>
              </tr>
              <tr>
                <td height="150">&nbsp;</td>
              </tr>
    </table>
    
    
    
    
    
    
     
  4. Ben

    Ben Active Member Moderator

    Is it a Smarty template system? That's the only one I know, where you have to put your JS code in {literal}cause of the parser...

    You could export the JS-Code to a file and include that file instead of writing the JS code directly to the ouput page...
     
  5. saigopal

    saigopal New Member

    Ben

    //-->{/literal}
    </SCRIPT>

    if i put this literal closing inside the </script> will be working now

    thanks
     

Share This Page