final function validate(Form $owner) { $value = @$_REQUEST[$this->GetName()]; if ($this->getFilterContainer()->validate($value)) { $errors = $this->getFilterContainer()->getErrors(); foreach ($errors as $error) { $owner->addError($error, true); } } }
public function onChangeLoginFormSubmit(Form $form) { if (!$form->isValid()) { return; } if ($form['old_password']->getValue() !== Environment::expand('%adminPassword%')) { $form->addError(__('Bad old password.')); return; } $content = "<?php\nreturn " . var_export(array('username' => $form['username']->getValue(), 'password' => $form['new_password']->getValue()), TRUE) . ";\n"; if (!@file_put_contents(Environment::expand('%adminLoginFile%'), $content)) { $form->addError(__('Cannot write new login settings.')); return; } Environment::getUser()->signOut(TRUE); adminlog::log(__('Changed login credentials, logging out')); $this->redirect('this'); $this->terminate(); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionNew() { $model = new Form(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Form'])) { $model->attributes = $_POST['Form']; $model->TABLE_NAME = str_replace(' ', '_', $model->FORM_NAME); $model->TABLE_NAME = strtoupper($model->TABLE_NAME); if (!Yii::app()->db->schema->getTable($model->TABLE_NAME)) { if ($model->save()) { Yii::app()->db->createCommand()->createTable($model->TABLE_NAME, array('ID' => 'pk', 'FORM_ID' => 'INT(10)', 'CREATED_BY' => 'VARCHAR(255)', 'LAST_MODIFIED_BY' => 'VARCHAR(255)', 'CREATED_DATE' => 'TIMESTAMP NOT NULL DEFAULT "0000-00-00 00:00:00"', 'LAST_MODIFIED_DATE' => 'TIMESTAMP NOT NULL DEFAULT "0000-00-00 00:00:00" ON UPDATE CURRENT_TIMESTAMP')); $this->redirect(array('fields/index', 'form' => $model->FORM_ID)); } } else { $model->addError('TABLE_NAME', 'Table "' . $model->TABLE_NAME . '" already used. Pick a new table name.'); } } $this->render('new', array('model' => $model)); }
/** * @abstract Add a new page * @access public */ public function add() { $form = new Form('pages'); // process the form if submitted if ($form->isSubmitted()) { $form->setCurrentValue('page_sort_order', $model->quickValue('SELECT MAX(page_sort_order) FROM pages', 'MAX(page_sort_order)') + 1); // form field validation if (!$form->isFilled('page_title')) { $form->addError('page_title', 'You must enter a page title.'); } // if we have no errors, save the record if (!$form->error()) { // set the link text field to the page title if blank if (!$form->isFilled('page_link_text')) { $form->setCurrentValue('page_link_text', $form->cv('page_title')); } return $form->save(); } } return false; }
public function onTitlePageFormSubmit(Form $form) { if (!$form->isValid()) { return; } $content = "<?php\nreturn "; $content .= var_export($form['content']->getValue(), TRUE); $content .= ";"; if (!@file_put_contents('safe://' . Environment::expand('%titlePageFile%'), $content)) { $form->addError(__('Cannot write to file.')); return; } adminlog::log(__('Updated title page'), $form['content']->getValue()); $this->redirect('this'); $this->terminate(); }
public function onSendMailFormSubmit(Form $form) { if (!$form->isValid()) { return; } $active = FALSE; try { dibi::begin(); $active = TRUE; mapper::order_emails()->insertOne(array('order_id' => $form['order_id']->getValue(), 'subject' => $form['subject']->getValue(), 'body' => $form['body']->getValue())); $mail = new Mail(); $mail->setFrom(Environment::expand('%shopName% <%shopEmail%>'))->addTo($form['to']->getValue())->setSubject($form['subject']->getValue())->setBody($form['body']->getValue())->send(); adminlog::log(__('Sent e-mail to "%s" with subject "%s"'), $form['to']->getValue(), $form['subject']->getValue()); $this->redirect('this'); $this->terminate(); } catch (RedirectingException $e) { dibi::commit(); throw $e; } catch (Exception $e) { if ($active) { dibi::rollback(); } $form->addError(__('Cannot send e-mail.')); } }
/** * Submits the form data to the API and returns info for use by register()! * * Should an error occur will this method append an error message to the form's error collection. * * @param Form $form * * @return mixed */ protected function registerUserUsingForm($form) { $values = $form->getData(); $userApi = $this->getUserApi(); $result = false; try { $result = $userApi->register($values); } catch (\Exception $e) { $form->addError(new FormError('An error occurred while registering you: ' . $e->getMessage())); } return $result; }
/** * @abstract Edits an event recprd * @param integer $id * @access public */ public function edit($id = false) { $form = new Form('courses', $id); // grab existing groups settings $model = model()->open('course_groups_link'); $model->where('course_id', $id); $group_records = $model->results(); $groups = array(); if ($group_records) { foreach ($group_records as $course_record) { $groups[] = $course_record['group_id']; } } $form->addField('groups', $groups, $groups); // proces the form if submitted if ($form->isSubmitted()) { // validation if (!$form->isFilled('title')) { $form->addError('title', 'You must enter a course title.'); } // if we have no errors, process sql if (!$form->error()) { if ($res_id = $form->save($id)) { $id = $id ? $id : $res_id; // update course groups $model->delete('course_groups_link', $id, 'course_id'); $groups = $form->cv('groups'); foreach ($groups as $group) { $sql = sprintf('INSERT INTO course_groups_link (course_id, group_id) VALUES ("%s", "%s")', $id, $group); $model->query($sql); } // if successful insert, redirect to the list sml()->say('The course has successfully been saved.'); router()->redirect('view'); } } } $data['form'] = $form; template()->addView(template()->getTemplateDir() . DS . 'header.tpl.php'); template()->addView(template()->getModuleTemplateDir() . DS . 'edit.tpl.php'); template()->addView(template()->getTemplateDir() . DS . 'footer.tpl.php'); template()->display($data); }
/** * The callback used by the form class. This callback is only called when * the add or edit controller actions are performed. * * @param array $data The data from the form * @param Form $form an instance of the form * @param mixed $c Specific data from the form, this normally includes an instance of the controller * @param boolean $redirect If true the controller redirects the page after execution * @see ModelController::$callbackFunction * @return boolean */ public static function callback($data, &$form, $c, $redirect = true, &$id = null) { switch ($c["action"]) { case "add": $return = $c["instance"]->model->setData($data); if ($return === true) { $id = $c["instance"]->model->save(); User::log($c["success_message"], $data); if ($redirect) { Application::redirect($c["instance"]->urlPath . "?notification=" . urlencode($c["success_message"])); } else { return true; } } else { $fields = array_keys($return["errors"]); foreach ($fields as $field) { foreach ($return["errors"][$field] as $error) { $element = $c["form"]->getElementByName($field); $element->addError(str_replace("%field_name%", $element->getLabel(), $error)); } } foreach ($return['errors'] as $fieldName => $error) { $form->addError(str_replace("%field_name%", str_replace('_', ' ', $fieldName), $error)); } } break; case "edit": $return = $c["instance"]->model->setData($data, $c["key_field"], $c["key_value"]); if ($return === true) { $c["instance"]->model->update($c["key_field"], $c["key_value"]); User::log($c["success_message"], $data); if ($redirect) { Application::redirect($c["instance"]->urlPath . "?notification=" . urlencode($c["success_message"])); } else { return true; } } else { $fields = array_keys($return["errors"]); self::$pendingErrors = $return['errors']; foreach ($fields as $field) { foreach ($return["errors"][$field] as $error) { $element = $c["form"]->getElementByName($field); $element->addError(str_replace("%field_name%", $element->getLabel(), $error)); } } foreach ($return['errors'] as $fieldName => $error) { $form->addError(str_replace("%field_name%", str_replace('_', ' ', $fieldName), $error)); } } break; } }
public function onImportManufacturersFormSubmit(Form $form) { if (!$form->isValid()) { return; } // read imported manufacturers if (!($handle = @fopen('safe://' . $form['file']->getValue()->getTemporaryFile(), 'r'))) { $form->addError(__('Cannot read file.')); return; } $import = array(); while (($_ = fgetcsv($handle)) !== FALSE) { $manufacturer = array(); list(, $manufacturer['name'], $manufacturer['nice_name']) = $_; $import[] = $manufacturer; } fclose($handle); adminlog::log(__('About to import manufacturers')); $manufacturers_added = 0; // import them foreach ($import as $manufacturer) { if (($_ = mapper::manufacturers()->findByNiceName($manufacturer['nice_name'])) === NULL) { mapper::manufacturers()->insertOne($manufacturer); $manufacturers_added++; } } adminlog::log(__('Added %d new manufacturers'), $manufacturers_added); }
/** * @covers Form::setErrorCode */ public function testSetErrorCode() { $this->myForm->addError('fail'); $this->myForm->setErrorCode('fail', 'This field is required'); $this->assertEquals('This field is required', $this->myForm->getError(0)); }