Hi guys,
with your suggestions, I solved the problem in the following way, using loadBy instead of try/catch:
protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
//$withDuplicates stores the result of validation on duplicate entries
$withDuplicates= false;
//first validation for the form field "AssignedCardNumber"
$obj = People::LoadByAssignedCardNumber($this->txtAssignedCardNumber->Text);
if (is_object($obj) && $obj->Id != $this->lblId->Text){
$this->txtAssignedCardNumber->Warning = 'value already used';
$withDuplicates = TRUE;
}
//second validation for the form field "Email"
$obj = People::LoadByEmail($this->txtEmail->Text);
if (is_object($obj) && $obj->Id != $this->lblId->Text) {
$this->txtEmail->Warning = 'value already used';
$withDuplicates = TRUE;
}
//if duplicates were found, saving function is aborted
if ($withDuplicates)
return;
//no duplicates, go on saving...
// Delegate "Save" processing to the PeopleMetaControl
$this->mctPeople->SavePeople();
$this->RedirectToListPage();
}
the following condition
(is_object($obj) && $obj->Id != $this->lblId->Text)
checks if the validation on the field with unique constraint fails: the condition is true if simultaneously:
- there is still a person in the table People with the 'mail' or 'assignedCardNumber' entered
- and this person is not the person that is currently under editing.
It could be very useful making Qcodo automatically generate this validation-code for any UNIQUE CONSTRAINT in the fields of a DB table
Thank you!