Here i am having three field like name,email,mobile. here what i am doing means i writing the test case for those fields in PHP,lets take name field condition like
- empty condition
- name should not be number
- name minimum 3 charector
suppose above conditions failed means i have to push to Failed Condition
array, i tried i am not getting my expected results,kindly any one update my code
My code
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
if(!empty($name) && !empty($email) && !empty($phone)){
// Scenario = 1
if(!preg_match("/^[a-zA-Z'-]+$/", $name)){
$namevalidation['status'] = "Fail" ;
$namevalidation['Failed Condition'][]['name'][]['Name should not be a number'] = "Condition Failed" ;
}
// Scenario = 2
$namelength = strlen($name);
if($namelength > 2){
}else{
$namevalidation['Failed Condition'][]['name'][]['Minimum Charector'] = "Condition Failed" ;
}
if(!empty($namevalidation)){
echo json_encode($namevalidation);
}
}else{
$mandatory['status'] = "Fail" ;
$mandatory['Error Message'] = "Mandatory fields mismatch";
if(empty($name)){
$mandatory['Required Fields'][] = "Name is mandatory";
}
if(empty($email)){
$mandatory['Required Fields'][] = "Email is mandatory";
}
if(empty($phone)){
$mandatory['Required Fields'][] = "Phone is mandatory";
}
if(!empty($mandatory)){
echo json_encode($mandatory);
}
}
?>
Expected Output
{
"status": "Fail",
"Failed Condition": [
{
"name": [
{
"Name should not be a number": "Condition Failed"
},
{
"Minimum Charector": "Condition Failed"
}
]
}
]
}
But i am getting out put like this
{
"status": "Fail",
"Failed Condition": [
{
"name": [
{
"Name should not be a number": "Condition Failed"
}
]
},
{
"name": [
{
"Minimum Charector": "Condition Failed"
}
]
}
]
}