I am trying this code:
<?php
$form = $_POST['myformdata'];
class validacoes {
function validate_year($form) {
$input_datas = $form['data'];
foreach($input_datas as $val){
if($val>1930 && $val<2012){
echo "correct";
}
else
echo "bad"; //show bad bad
}
}
}
$val = new validacoes();
$data = array();
var_dump($form['data']);
try {
if (!empty($form['data'])){// why this is true ?
$data['livre'] = $val->validate_year($form);
}
else
echo "empty";
} catch (Exception $e) {
$data['livre'] = $e->getMessage();
}
echo json_encode($data);
?>
//var_dump
var_dump($form['data']);
array
0 => string '' (length=0)
1 => string '' (length=0)
Why the function validate_year($form)
is running without any input ? should be empty, correct ?
It is not empty -- it is an array with 2 empty elements.
You posted the information yourself. $form['data']
contains an array of two objects. Therefore it isn't empty.
Maybe you want
if (!empty($form['data'][0]) && !empty($form['data'][1]))