I'm using nested Zend\Form\Fieldsets and Zend\Form\Collections, that provide an extremely comfortable way to map complex object structures to the form, in order to get a complete object (ready to be saved) from the form input.
The element I want to add now to my form should represent a list of possible protocols. In the database it's a simple table with columns id and name and the objects' structure can be described like Endpoint has Protocol[]. I defined a MultiCheckbox (s. below), but I have no idea, how to bind this element to a Protocol prototype. For a Fieldset it works via Fieldset\setObject(...).
How to get the form processing mechanism of Zend\Form creating objects from checkboxes?
Code so far:
EndpointFieldset.php
// namespace ...;
// use ....;
class EndpointFieldset extends Fieldset
{
// ...
public function init()
{
parent::init();
$this->add(
[
'type' => 'multi_checkbox',
'name' => 'protocols',
'options' => [
'label' => _('protocols'),
'label_attributes' => [
'class' => 'col-md-1 protocol-field'
],
'value_options' => $this->getValueOptions(),
'selected' => static::PROTOCOLS_DUMMY_VALUE
]
]
);
}
// ...
protected function getValueOptions()
{
$valueOptions = [];
foreach (Protocol::PROTOCOLS as $key => $value) {
$valueOptions[] = [
'value' => $key,
'label' => $value
];
}
return $valueOptions;
}
}
myform.phml
use Zend\Form\View\Helper\FormMultiCheckbox;
echo $this->formMultiCheckbox($myFieldset->get('protocols'), FormMultiCheckbox::LABEL_PREPEND);
UPDATE
I found a workaround for the saving of a new entry: I simply complete the object provided by the form manually and make Protocol objects from the MultiCheckBox values. But when I pass complete object to the update form (in order to edit an existing entry), I get a notice and the checkboxes don't get built:
Notice: Object of class My\DataObject\Protocol could not be converted to int in /var/www/path/to/project/vendor/zendframework/zend-form/src/View/Helper/FormMultiCheckbox.php on line 202
My interpretation of this is, that the MultiCheckBox expects an array with values as primitive types (e.g. int). Instead it gets an array with Protocol objects and tries to use its values for in_array(...) -- and that cannot work.
Related
In my current project I'm using nested Zend\Form\Fieldsets and Zend\Form\Collections, that provide an extremely comfortable way to map complex object structures to the form, in order to get a complete object (ready to be saved) from the form input.
To the problem: I have a Fieldset FooFieldset containing an Element foo_element with a Label "foo element" (code see below) and need to use this twice: 1. as a single Fieldset; 2. in a Collection. At the first place in the form I want its elements to be displayed; at the the second place I want to disable the labels (or maybe change them). (I also want to format it another way in the second case, but the most important thing now is the label.)
How to decorate Zend\Form\Elements of a Zend\Form\Fieldset in a Zend\Form\Element\Collection depending on the context?
Code
class FooFieldset extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add([
'type' => 'text',
'name' => foo_element',
'options' => ['label' => _('foo element')]
]);
}
public function getInputFilterSpecification() { ... }
}
class BarFieldset extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add([
'name' => 'foo',
'type' => 'My\Form\Fieldset\Foo',
'options' => []
]);
}
public function getInputFilterSpecification() { ... }
}
class BuzFieldset extends Fieldset implements InputFilterProviderInterface
{
$this->add(
[
'name' => 'foos',
'type' => 'Zend\Form\Element\Collection',
'options' => [
'label' => _('multiple foos'),
'count' => 5,
'should_create_template' => true,
'template_placeholder' => '__placeholder__',
'allow_add' => true,
'target_element' => [
'type' => 'Order\Form\Fieldset\Foo',
],
'label_attributes' => [
'class' => 'col-md-12'
]
]
]);
public function getInputFilterSpecification() { ... }
}
echo $this->formRow($myForm->get('main_fieldset')->get('bar')->get('foo')->get('foo_element');
echo $this->formRow($myForm->get('main_fieldset')->get('buz')->get('foos');
Workaround 1
It would be possible to use another Fieldset, e.g. a sub-class of FooFieldst (sometnig like FooFieldsetForUsingInCollection extends FooFieldst) and adjust the Label (and other settings) there.
Workaround 2
It also would be possible to access the Collection's Elements in the view script and manipulate them there (as here demonstrated). But I don't really like this solution, since then the Fieldset is defined at multiple places. And it also need further effort, if the number of the Collection elements is variable.
It seems like you need to reuse the 'foos' collection and the 'bar' element together in their own fieldset while keeping how it is currently created.
I would
Move the collection element foo out of the BuzFieldset::init and into it's own factory (create the element and all it's options in the factory).
Register it with the form element manager as and new service, lets call it FooCollection. This element is now reusable and can be called from the form element manager as $fem->get('FooCollection').
Replace removed $fieldset->add('type' => 'Zend\Form\Element\Collection') with $fieldset->add('type' => 'FooCollection') in BuzFieldset.
Repeat for foo_element with a new service name of FooElement.
Then you need to create a new fieldset factory called FooCollectionAndFooElementFieldsetFactory this factory will return a new fieldset with both the FooCollection and FooElement attached.
In the factory of main_fieldset decide if you need to attach FooCollectionAndFooElementFieldsetFactory or the existing bar or baz fieldsets.
This is my first question, so I would also appreciate hints on how to ask properly.
So, In my Laravel app, I have a database table with users. For start, I wanted to have a model factory for it. So I took a standard code from laravel doc page:
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
And I changed it to:
$factory->define(App\User::class,
function(Faker\Generator $faker) {
return [
'name' => $faker->name(),
'email' => $faker->safeEmail(),
'password' => bcrypt(str_random(10)),
'phone_number' => $faker->phoneNumber(),
'remember_token' => str_random(10),
'account_type' => 0,
];
});
So far, everything works. But I wanted it to be more sophisticated, and I decided to use more specific kind of Faker class, to generate Italian data. I changed it to:
$factory->define(App\User::class,
function(Faker\Generator $faker,
Faker\Provider\it_IT\PhoneNumber $fakerITPN,
Faker\Provider\it_IT\Person $fakerITPER,
Faker\Provider\it_IT\Internet $fakerITInt) {
return [
'name' => $fakerITPER->name(),
'email' => $fakerITInt->safeEmail(),
'password' => bcrypt(str_random(10)),
'phone_number' => $fakerITPN->phoneNumber(),
'remember_token' => str_random(10),
'account_type' => 0,
];
});
In seeder class I wrote:
factory(App\User::class)->create();
And then, after I used Artisan, command:
artisan migrate:refresh --seed -vvv
I get following error (just the head, for clearance):
[ErrorException]
Argument 2 passed to Illuminate\Database\Eloquent\Factory::{closure}() must be an instance of Faker\Provider\it_IT\PhoneNumber, array given
Exception trace:
() at /home/vagrant/php/housing/database/factories/ModelFactory.php:19
Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at /home/vagrant/php/housing/database/factories/ModelFactory.php:19
Illuminate\Database\Eloquent\Factory::{closure}() at n/a:n/a
call_user_func() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:130
Illuminate\Database\Eloquent\FactoryBuilder->Illuminate\Database\Eloquent\{closure}() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2308
Illuminate\Database\Eloquent\Model::unguarded() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:133
Illuminate\Database\Eloquent\FactoryBuilder->makeInstance() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:105
Illuminate\Database\Eloquent\FactoryBuilder->make() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:83
Illuminate\Database\Eloquent\FactoryBuilder->create() at /home/vagrant/php/housing/database/seeds/UsersTableSeeder.php:24
UsersTableSeeder->run() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Seeder.php:42
Clearly, there is something wrong with dependency injection, but I don't know what. I know, that in this case I could just manually create instances of classes I need, but I want to know, how to do it properly. Can anyone help?
If you take a look at the documention of faker # https://github.com/fzaninotto/Faker#localization, you'll see that you can simply assign the proper localization as a parameter to create.
In your case, just use:
Faker\Factory::create('it_IT');
You don't need to add more parameters in the anonymous function when you define the factory.
Edit:
Just to add on the issue on dependency injection. If you trace the source code, it does not do any dependency injection underneath.
$factory->define(...)
Only sets an array of definitions
public function define($class, callable $attributes, $name = 'default')
{
$this->definitions[$class][$name] = $attributes;
}
Calling
Faker\Factory::create();
or
factory(App\User::class)->create();
$factory->of($class)
calls "of" method that instantiate FactoryBuilder
(see lines 169-172 of Illuminate\Database\Eloquent\Factory.php)
public function of($class, $name = 'default')
{
return new FactoryBuilder($class, $name, $this->definitions, $this->faker);
}
after that, it chains "create" method of FactoryBuilder that calls "make" method which also calls "makeInstance"
protected function makeInstance(array $attributes = [])
{
return Model::unguarded(function () use ($attributes) {
if (! isset($this->definitions[$this->class][$this->name])) {
throw new InvalidArgumentException("Unable to locate factory with name [{$this->name}].");
}
$definition = call_user_func($this->definitions[$this->class][$this->name], $this->faker, $attributes);
return new $this->class(array_merge($definition, $attributes));
});
}
Notice "call_user_func" inside "makeInstance", that is the one responsible for calling the anonymous function created as the 2nd argument to define (inside ModelFactory.php). It specifically pass only 2 arguments to the callable function, these are:
...$this->faker, $attributes);
Only 1 faker is passed on the first argument and an array of attributes on the 2nd argument (this is the one you saw on your ErrorException earlier)
That means you can only define your factory in this way:
$factory->define(App\User::class,
function (Faker\Generator $faker, $attributes=array()) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
If you really need other classes, you can initialize it outside of "define" and use it in the function like this:
$sampleInstance = app(App\Sample::class);
$factory->define(App\User::class,
function (Faker\Generator $faker, $attributes=array()) use($sampleInstance){
//...do something here
//...or process the $attributes received
//...or call a method like
$sampleData = $sampleInstance->doSomething();
return [
'someField' => $sampleData,
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
You can put this setting in register() of AppServiceProvider:
$this->app->singleton(\Faker\Generator::class, function () {
return \Faker\Factory::create('it_IT');
});
What I am actually doing is, fetching a list of companies from the database and passing that to the form SELECT element.
So I created a Model file, which returns an array
//=== return an array of $ID => $name of companies to use in dropdown in reports form
public function getTotalResult($table, $type, $id) {
$this->table = $table;
$select = new Select();
$spec = new Where();
$spec->equalTo('status', 1);
if ($type == 'name') {
$spec->equalTo('id', $id);
}
$select->from($this->table);
$select->where($spec);
$resultSet = $this->selectWith($select);
//$resultSet->buffer();
return $resultSet;
}
public function resultList($table){
$results = $this->getTotalResult($table, '', '');
foreach ($results as $result) {
$this->id[] = $result->id;
$this->name[] = $result->name;
}
$result = array_combine($this->id, $this->name);
return $result;
}
Then I tested this in my Controller, which returned exactly what I wanted:
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use SpangelLogin\Model\Register; // <-- Add this import
use SpangelLogin\Model\companyList; // <-- Add this import
class RegisterController extends AbstractActionController
{
protected $registerTable;
protected $companyList;
public function getcompanyList()
{
if (!$this->companyList) {
$sm = $this->getServiceLocator();
$this->companyList = $sm->get('SpangelLogin\Model\companyList');
}
return $this->companyList;
}
public function indexAction()
{
//== get list of companies
$company_table = 'rs_company';
$sector_table = 'rs_sector';
$companiesList = $this->getcompanyList()->getName($company_table, 2);
}
}
So now I want this companiesList array passed in my form's Select Element. How can I achieve that. Here is my form in which I am using select.
use Zend\Form\Form;
use Zend\Form\Element;
class SectorReportForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('sectorreport');
$companiesArray = $this->companiesList();
$sectorsArray = $this->sectorsList();
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/form-data');
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'company',
'attributes' => array(
'id' => 'company',
'multiple' => true,
'options' => $companiesArray,
),
'options' => array(
'label' => 'Company',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Upload',
'id' => 'submitbutton',
'class' => 'button violet right'
),
));
}
}
From a Design-Perspective, the best approach would be to handle this via Dependency-Injection. That sneaky little buzzword that confuses people so much, but actually is nothing more but to forward data between objects :P
General Dependency-Injection for Forms can be seen looking at the following answer, as well as my Blog article
How to get data from different model for select?
Zend\Form\Element\Select and Database-Values
If you do not want to go this approach, you can handle this at the Controller level, too.
$form = new My\Form();
$select = $form->get('selectCountries');
$model = new My\Countries();
$listData = $model->getCountriesAsArray();
$select->setValueOptions($listData);
I still advise you to go the different approach ;) Keeps the controllers more clean, too, which is always a good thing. Separation of concern!
Here’s my widget in the Form.Class:
$this->widgetSchema['schools'] = new sfWidgetFormChoice(array(
'choices' => Doctrine_Core::getTable('school')->getUsersSchools($userId),
'renderer_class' => 'sfWidgetFormSelectDoubleList',
'renderer_options' => array(
'label_unassociated' => 'Unassociated',
'label_associated' => 'Associated'
)));
The above works just fine, but the values that are stored are unassociated to the choices list referenced above. I need to store the ids of the array retrieved as the values. Instead, the list that is retrieved is chronological and the ids are ignored.
Here's the schoolTable query:
public function getUsersSchools($id){
$q =Doctrine_Query::create()
->select('id')
->from('school')
->where('user_id = ?', $id)
->execute();
return $q;
}
If I understand your question correctly you would like to store associated school ids.
Use the sfWidgetFormDoctrineChoice widget instead and it will work out of the box, as it using primary keys as ids.
$query = Doctrine_Core::getTable('school')->queryForSelect($userId);
$this->setWidget('schools', new sfWidgetFormDoctrineChoice(array(
'model' => 'school',
'query' => $query,
'multiple' => true,
'renderer_class' => 'sfWidgetFormSelectDoubleList',
'renderer_options' => array(
'label_unassociated' => 'Unassociated',
'label_associated' => 'Associated'
),
)));
$this->setValidator('schools', new sfValidatorDoctrineChoice(array(
'model' => 'schoool',
'query' => $query,
'multiple' => true,
)));
// in SchoolTable class
public function queryForSelect($userId)
{
return $this->createQuery('s')
->andWhere('s.user_id = ?', $userId)
;
}
If you has a proper schema (I presume the schools should be a many-to-many association), then the current from should has a schools_list field (properly defined in the generated base from) and then you can modify that field to be rendered by sfWidgetFormSelectDoubleList:
$this->widgetSchema['schools_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
$this->widgetSchema['schools_list']->setOption('renderer_options', array(
'label_unassociated' => 'Unassociated',
'label_associated' => 'Associated'
));
i got one model that has some properties and a 1:1 relation to my second model in the same extension and i wanted to map that second model completely to tt_content.
so the user can insert a tt_content object into my first model.
No Problem in the BE. I can insert objects from the first model and in there i can insert a tt_content object. In the Database my first model got that "content" column where the uid of the tt_content object so i thought everything is correct...
But then to the Controller... i just get nothing... just a NULL value on the "content" property...
this is how i tested the "content" property:
$contentBoxes = $this->contentBoxRepository->findAll();
print(gettype($contentBoxes->current()->getContent()));
and it returns just "NULL"
aaaaaand here are some infos about that first model whitch contains the tt_content object:
First Model:
class Tx_PlusbSlidingcontent_Domain_Model_ContentBox extends Tx_Extbase_DomainObject_AbstractEntity {
/**
* Content
*
* #var Tx_PlusbSlidingcontent_Domain_Model_Content
*/
protected $content;
...........
/**
* Returns the content
*
* #return Tx_PlusbSlidingcontent_Domain_Model_Content $content
*/
public function getContent() {
return $this->content;
}
/**
* Sets the content
*
* #param Tx_PlusbSlidingcontent_Domain_Model_Content $content
* #return void
*/
public function setContent(Tx_PlusbSlidingcontent_Domain_Model_Content $content) {
$this->content = $content;
}
...............
}
Second Model:
class Tx_PlusbSlidingcontent_Domain_Model_Content extends Tx_Extbase_DomainObject_AbstractEntity {
}
The "content" section in the TCA of the first Model:
'content' => array(
'exclude' => 0,
'label' => 'LLL:EXT:plusb_slidingcontent/Resources/Private/Language/locallang_db.xml:tx_plusbslidingcontent_domain_model_contentbox.content',
'config' => array(
'type' => 'inline',
'foreign_table' => 'tt_content',
'minitems' => 0,
'maxitems' => 1,
'appearance' => array(
'collapseAll' => 0,
'levelLinksPosition' => 'top',
'showSynchronizationLink' => 1,
'showPossibleLocalizationRecords' => 1,
'showAllLocalizationLink' => 1
),
),
),
And in the TS Setup i added this in "persistence":
classes {
Tx_PlusbSlidingcontent_Domain_Model_Content {
mapping {
tableName = tt_content
columns {
}
}
}
}
i just don't know where the error is in that config... doesn't the repository/model/anything have to autofill the content property on the first model with an object of the second model? at least an empty one?
(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
okay mystery solved...
the extension_builder adds a file called "ext_typoscript_setup.txt". In that file there is the for tt_content deadly "recordType" definition... removed that and voila... everything worked
Yes its very easy to integrate tt_content field in your extension.
If you want to set second title field in your tt_content record then you have to set following script in your ext_table.php
$tempColumns = array(
'subtitle' => array(
'exclude' => 0,
'label' => 'title2',
'config' => array(
'type' => 'input'
)
)
);
Then you have to load tt_content TCA file
#This is for EXTBASE Extension
\TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA('tt_content');
OR
#This is for PI Base Extension
t3lib_div::loadTCA("tx_dam_cat");
Following field will user for PARTICULAR EXTENSION then use following script.
$TCA["tt_content"]["types"]["list"]["subtypes_excludelist"]["abc_pqr"]="layout,select_key,pages";
$TCA["tt_content"]["types"]["list"]["subtypes_addlist"]["abc_pqr"]="subtitle;;;;1-1-1";
For more information about this then visit
https://jainishsenjaliya.wordpress.com/2014/08/25/how-to-use-tt_content-fields-in-custom-plugin-of-typo3/