Skip to content

Commit

Permalink
Update form_validation library
Browse files Browse the repository at this point in the history
  • Loading branch information
ronmarasigan committed Jan 15, 2021
1 parent 26af7ef commit 6bf668a
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 8 deletions.
21 changes: 21 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020, Ronald M. Marasigan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
141 changes: 134 additions & 7 deletions system/libraries/Form_validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@
*/

class Form_validation {
/**
* Reference to the LavaLust instance
*
* @var object
*/
protected $LAVA;

//Default Error Messages
private static $err_required = '%s is required';
private static $err_matches = '%s does not match with the other field';
private static $err_differs = '%s matches with the other field';
private static $err_is_unique = '%s is not unique';
private static $err_exact_length = '%s not in exact length';
private static $err_min_length = 'Please enter less than %d character/s';
private static $err_max_length = 'Please enter more than %d character/s';
private static $err_email = '%s contains invalid email address';
Expand All @@ -55,8 +64,12 @@ class Form_validation {
private static $err_alphaspace = '%s accepts letters and spaces only';
private static $err_alphanumdash = '%s accepts letters, numbers and dashes only';
private static $err_numeric = '%s accepts numbers only';
private static $err_grater_than = 'Please enter a value less than %f';
private static $err_integer = '%s accepts integers only';
private static $err_decimal = '%s accepts decimals only';
private static $err_greater_than = 'Please enter a value less than %f';
private static $err_less_than = 'Please enter a value greater than %f';
private static $err_greater_than_equal_to = 'Please enter a value less than or equal to %f';
private static $err_less_than_equal_to = 'Please enter a value greater than or equal to %f';
private static $err_in_list = '%s is not in the list';
private static $err_pattern = 'Please is not in %s format';

Expand Down Expand Up @@ -84,6 +97,7 @@ class Form_validation {


public function __construct() {
$this->LAVA =& get_instance();
foreach($_POST as $key => $value) {
$this->post_arrays[$key] = $value;
}
Expand Down Expand Up @@ -190,6 +204,59 @@ public function matches($field, $custom_error = '') {
return $this;
}

/**
* Check if current field differs from other field
*
* @param string $field
* @param string $err Custom Error
* @return $this
*/
public function differs($field, $custom_error = '') {
if($this->value === $this->post_arrays[$field]){
$this->set_error_message($custom_error, self::$err_differs, $this->name);
}
return $this;
}

/**
* Is Unique
*
* Check if the input value doesn't already exist
* in the specified database field.
*
* @param string $str
* @param string $field
* @return bool
*/
public function is_unique($table, $str, $field, $custom_error = '')
{
if(isset($this->LAVA->db))
{
$this->LAVA->db->table($table)->where($field, $str)->limit(1)->get();
if($this->LAVA->db->row_count()!==0)
$this->set_error_message($custom_error, self::$err_is_unique, $this->name);
}
return $this;
}

/**
* Exact Length
*
* @param string
* @param string
* @return bool
*/
public function exact_length($length, $custom_error = '')
{
if ( ! is_numeric($length))
return FALSE;

if(mb_strlen($this->value) === (int) $length){
$this->set_error_message($custom_error, self::$err_exact_length, $length);
}
return $this;
}

/**
* Check for minumum length
*
Expand Down Expand Up @@ -255,7 +322,7 @@ public function alpha($custom_error = '')
* @param string
* @return bool
*/
public function alpha_num($custom_error = '')
public function alpha_numeric($custom_error = '')
{
if(!ctype_alnum((string) $this->value))
$this->set_error_message($custom_error, self::$err_alphanum, $this->name);
Expand All @@ -268,7 +335,7 @@ public function alpha_num($custom_error = '')
* @param string
* @return bool
*/
public function alpha_num_space($custom_error = '')
public function alpha_numumeric_space($custom_error = '')
{
if(!preg_match('/^[A-Z0-9 ]+$/i', $this->value))
$this->set_error_message($custom_error, self::$err_alphanumspace, $this->name);
Expand All @@ -294,7 +361,7 @@ public function alpha_space($custom_error = '')
* @param string
* @return bool
*/
public function alpha_num_dash($custom_error = '')
public function alpha_numumeric_dash($custom_error = '')
{
if(!preg_match('/^[a-z0-9_-]+$/i', $this->value))
$this->set_error_message($custom_error, self::$err_alphanumdash, $this->name);
Expand All @@ -315,6 +382,34 @@ public function numeric($custom_error = '')

}

/**
* Integer
*
* @param string
* @return bool
*/
public function integer($custom_error = '')
{
if(!preg_match('/^[\-+]?[0-9]+$/', $this->value))
$this->set_error_message($custom_error, self::$err_integer, $this->name);
return $this;

}

/**
* decimal
*
* @param string
* @return bool
*/
public function decimal($custom_error = '')
{
if(!preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $this->value))
$this->set_error_message($custom_error, self::$err_decimal, $this->name);
return $this;

}

/**
* Greater than
*
Expand All @@ -327,7 +422,23 @@ public function greater_than($min, $custom_error = '')
if(!is_numeric($this->value))
return FALSE;
if($this->value < $min)
$$this->set_error_message($custom_error, self::$err_numeric, $min);
$$this->set_error_message($custom_error, self::$err_greater_than, $min);
return $this;
}

/**
* Greater than or Equal
*
* @param string
* @param int
* @return bool
*/
public function greater_than_equal_to($min, $custom_error = '')
{
if(!is_numeric($this->value))
return FALSE;
if($this->value <= $min)
$$this->set_error_message($custom_error, self::$err_greater_than_equal_to, $min);
return $this;
}

Expand All @@ -342,8 +453,24 @@ public function less_than($max, $custom_error = '')
{
if(!is_numeric($this->value))
return FALSE;
if($this->value > $min)
$this->set_error_message($custom_error, self::$err_numeric, $max);
if($this->value > $max)
$this->set_error_message($custom_error, self::$err_less_than, $max);
return $this;
}

/**
* Less than equal to
*
* @param string
* @param int
* @return bool
*/
public function less_than_equal_to($max, $custom_error = '')
{
if(!is_numeric($this->value))
return FALSE;
if($this->value >= $max)
$this->set_error_message($custom_error, self::$err_less_than_equal_to, $max);
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion system/libraries/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public function flashdata($key = NULL)
}

/**
* Get flash data to Session
* Set flash data to Session
* @param array $key Session Keys
* @return function
*/
Expand Down

0 comments on commit 6bf668a

Please sign in to comment.