SameAs validator compare two fields values. Field is valid if matched field value and value type are the same.
namespace Foo\Forms;
use Phalcon\Forms\Element\Text;
use Vegas\Validation\Validator\SameAs;
class Bar extends \Vegas\Forms\Form
{
public function initialize()
{
$this->add(new Text('email'));
$emailConfirm = new Text('email_confirm');
$emailConfirm->addValidator(new SameAs(['match' => 'email']));
$this->add($emailConfirm);
// ...
}
}
There is possibility to validate values in array. Possible output for this type of validation may look as follows:
use Vegas\Validation\Validator\SameAs;
$validation = new \Phalcon\Validation();
$validation->add('field1', new SameAs(['match' => 'field2']));
$values = array(
'field1' => [
0 => 'foo',
1 => 'bar'
],
'field2' => 'baz'
);
$messages = $validation->validate($values);
// $messages not empty; values not valid
$values = array(
'field1' => [
0 => 'foo',
1 => 'bar'
],
'field2' => 'foo'
);
$messages = $this->validation->validate($values);
// $messages not empty because one of "field1" values do not match "field2" value
$values = array(
'field1' => [
0 => 'foo',
1 => 'foo'
],
'field2' => 'foo'
);
$messages = $this->validation->validate($values);
// $messages empty, both values match field2 value