I am validating two variables of an Identification card (cedula
in the code). The first one is a string and the second is an array.
My inputs are:
- 1- e.g.
3-728-2208
- 2- e.g.
array('1' => '3-728-2208', '2' => '1-728-2208')
My method is :
public function validateId($cedula)
{
$rules = ['cedula' => 'cedula'];
$input = ['cedula' => $cedula];
if(is_array($cedula))
{
$validator = $this->validateIdIsArray($cedula);
} else {
$validator = Validator::make($input, $rules);
}
return !($validator->fails());
}
public function validateIdIsArray($cedula)
{
$rules = ['cedula' => 'cedula'];
foreach ($cedula as $ids) {
$input = ['cedula' => $ids];
$validator = Validator::make($input, $rules);
}
return !($validator->fails());
}
My method from Rule:
public function passes($attribute, $value)
{
return preg_match('/^((\d{1,2}|N|E|PE|\d{1,2}PI)-\d{1,5}-\d{1,6})$/u', $value);
}