joomla form validation in controller

// GET THE DATA FROM THE POSTED FORM
$data = JRequest::getVar(‘jform’, array(), ‘post’, ‘array’);
// USE THE MODEL CLASS TO LOAD THE FORM OBJECT.
$form = $this->getForm($data);
// GET THE VALUE OF A SPECIFIC FIELD
$a = $form->getValue(‘a’);
// IF THE VALUE OF a IS ZERO, THEN b IS NOT REQUIRED
if((int)$a == 0){
$form->setFieldAttribute(‘b’, ‘required’, ‘false’);
}
// VALIDATE THE FORM BASED ON CHANGES YOU MADE
$form->validate($data);