/**
  * Validate the form
  *
  * @return	void
  */
 private function validateForm()
 {
     // is the form submitted?
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // required fields
         $this->frm->getField('file')->isFilled(BL::err('FieldIsRequired'));
         $this->frm->getField('label')->isFilled(BL::err('FieldIsRequired'));
         $this->frm->getField('format')->isFilled(BL::err('FieldIsRequired'));
         // loop the know fields and validate them
         for ($i = 1; $i <= $this->frm->getField('num_blocks')->getValue(); $i++) {
             $this->frm->getField('name_' . $i)->isFilled(BL::err('FieldIsRequired'));
         }
         // validate syntax
         $syntax = trim(str_replace(array("\n", "\r"), '', $this->frm->getField('format')->getValue()));
         // init var
         $table = BackendPagesModel::templateSyntaxToArray($syntax);
         $cellCount = 0;
         $first = true;
         // loop rows
         foreach ($table as $row) {
             // first row defines the cellcount
             if ($first) {
                 $cellCount = count($row);
             }
             // not same number of cells
             if (count($row) != $cellCount) {
                 // add error
                 $this->frm->getField('format')->addError(BL::err('InvalidTemplateSyntax'));
                 // stop
                 break;
             }
             // reset
             $first = false;
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // build array
             $item['theme'] = $this->frm->getField('theme')->getValue();
             $item['label'] = $this->frm->getField('label')->getValue();
             $item['path'] = 'core/layout/templates/' . $this->frm->getField('file')->getValue();
             $item['num_blocks'] = $this->frm->getField('num_blocks')->getValue();
             $item['active'] = $this->frm->getField('active')->getChecked() ? 'Y' : 'N';
             $item['data']['format'] = trim(str_replace(array("\n", "\r"), '', $this->frm->getField('format')->getValue()));
             // loop fields
             for ($i = 1; $i <= $this->frm->getField('num_blocks')->getValue(); $i++) {
                 $item['data']['names'][] = $this->frm->getField('name_' . $i)->getValue();
                 $item['data']['default_extras'][] = $this->frm->getField('type_' . $i)->getValue();
                 $item['data']['default_extras_' . BackendLanguage::getWorkingLanguage()][] = $this->frm->getField('type_' . $i)->getValue();
             }
             // serialize the data
             $item['data'] = serialize($item['data']);
             // insert the item
             $item['id'] = BackendPagesModel::insertTemplate($item);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add_template', array('item' => $item));
             // set default template
             if ($this->frm->getField('default')->getChecked() && $item['theme'] == BackendModel::getModuleSetting('core', 'theme', 'core')) {
                 BackendModel::setModuleSetting($this->getModule(), 'default_template', $item['id']);
             }
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('templates') . '&theme=' . $item['theme'] . '&report=added-template&var=' . urlencode($item['label']) . '&highlight=row-' . $item['id']);
         }
     }
 }