/** * When calling add() on this field, it will delegate the call back up to * the associated \Dewdrop\Fields object. This allows for a very fluid * method chaining style when defining a large set of fields. * * @param mixed $field * @param string $modelName * @throws Exception * @return mixed */ public function add($field, $modelName = null) { if (!$this->fieldsSet) { throw new Exception('Cannot add field because no \\Dewdrop\\Fields object is available'); } return $this->fieldsSet->add($field, $modelName); }
/** * @expectedException \Dewdrop\Exception */ public function testUsingDifferentModelsWithTheSameNameThrowsException() { require_once __DIR__ . '/Db/table/DewdropTestFruits.php'; $fruits = new \DewdropTest\DewdropTestFruits(); require_once __DIR__ . '/Db/table/DewdropTestAnimals.php'; $animals = new \DewdropTest\DewdropTestAnimals(); $this->fields->add($fruits->field('name'), 'duplicate')->add($animals->field('name'), 'duplicate'); }
/** * Create any resources that need to be accessible both for processing * and rendering. * * @return void */ public function init() { $this->model = Pimple::getResource('users-gateway'); $this->fields = new Fields(); $this->row = $this->model->find($this->request->getQuery('user_id')); $this->fields->add('password')->setLabel('Password')->setEditable(true)->add('confirm_password')->setLabel('Confirm Password')->setEditable(true); $this->inputFilter = new InputFilter(); $password = new Input('password'); $this->inputFilter->add($password); $password->setRequired(true)->getValidatorChain()->attach(new StringLength(array('min' => 6))); $confirm = new Input('confirm_password'); $this->inputFilter->add($confirm); $validator = new Callback(array('callback' => function ($value) { return $value === $this->request->getPost('password'); })); $validator->setMessage('Passwords do not match.'); $confirm->setRequired(true)->getValidatorChain()->attach($validator); }
/** * Filter the supplied Fields object using the filter's callback. * * @param Fields $fields * @return Fields */ public function apply(Fields $fields) { $filteredFields = new Fields([], $fields->getUser()); foreach ($fields as $field) { if (true === call_user_func($this->callback, $field)) { $filteredFields->add($field); } } return $filteredFields; }
private function getFields() { $fields = new Fields(); $fields->add('one')->setEditable(true)->setLabel('One')->assignHelperCallback('EditControl.Control', function ($helper, $view) { return 'FIELD_ONE'; })->add('two')->setEditable(true)->setLabel('Two')->assignHelperCallback('EditControl.Control', function ($helper, $view) { return 'FIELD_TWO'; })->add('three')->setEditable(false)->setLabel('Not Editable'); return $fields; }
public function testSavingWillTraverseLinkedFieldsToHookRowsTogether() { $db = Pimple::getResource('db'); $db->query('DELETE FROM dewdrop_test_fruits'); $animalModel = new RowEditorAnimalModel(); $this->fields->add($animalModel->field('name')); $this->fields->add($animalModel->field('is_fierce')); $this->fields->add($animalModel->field('is_cute')); $this->rowEditor->linkByQueryString('dewdrop_test_animals', 'dewdrop_test_animal_id'); $this->rowEditor->linkByField('dewdrop_test_fruits', $animalModel->field('favorite_fruit_id')); $this->rowEditor->link(); $this->assertTrue($this->rowEditor->isValid(array('dewdrop_test_fruits:name' => 'Banana', 'dewdrop_test_fruits:level_of_deliciousness' => 8, 'dewdrop_test_animals:name' => 'Gorilla', 'dewdrop_test_animals:is_fierce' => 1, 'dewdrop_test_animals:is_cute' => 1))); $this->rowEditor->save(); $this->assertEquals($db->fetchOne('SELECT MAX(dewdrop_test_fruit_id) FROM dewdrop_test_fruits'), $db->fetchOne('SELECT favorite_fruit_id FROM dewdrop_test_animals ORDER BY dewdrop_test_animal_id DESC LIMIT 1')); }
public function testCanOptionallySupplyCustomRenderer() { $fields = new Fields(); $fields->add('custom')->setVisible(true)->setLabel('Custom')->assignHelperCallback('CsvCell.Content', function ($helper, $rowData) { return 'shouldnotberendered'; })->add('not_visible')->setVisible(false)->setLabel('Unseen')->assignHelperCallback('CsvCell.Content', function ($helper, $rowData) { return 'content.unseen'; }); $renderer = $this->view->csvCellRenderer(); $renderer->getContentRenderer()->assign('custom', function ($helper, $rowData) { return 'customrendering'; }); $output = $this->csvExportViewHelper->direct($fields, array(array('test_field' => 'FAFAFAFA')), $renderer); $this->assertContains('customrendering', $output); $this->assertNotContains('shouldnotberenderered', $output); }
private function buildFields() { $fields = new Fields(); $fields->add('file')->setLabel('File')->setVisible(true)->assignHelperCallback('TableCell.Content', function (TableCell $helper, array $rowData) { if (!isset($rowData['file'])) { return null; } return $helper->getView()->escapeHtml($rowData['file']); })->add('line')->setLabel('Line')->setVisible(true)->assignHelperCallback('TableCell.Content', function (TableCell $helper, array $rowData) { if (!isset($rowData['line'])) { return null; } return $helper->getView()->escapeHtml($rowData['line']); })->add('function')->setLabel('Function')->setVisible(true)->assignHelperCallback('TableCell.Content', function (TableCell $helper, array $rowData) { $out = ''; if (!isset($rowData['class'])) { $out .= $helper->getView()->escapeHtml($rowData['function']); } else { $out .= $helper->getView()->escapeHtml("{$rowData['class']}{$rowData['type']}{$rowData['function']}"); } $out .= '('; $argStrings = []; foreach ($rowData['args'] as $arg) { if (is_array($arg)) { $argStrings[] = 'Array'; } elseif (is_object($arg)) { $argStrings[] = get_class($arg); } else { $argStrings[] = var_export($arg, true); } } $out .= implode(', ', $argStrings); $out .= ')'; return $out; }); return $fields; }
/** * Build a Fields object that can be used for displaying or editing * subscriptions. * * @todo Refactor this into a separate class. * * @param string $editUrl * @param Fields $componentFields * @return Fields */ public function buildFields($editUrl, Fields $componentFields) { $fields = new Fields(); $fields->add('recipients')->setLabel('Recipients')->setNote('Enter one or more email addresses separated by commas.')->setVisible(true)->assignHelperCallback('TableCell.Content', function (TableCellHelper $helper, array $rowData) use($editUrl) { return $helper->getView()->escapeHtml($this->renderRecipients($rowData['dewdrop_notification_subscription_id'])); })->setEditable(true)->assignHelperCallback('EditControl.Control', function ($helper, View $view) { return $view->inputText('recipients', $this->renderRecipients($view->getRequest()->getQuery('dewdrop_notification_subscription_id')), 'form-control'); })->assignHelperCallback('InputFilter', function ($helper) { $input = new Input('recipients'); $input->setAllowEmpty(false); return $input; })->add($this->field('dewdrop_notification_frequency_id'))->add('fields')->setLabel('Which fields would you like to include in the notification emails?')->setEditable(true)->assignHelperCallback('EditControl.Control', function ($helper, View $view) use($componentFields) { $options = array(); foreach ($componentFields->getVisibleFields() as $id => $field) { $options[$id] = $field->getLabel(); } return $view->checkboxList('fields', $options, $this->getSelectedFields($view->getRequest()->getQuery('dewdrop_notification_subscription_id'), $options)); })->assignHelperCallback('InputFilter', function ($helper) { $input = new Input('fields'); $input->setAllowEmpty(false); return $input; }); return $fields; }
/** * Get any fields that return true when the supplied method is called and pass * the supplied filters. Note that you can either pass a single * \Dewdrop\Fields\Filter or an array of them. * * @param string $fieldMethodName * @param mixed $filters * @return Fields */ protected function getFieldsPassingMethodCheck($fieldMethodName, $filters) { $fields = new Fields([], $this->user); foreach ($this->fields as $field) { if ($field->{$fieldMethodName}($this->user)) { $fields->add($field); } } return $this->applyFilters($fields, $filters); }
/** * Add a field to this group and the underlying GroupedFields object. * * @param mixed $field * @param string $modelName * @return FieldInterface */ public function add($field, $modelName = null) { $this->groupedFields->add($field, $modelName); return parent::add($field, $modelName); }
private function getFields() { $fields = new Fields(); $fields->add($this->model->field('name'))->add($this->model->field('is_delicious')); return $fields; }
/** * Add password and confirmation fields to the given fields collection * * @param Fields $fields * @return Component */ protected function addPasswordFields(Fields $fields) { static $passwordFieldName = 'password', $confirmFieldName = 'confirm_password', $minimumLength = 6; $request = $this->getRequest(); $fields->add($passwordFieldName)->assignHelperCallback('EditControl.Control', function ($helper, View $view) use($passwordFieldName, $request) { return $view->inputText(['name' => $passwordFieldName, 'type' => 'password', 'value' => $request->getPost($passwordFieldName)]); })->assignHelperCallback('InputFilter', function () use($passwordFieldName, $minimumLength) { $input = new Input($passwordFieldName); $input->setRequired(true)->getValidatorChain()->attach(new StringLength(['min' => $minimumLength])); return $input; })->setLabel('Password')->setEditable(true); $this->fields->add('confirm_password')->assignHelperCallback('EditControl.Control', function ($helper, View $view) use($confirmFieldName, $request) { return $view->inputText(['name' => $confirmFieldName, 'type' => 'password', 'value' => $request->getPost($confirmFieldName)]); })->assignHelperCallback('InputFilter', function () use($request, $passwordFieldName, $confirmFieldName) { $input = new Input($confirmFieldName); $passwordsMatchValidator = new Callback(['callback' => function ($value) use($request, $passwordFieldName) { return $value === $request->getPost($passwordFieldName); }]); $passwordsMatchValidator->setMessage('Passwords do not match.'); $input->setRequired(true)->getValidatorChain()->attach($passwordsMatchValidator); return $input; })->setLabel('Confirm Password')->setEditable(true); return $this; }
public function testAddingFieldToAdditionalFieldsetDoesNotOverrideOriginalSet() { $orig = new Fields(); $orig->add($this->field); $second = new Fields(); $second->add($this->field); $this->field->add('test'); $this->assertTrue($orig->has('test')); $this->assertEquals(2, count($orig)); }
/** * Build a Fields object that can be used when rendering the grouped counts. Supplies a field * to render the actual HTML value from the original Fields object used when fetching data for * the listing and a field to render the count for that value. We don't do any escaping here * because we assume that the original Fields object handled that in its rendering code. The * fields object returned from this method supports both TableCell and CsvCell rendering. * * @return Fields */ public function buildRenderFields() { $fields = new Fields(); $fields->add('content')->setLabel($this->groupField->getLabel())->setVisible(true)->assignHelperCallback('TableCell.Content', function (TableCell\Content $helper, array $rowData) { // Not escaping here because we assume it was escaped by the original renderer in fetchData() return $rowData['content']; })->assignHelperCallback('CsvCell.Content', function (CsvCell\Content $helper, array $rowData) { return $rowData['content']; })->add('count')->setLabel('Count')->setVisible(true)->assignHelperCallback('TableCell.Content', function (TableCell\Content $helper, array $rowData) { return $helper->getView()->escapeHtml($rowData['count']); })->assignHelperCallback('CsvCell.Content', function (CsvCell\Content $helper, array $rowData) { return $rowData['count']; }); return $fields; }