Thursday, 22 August 2013

Reading all arrays

Reading all arrays

Im making a simple validation form system (over my raw php MCV framework)
on PHP but Im having an issue I cant fix
I have this code
$credentials = array('name' => 'required', 'password' =>
'required|between');
$validator = new Validation;
if (!$validator->check($credentials, array(5, 10)))
Redirect::to('/login', 'error', $validator->msg);
Then my class calidator looks like
public function check($fields, $size)
{
foreach ($fields as $key => $val)
{
$rule = explode('|', $val);
if (in_array('required', $rule))
{
if (empty($_POST[$key]))
{
$this->msg = 'Field '.$key.' is required';
$this->final = false;
}
}
elseif (in_array('between', $rule))
{
if (strlen($_POST[$key]) < $size[0])
{
$this->msg = 'Filed '.$key.' must be between '.$size[0].'
and '.$size[1].' chars';
$this->final = false;
}
if (strlen($_POST[$key]) > $size[1])
{
$this->msg = 'Filed '.$key.' must be between '.$size[0].'
and '.$size[1].' chars';
$this->final = false;
}
}
}
if (!$final)
{
return false;
}
else
{
return true;
}
}
The thing is that when I send the array like this ( with just 1 rule )
$credentials = array('name' => 'required', 'password' => 'between');
It works fine but if I add more rules (required|between ...) my function
will just work with the first one so in this case the between rule is
ignored...

No comments:

Post a Comment