Skip to content

Instantly share code, notes, and snippets.

@jacricelli
Created February 13, 2017 15:28
Show Gist options
  • Save jacricelli/e9893f3999e5d5091618d2942c559b44 to your computer and use it in GitHub Desktop.
Save jacricelli/e9893f3999e5d5091618d2942c559b44 to your computer and use it in GitHub Desktop.
Agregar opción 'last' a cada regla de validación si ésta no se ha definido
<?php
namespace App\Model\Validation;
use Cake\Validation\Validator;
/**
* [CustomValidator description]
*/
class CustomValidator extends Validator
{
/**
* Añade una nueva regla al conjunto de reglas de un campo
*
* @param string $field Nombre del campo
* @param array|string $name El alias para una única regla o un array para múltiples reglas
* @param array|\Cake\Validation\ValidationRule $rule La regla para agregar
*
* @return $this
*/
public function add($field, $name, $rule = [])
{
if (!is_array($name)) {
$rules = [$name => $rule];
} else {
$rules = $name;
}
foreach ($rules as $name => $options) {
if (!isset($options['last'])) {
$rules[$name]['last'] = true;
}
}
return parent::add($field, $name, $rule);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment