/** * @param string[][] $formDataSet * @param (string[]|string)[][] $expectResult * @dataProvider dictionaryProvider */ public function testEncode(array $formDataSet, array $expectResult) { $formData = new FormData(); foreach ($formDataSet as $entry) { $formData->append($entry['name'], $entry['value'], $entry['filename'] ?? null, $entry['type'] ?? null); } $this->assertEquals($expectResult, (new \h4cc\Multipart\ParserSelector())->getParserForContentType(str_replace('content-type: ', '', $formData->getContentType()))->parse($formData->encode())); }
public function submitAction() { $loginData = new FormData(['email', 'password', 'return']); $loginData->retrieve(); $success = Auth::attemptLogin(@$loginData->email, @$loginData->password); if (!$success) { $message = 'Incorrect login details, please try again.'; } else { HTTP::redirect(@$loginData->return); } $pageTitle = 'Log In'; $this->display('login.tpl', get_defined_vars()); }
public function __construct($data = array()) { parent::__construct($data); $this->_multipartBoundary = md5(microtime(true)); $this->_files = new ArrayObject(); }
public function editSubmitAction() { $db = Env::get('db'); $db->begin(); $entity = $this->entity; $item = $entity::createById(@$_GET['id']); $formData = new FormData($this->editFields); $formData->retrieve(); $this->beforeSave($item, $formData); $item->setData($formData->getValues()); $item->save(); $this->afterSave($item); $db->commit(); HTTP::redirect($this->url('edit', ['id' => $item->id, 'message' => 'Changes Saved'])); }
<?php /* * Two ways to declare select options. * * Do not mix the two methods as unexpected results can occur. */ // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE); // include the class file include __DIR__ . '/../FormData/FormData.php'; $FormData = new FormData(); try { // create the select form field $FormData->setFields([['type' => 'select', 'label' => 'Options declared as pairs', 'id' => 'pairs', 'option' => ['1' => 'Blue', '2' => 'Red', '3' => 'Yellow', '4' => 'Green', '5' => 'Orange']], ['type' => 'select', 'label' => 'Options declared as sub arrays', 'id' => 'arrays', 'required' => '1', 'option' => [['1', 'Blue'], ['2', 'Red'], ['_disabled', 'Brown'], ['3', 'Green'], ['4', 'Orange'], ['_disabled', ''], ['10', 'Earth'], ['11', 'Wind'], ['12', 'Fire']]]]); } catch (Exception $e) { echo 'Exception caught: ' . $e->getMessage(); exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>FormData: Select options</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
public function editAction() { $entity = $this->entity; $item = $entity::createById(@$_GET['id']); $formData = new FormData($this->editFields); $formData->setValues($item); $this->beforeEditOrAdd($item, $formData); $formType = 'Edit'; $formAction = $this->url('edit-submit', ['id' => $item->id]); $this->pageTitle = 'Edit ' . $this->noun; $this->prepareSidebarList(); $displayField = array_keys($this->listFields)[0]; $crudContentTpl = $this->editTpl; $this->display('admin-pages.tpl', get_defined_vars()); }
function subSelect($param = null) { if ($param['r'] === 'app') { foreach ($this->user->appraisee as $username => $fullname) { $data = new FormData($username, $param['uid']); $result = $data->getFormData(); $param['selection'][$username]['fullName'] = $fullname; $param['selection'][$username]['isChanged'] = $result['is_recently_changed_by_self']; $param['selection'][$username]['completeness'] = $result['is_final_by_appraiser']; } $this->content = 'survey/subSelect.php'; $this->header = "surveyHeader.php"; $this->extraCSS = "select_form.css"; $this->view($param); } else { if ($param['r'] === 'counter') { foreach ($this->user->countersignee as $username => $role) { $data = new FormData($username, $param['uid']); $result = $data->getFormData(); $param['selection'][$username]['fullName'] = $this->user->countersigneeFullName[$username]; $param['selection'][$username]['role'] = $role; $param['selection'][$username]['status'] = $result['is_final_by_self'] && $result['is_final_by_appraiser']; } $this->content = 'survey/subSelect.php'; $this->header = "surveyHeader.php"; $this->extraCSS = "select_form.css"; $this->extraJS = array("jquery-2.1.3.js", "jquery-ui.js"); $this->view($param); } else { throw new Exception('Illegal parameter provided.'); } } }
<?php // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE); // include the class file include __DIR__ . '/../FormData/FormData.php'; $FormData = new FormData(); try { // create the form fields and set parameters $FormData->setFields([['type' => 'text', 'label' => 'First name', 'name' => 'first-name', 'required' => '1', 'required_message' => 'We\'d love to know your first name!', 'max_length' => '50'], ['type' => 'select', 'label' => 'Favourite colour', 'name' => 'colour', 'required' => '1', 'option' => ['1' => 'Red', '2' => 'Yellow', '3' => 'Blue']], ['type' => 'checkbox', 'label' => 'Are you OK?', 'name' => 'ok', 'checkbox_value' => 'yes', 'checkbox_value_alt' => 'no']]); // check for form submission if ($_POST['submit'] == 'submit') { // search POST array for all fields that were set above and check submitted data $FormData->checkPost(); /* ********************************* Adding some custom error messages ********************************* */ // add a generic error that is not linked to any fields, these will always be added to the end of the list $FormData->setError(NULL, 'A generic error message'); // perform further, custom checks on submitted data, for example - create an error if 'first-name' is Paul if (!$FormData->hasError('first-name')) { $submitted = $FormData->getClean('first-name'); if (strtolower($submitted) == 'paul') { $FormData->setError('first-name', 'cannot be Paul, sorry'); // message will be prefixed with field label } } // assign an error to a field without generating a message - for example - create an error if the 'colour' is yellow (ID = 2) but the checkbox is not checked if ($FormData->getClean('colour') == '2' && $FormData->getClean('ok') == 'no') { $FormData->setError('colour'); // this field will be highlighted as having an error but no message will be logged, the next line will create the message for both fields $FormData->setError('ok', 'If your favourite colour is yellow then you must be OK', false); // to disable the field label being prefixed, add a 3rd argument 'false'
function outputFile($param = null) { //Init. PHPExcel Plugin require_once ROOT . DS . 'plugin/PHPExcel.php'; require_once ROOT . DS . 'plugin/PHPExcel/IOFactory.php'; //Create reader and writer $reader = PHPExcel_IOFactory::createReader('Excel5'); $excelTemplate = $reader->load(ROOT . DS . "view" . DS . "template" . DS . "PA_Raw.xls"); $writer = PHPExcel_IOFactory::createWriter($excelTemplate, 'Excel2007'); //Check right and param. $data = new FormData($param['u'], $param['uid']); if ($data->isNewForm()) { throw new Exception("Username not found or User have not filled in survey yet"); } //Get Data to be passed into Excel $formDetail = $data->getFormData(); //Bind data to cell $excelTemplate->getProperties()->setCreator("Anthony Poon")->setTitle("PA Form Output"); $sheet = $excelTemplate->getActiveSheet(); $sheet->getCell("A5")->setValue($formDetail['staff_name']); $sheet->getCell("F5")->setValue($formDetail['staff_department']); $sheet->getCell("L5")->setValue($formDetail['staff_position']); $sheet->getCell("A7")->setValue($formDetail['staff_office']); $sheet->getCell("F7")->setValue($formDetail['appraiser_name']); $sheet->getCell("L7")->setValue($formDetail['countersigner_name']); $sheet->getCell("A9")->setValue($formDetail['survey_period']); $sheet->getCell("F9")->setValue($formDetail['survey_commencement_date']); $sheet->getCell("L9")->setValue($formDetail['survey_type']); if (!empty($formDetail['partA'])) { foreach ($formDetail['partA'] as $index => $valueArray) { $row = 21 + ($valueArray['question_no'] - 1); if ($valueArray['question_no'] <= 8) { //max 8 entries for now $sheet->getCell("A" . $row)->setValue($valueArray['respon_name']); $sheet->getCell("D" . $row)->setValue($valueArray['respon_result']); $sheet->getCell("J" . $row)->setValue($valueArray['respon_comment']); if (!empty($valueArray['respon_weight'])) { $sheet->getCell("P" . $row)->setValue($valueArray['respon_weight'] / 100); } if (!empty($valueArray['respon_score'])) { $sheet->getCell("Q" . $row)->setValue($valueArray['respon_score']); } } } } $sheet->getCell("P29")->setValue($formDetail['part_a_overall_score']); $sheet->getCell("G31")->setValue($formDetail['countersigner_1_name']); $sheet->getCell("L31")->setValue($formDetail['countersigner_2_name']); $sheet->getCell("G32")->setValue($formDetail['countersigner_1_part_a_score']); $sheet->getCell("L32")->setValue($formDetail['countersigner_2_part_a_score']); $sheet->getCell("P34")->setValue($formDetail['part_a_total']); if (!empty($formDetail['partB1'])) { foreach ($formDetail['partB1'] as $index => $valueArray) { $row = 42 + ($valueArray['question_no'] - 1) * 5; if ($valueArray['question_no'] >= 7) { $row = $row + 2; } $sheet->getCell("K" . $row)->setValue($valueArray['self_score']); $sheet->getCell("L" . $row)->setValue($valueArray['self_example']); $sheet->getCell("M" . $row)->setValue($valueArray['appraiser_score']); $sheet->getCell("O" . $row)->setValue($valueArray['appraiser_example']); } } $sheet->getCell("B84")->setValue($formDetail['part_b1_overall_comment']); $sheet->getCell("P85")->setValue($formDetail['part_b1_overall_score']); if (!empty($formDetail['partB2'])) { foreach ($formDetail['partB2'] as $index => $valueArray) { $row = 90 + ($valueArray['question_no'] - 1) * 5; $sheet->getCell("K" . $row)->setValue($valueArray['self_score']); $sheet->getCell("L" . $row)->setValue($valueArray['self_example']); $sheet->getCell("M" . $row)->setValue($valueArray['appraiser_score']); $sheet->getCell("O" . $row)->setValue($valueArray['appraiser_example']); } } $sheet->getCell("B105")->setValue($formDetail['part_b2_overall_comment']); $sheet->getCell("P106")->setValue($formDetail['part_b2_overall_score']); $sheet->getCell("G108")->setValue($formDetail['countersigner_1_name']); $sheet->getCell("L108")->setValue($formDetail['countersigner_2_name']); $sheet->getCell("G109")->setValue($formDetail['countersigner_1_part_b_score']); $sheet->getCell("L109")->setValue($formDetail['countersigner_2_part_b_score']); $sheet->getCell("P111")->setValue($formDetail['part_b_total']); $sheet->getCell("P116")->setValue($formDetail['part_a_b_total']); $sheet->getCell("C122")->setValue($formDetail['prof_competency_1']); $sheet->getCell("C123")->setValue($formDetail['prof_competency_2']); $sheet->getCell("C124")->setValue($formDetail['prof_competency_3']); $sheet->getCell("C125")->setValue($formDetail['core_competency_1']); $sheet->getCell("C126")->setValue($formDetail['core_competency_2']); $sheet->getCell("C127")->setValue($formDetail['core_competency_3']); $sheet->getCell("D129")->setValue($formDetail['on_job_0_to_1_year']); $sheet->getCell("D130")->setValue($formDetail['on_job_1_to_2_year']); $sheet->getCell("D131")->setValue($formDetail['on_job_2_to_3_year']); $sheet->getCell("D132")->setValue($formDetail['function_training_0_to_1_year']); $sheet->getCell("D133")->setValue($formDetail['function_training_1_to_2_year']); $sheet->getCell("D134")->setValue($formDetail['function_training_2_to_3_year']); $sheet->getCell("D135")->setValue($formDetail['generic_training_0_to_1_year']); $sheet->getCell("D136")->setValue($formDetail['generic_training_1_to_2_year']); $sheet->getCell("D137")->setValue($formDetail['generic_training_2_to_3_year']); if (!empty($formDetail['partD'])) { foreach ($formDetail['partD'] as $index => $valueArray) { $row = 142 + ($valueArray['question_no'] - 1); if ($valueArray['question_no'] <= 8) { //max 8 entries for now $sheet->getCell("A" . $row)->setValue($valueArray['key_respon']); $sheet->getCell("D" . $row)->setValue($valueArray['goal_name']); $sheet->getCell("G" . $row)->setValue($valueArray['measurement_name']); $sheet->getCell("M" . $row)->setValue($valueArray['goal_weight']); $sheet->getCell("O" . $row)->setValue($valueArray['complete_date']); } } } $sheet->getCell("A152")->setValue($formDetail['survey_overall_comment']); header('Content-type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="PA Form (' . $formDetail['staff_name'] . ').xlsx"'); $writer->save('php://output'); }
</div> </div> </div> </div> </div> </div> <div class='part' id='part_d'> <div class='part_title' id='part_d_title'> Part D: Goals Setting For The Coming Year (Not applicable for Mid-Yearly Appraisal) </div> <p>This part shall be completed by the Appraising Officer and the Employee collaboratively.</p> <div class='section_error'> </div> <?php $data = new FormData($param['u'], $param['uid']); $rowCount = $data->getPartDCount(); if ($rowCount == 0) { $rowCount = 1; } for ($i = 1; $i <= $rowCount; $i++) { ?> <div class='collectionContainer'> <div class='collection delete_button'> <div class='delete_button_d' qid='<?php echo $i; ?> '></div> </div> <div class='collection'>
<?php // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE); // include the class file include __DIR__ . '/../FormData/FormData.php'; $FormData = new FormData(); try { // create the form fields and set parameters $FormData->setFields([['type' => 'text', 'label' => 'First name', 'name' => 'first-name', 'id' => 'first-name', 'required' => '1', 'required_message' => 'We\'d love to know your first name!', 'max_length' => '50'], ['type' => 'number', 'label' => 'Favourite multiple of 5', 'id' => 'number', 'required' => '1', 'min' => '0', 'max' => '100', 'step' => '5'], ['type' => 'select', 'label' => 'Favourite colour', 'id' => 'colour', 'required' => '1', 'option' => ['1' => 'Red', '2' => 'Yellow', '3' => 'Blue']], ['type' => 'checkbox', 'label' => 'Are you OK?', 'id' => 'ok', 'checkbox_value' => 'yes', 'checkbox_value_alt' => 'no']]); // check for form submission if ($_POST['submit'] == 'submit') { // search POST array for all fields that were set above and check submitted data $FormData->checkPost(); if ($FormData->hasError()) { // there was some errors - handle them here // an object containing all error fields with user friendly messages can be obtained as follows $error_obj = $FormData->getErrorList(); // an HTML ready list of error messages (separated with '<br />') can be obtained as follows $error_message = $FormData->getErrorList(true); // just as an example, prepare the data for a confirmation message $form_report = '<div class="alert alert-danger" role="alert"><h4>Form report: Errors!</h4><p>' . $error_message . '</p></div>'; } else { // there were no errors so the information can be processed // example of extracting data from the object $first_name = $FormData->getClean('first-name'); $surname = $FormData->getClean('surname'); $colour = $FormData->getClean('colour'); $ok = $FormData->getClean('ok'); // put the data in a confirmation message $form_report = '<div class="alert alert-success" role="alert">';
public static function credUserFormShortcode($atts) { global $post; if (is_null($post)) { return null; } /** * clone post object to revert after form */ $orginal = clone $post; $params = shortcode_atts(array('form' => '', 'post' => ''), $atts); $type = "edit"; if (empty($params['post'])) { $form = get_page_by_title(html_entity_decode($params['form']), OBJECT, CRED_USER_FORMS_CUSTOM_POST_NAME); require_once 'FormData.php'; $formData = new FormData($form->ID, CRED_USER_FORMS_CUSTOM_POST_NAME, false); $fields = $formData->getFields(); $type = $fields['form_settings']->form['type']; } if ($type == 'edit') { if (empty($params['post'])) { $user_id = get_current_user_id(); if ($user_id == 0) { $out = 'You are currently not logged in.'; } else { $params['post'] = $user_id; $out = self::cred_user_form($params['form'], $params['post']); } } else { $out = self::cred_user_form($params['form'], $params['post']); } } else { $out = self::cred_user_form($params['form'], $params['post']); } wp_reset_query(); /** * revert orginal $post */ $post = $orginal; unset($orginal); return $out; }