Skip to content

Commit

Permalink
added field option
Browse files Browse the repository at this point in the history
  • Loading branch information
amitkhare committed Sep 28, 2017
1 parent 3db32f0 commit 4452eed
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/EasyValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private function connect($host="localhost",$username="root",$password="",$dbname
$mysqli = new \mysqli($host,$username,$password,$dbname);
/* check connection */
if (mysqli_connect_errno()) {
$this->setStatus(500, $this->translate("DB_ERROR",mysqli_connect_error()));
$this->setStatus(500,"DB", $this->translate("DB_ERROR",mysqli_connect_error()));
$this->isConnected=false;
} else {
$this->dbConn = $mysqli;
Expand All @@ -101,7 +101,7 @@ public function match($field1="",$field2="",$rules=[]){

if($this->source[$field1] != $this->source[$field2]){
$status = $this->translate("FIELDS_DONT_MATCH",[$field1,$field2]);
$this->setStatus(500,$status);
$this->setStatus(500,$field2,$status);
}

}
Expand Down Expand Up @@ -139,13 +139,13 @@ public function check($field="",$rules="required|numeric|min:1|max:50"){
private function minMax($field,$min=0,$max=0){
if ($max>0 && $max>=$min){
if(strlen($this->source[$field]) > $max) {
$this->setStatus(500, $this->translate("FIELD_VALUE_TOO_LONG",[$field,$max]));
$this->setStatus(500,$field, $this->translate("FIELD_VALUE_TOO_LONG",[$field,$max]));
$this->sanitizeString($field);
}
}
if ($min>0 && $min <= $max){
if(strlen($this->source[$field]) < $min) {
$this->setStatus(500, $this->translate("FIELD_VALUE_TOO_SHORT",[$field,$min]));
$this->setStatus(500,$field, $this->translate("FIELD_VALUE_TOO_SHORT",[$field,$min]));
$this->sanitizeString($field);
}
}
Expand Down Expand Up @@ -194,9 +194,9 @@ private function _fetchRule($field,$rule){
break;
}
}
public function setStatus($code,$msg){
public function setStatus($code,$field,$msg){
$this->code=$code;
$this->msgs[]=$msg;
$this->msgs[$field]=$msg;
}
public function isValid(){
if($this->code===200){
Expand Down Expand Up @@ -228,43 +228,43 @@ private function is_set($field) {
if(isset($this->source[$field])){
return true;
}else {
$this->setStatus(500, $this->translate("FIELD_NOT_SET",$field));
$this->setStatus(500,$field, $this->translate("FIELD_NOT_SET",$field));
}
}
private function required($field){
if(!isset($this->source[$field])){
$this->setStatus(500, $this->translate("FIELD_NOT_SET",$field));
$this->setStatus(500,$field, $this->translate("FIELD_NOT_SET",$field));
} elseif(empty($this->source[$field]) || $this->source[$field]=="" || strlen($this->source[$field]) == 0){
$this->setStatus(500, $this->translate("FIELD_REQUIRED",$field));
$this->setStatus(500,$field, $this->translate("FIELD_REQUIRED",$field));
}
}
private function validateIpv4($field) {
if(filter_var($this->source[$field], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {
$this->setStatus(500, $this->translate("FIELD_INVALID_IPV4",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_IPV4",$field));
}
}
public function validateIpv6($field) {
if(filter_var($this->source[$field], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) {
$this->setStatus(500, $this->translate("FIELD_INVALID_IPV6",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_IPV6",$field));
}
}
private function validateFloat($field) {
if(filter_var($this->source[$field], FILTER_VALIDATE_FLOAT) === false) {
$this->setStatus(500, $this->translate("FIELD_INVALID_FLOAT",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_FLOAT",$field));
}
}
private function validateString($field) {
if(isset($this->source[$field])) {
if(!is_string($this->source[$field])) {
$this->setStatus(500, $this->translate("FIELD_INVALID_STRING",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_STRING",$field));
$this->sanitizeString($field);
}
}
}
private function alphaNumeric($field,$min=0,$max=0) {
if(isset($this->source[$field])) {
if(preg_match("/[^a-z_\.\-0-9\s]/i", $this->source[$field] ) == true) {
$this->setStatus(500, $this->translate("FIELD_INVALID_ALPHA_NUMERIC",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_ALPHA_NUMERIC",$field));
$this->sanitizeString($field);
}
}
Expand All @@ -273,51 +273,51 @@ private function alphaNumeric($field,$min=0,$max=0) {
private function alpha($field,$min=0,$max=0) {
if(isset($this->source[$field])) {
if(preg_match("/[^a-z\s]/i", $this->source[$field] ) == true) {
$this->setStatus(500, $this->translate("FIELD_INVALID_ALPHA",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_ALPHA",$field));
$this->sanitizeString($field);
}
}
}
private function alphaNumericUnicode($field,$min=0,$max=0) {
if(isset($this->source[$field])) {
if(preg_match("[^a-z_\.\-0-9\x{4e00}-\x{9fa5}]/ui", $this->source[$field] ) == true) {
$this->setStatus(500, $this->translate("FIELD_INVALID_ALPHA_NUMERIC_UNICODE",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_ALPHA_NUMERIC_UNICODE",$field));
$this->sanitizeString($field);
}
}
}
private function validateNumeric($field, $min=0, $max=0) {
if(preg_match("/[^0-9]+/",$this->source[$field])) {
$this->setStatus(500, $this->translate("FIELD_INVALID_NUMERIC",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_NUMERIC",$field));
$this->sanitizeNumeric($field);
}
}
private function validateUrl($field) {
if(filter_var($this->source[$field], FILTER_VALIDATE_URL) === FALSE) {
$this->setStatus(500, $this->translate("FIELD_INVALID_URL",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_URL",$field));
$this->sanitizeUrl($field);
}
}
private function validateEmail($field) {
if(filter_var($this->source[$field], FILTER_VALIDATE_EMAIL) === FALSE) {
$this->setStatus(500, $this->translate("FIELD_INVALID_EMAIL",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_EMAIL",$field));
$this->sanitizeEmail($field);
}
}
private function validateBool($field) {
filter_var($this->source[$field], FILTER_VALIDATE_BOOLEAN);{
$this->setStatus(500, $this->translate("FIELD_INVALID_BOOLEAN",$field));
$this->setStatus(500,$field, $this->translate("FIELD_INVALID_BOOLEAN",$field));
}
}
private function isUnique($field,$table,$column){
if($this->isValid()) {
if($this->isConnected){
$query = mysqli_query($this->dbConn, "SELECT * FROM `$table` WHERE `$column`='".$this->sanitizeField($field)."'");
if(mysqli_num_rows($query) > 0){
$this->setStatus(500, $this->translate("FIELD_VALUE_ALREADY_EXISTS",$field));
$this->setStatus(500,$field, $this->translate("FIELD_VALUE_ALREADY_EXISTS",$field));
}
} else {
$this->setStatus(500, $this->translate("DB_NOT_CONNECTED",$field));
$this->setStatus(500,$field, $this->translate("DB_NOT_CONNECTED",$field));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en-IN.lang
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"FIELD_VALUE_TOO_SHORT" : "The `%s` field value is too short, it should be at least `%s` characters long.",

"USERNAME":"Username",
"IDENTIFIER": "Username/ Email",
"GENDER": "Gender",
"FIRSTNAME":"First Name",
"LASTNAME":"Last Name",
"MIDDLENAME":"Middle Name",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/hi-IN.lang
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"FIELD_VALUE_TOO_SHORT" : "`%s` फील्ड की वैल्यू बहुत कम है, इसे कम से कम `%s` अक्षर का होना चाहिए.",

"USERNAME":"यूजरनेम",
"IDENTIFIER": "यूजरनेम/ईमेल",
"GENDER": "जेंडर",
"FIRSTNAME":"प्रथम नाम",
"LASTNAME":"उप नाम",
"MIDDLENAME":"मध्य नाम",
Expand Down

0 comments on commit 4452eed

Please sign in to comment.