I have a problem with ZF2 RecordExists method. I will explain my problematic case/scenario.
Table: users
Columns: id, emailaddress, websitename
Sample Records:
1, user1@email.com, 1site.com
2, user2@email.com, 1site.com
3, user3@email.com, 2site.com
4, user4@email.com, 2site.com
5, user5@email.com, 1site.com
6, user6@email.com, 3site.com
7, user7@email.com, 4site.com
I am using the following snippet for already exist condition.
//Check that the email address exists in the database
$validator = new Zend\Validator\Db\RecordExists(
array(
'table' => 'users',
'field' => 'emailaddress'
)
);
if ($validator->isValid($emailaddress)) {
// email address appears to be valid
} else {
// email address is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
As per the above snippets, user1@email.com cannot register again. Because, that emailaddress is exist in table.
But, i would like to do register with 2site.com. Because, user1@email.com is in 1site.com.
So, user1@email.com cannot register with 1site.com again. But, user1@email.com can register with 2site.com.
How is it possible? Let me know your suggestions.
There are two way to do this.
First using Excluding Record methods
Where you exclude the record of websitename field value.
Second by writing your own custom validtor.
You need to extend AbstractDB class and create your own class on directions of RecordExists Class. In your own cust class your can define your own query and pass it to isValid function.
I have created a Custom Validator oposite to Exclude, which is include.
include is reserve word, Now sure if it will work.
check it here
More readings on this
Guidlines 1
Please have look to existing validtor for creating your own custom validatorCustom validator guildline
Chain validator 2