Using the Zend Framework Db_RecordExists and Db_NoRecordExists Validators

Written by James Mansson on January 29, 2013 Categories: Databases, Zend Framework 1

When validating data submitted via a form on a web page, a common task is to check whether certain fields are or are not in a particular database table. There are two basic scenarios:

  • The user is adding a new record. One of the fields entered will be the primary key of a new record in a table. Clearly that primary key should not already be in the table.
  • The user is selecting an existing record. One of the fields entered is the primary key of an existing record in a table. Clearly that primary key should already be in the table.

The Zend Framework provides two validators to handle these scenarios: Db_NoRecordExists and Db_RecordExists.

The Db_NoRecordExists validator is used to check that the value being validated is not to be found in the specified fields of a database table. The Db_RecordExists validator is used to check that the value being validated can be found in the specified fields of a database table.

Recently, I needed to make use of the Db_NoRecordExists validator on several forms that were used to add database records. For instance, there was a form used to add a machine to the machine table in the database. I used Zend_Form to handle the form rendering and processing. The form itself was declared in an INI file. The relevant section for the serial number field, which was the primary key of the table, was as follows:

; Serial Number (text element)
elements.serial_number.type = "text"
elements.serial_number.options.label = "Serial Number"
elements.serial_number.options.maxLength = 64
elements.serial_number.options.required = true
elements.serial_number.options.validators.dbNoRecordExists.validator = "Db_NoRecordExists"
elements.serial_number.options.validators.dbNoRecordExists.options.table = "machine"
elements.serial_number.options.validators.dbNoRecordExists.options.field = "serial_number"
elements.serial_number.options.errorMessages.required = "Please enter a serial number that is not already in the database."

Note how validators with parameters are declared using the INI file. In the options array there is a validators array; this contains an entry for each validator. This entry is in itself an array, consisting of the validator name and the validator options array. The validator options array should contain an entry for each option that is relevant to this particular validator, in this case the database table and field.

It would also be possible to create the above validator using PHP code:

$validator = new Zend_Validate_Db_NoRecordExists(
	array(
		'table' => 'machine',
		'field' => 'serial_number'
	)
);

This validator could then be used directly, or attached to a Zend_Form_Element as part of creating a form.

No Comments on Using the Zend Framework Db_RecordExists and Db_NoRecordExists Validators

Leave a Reply

Your email address will not be published. Required fields are marked *