form: Alter your filter form¶
Customizing the filter form for your search can be done by implementing the form() method in your Chado Custom Search class.
/**
* Generate the filter form.
*
* The base class will generate textfields for each filter defined in $info,
* set defaults, labels and descriptions, as well as, create the search
* button.
*
* Extend this method to alter the filter form.
*/
public function form($form, $form_state) {
// This allows the base class to generate your form for you.
$form = parent::form($form, $form_state);
// Now just alter the elements you want to change.
}
The base class will use the information provided in $info['filters'] to generate text fields for each filter defined. The key for the textfield will be the key provided in $info['filters']. Now you can use the Drupal Form API to alter the form array generated by the base class to customize your form.
For example, the following will change the genus and species filters on our organism search to drop-downs with values present in the database. We did not do dependant drop-downs in the following example for simplicity.
public function form($form, $form_state) {
// This allows the base class to generate your form for you.
$form = parent::form($form, $form_state);
// Now just alter the elements you want to change.
// Genus: drop-down.
$options = chado_query('SELECT genus FROM {organism} GROUP BY genus')->fetchAllKeyed(0,0);
$form['genus']['#type'] = 'select';
$form['genus']['#options'] = $options;
// Species: drop-down.
$options = chado_query('SELECT species FROM {organism} GROUP BY species')->fetchAllKeyed(0,0);
$form['species']['#type'] = 'select';
$form['species']['#options'] = $options;
// Always make sure to return the form!
return $form;
}
validateForm: Validate user search criteria!¶
If you want to validate the values submitted by users as search criteria, implement validateForm() in your Chado Custom Search class. You can access the form values through the $form_state['values'] variable. If they don’t match expectations, you can reject them with a message using form_set_error().
public function validateForm($form, $form_state) {
$values = $form_state['values'];
if (empty($values['my element'])) {
form_set_error('my element', 'My Element is required.');
}
}