bind object to more than one fieldset in a form - object

Having one entity but two fieldsets, I found out that just one fieldset is populated when using the use_as_base_fieldset option.
Is there a solution for that problem?
First fieldset
class ProfileFieldset extends AbstractFieldset implements InputFilterProviderInterface
{
public function __construct($em)
{
....
$this->setHydrator(new Hydrator(false))
->setObject(new User());
...
}
...
}
Second Fieldset
<?php
use \Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as Hydrator;
use User\Entity\User;
class CredentialsFieldset extends AbstractFieldset
implements InputFilterProviderInterface
{
public function __construct($em)
{
.....
$this->setHydrator(new Hydrator(false))
->setObject(new User());
....
}
...
}
Form
use Zend\Stdlib\Hydrator\ClassMethods as Hydrator;
use User\Entity\User;
class UserForm extends AbstractForm
{
public function __construct()
{
....
$this->setHydrator(new Hydrator(false))
->setObject(new User());
....
$this->add(array(
'type' => 'User\Form\Fieldset\ProfileFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
$this->add(array(
'type' => 'User\Form\Fieldset\CredentialsFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
....
}
The entity itself contains properties for both fieldsets...
When binding the user entity for editing, just the last added fieldset is populated. Of course, there can be only one base fieldset ...
Anyone having an idea, how to solve that problem?

I think this could be done by creating a new fieldset that consists of the two fieldsets.
class UserForm extends AbstractForm
{
public function __construct()
{
$this->add(array(
'type' => 'User\Form\Fieldset\UserFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
....
}
Then UserFieldset would become
<?php
namespace User\Form\Fieldset;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class UserFieldset extends Fieldset {
public function __construct()
{
parent::__construct('user');
....
$this->add(array(
'type' => 'User\Form\Fieldset\ProfileFieldset',
));
$this->add(array(
'type' => 'User\Form\Fieldset\CredentialsFieldset',
));
.....
}
public function init()
{
$this->setHydrator(new ClassMethodsHydrator(false))
->setObject(new User());
....
}
....
}

Related

Styling laravel excel using from view

This is the controller
public function dxport()
{
//set excel style
$styleArray = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
//download excel with style
return Excel::download(new FieldequipsExport, 'users.xlsx')->setStyle($styleArray);
}
This is the export
<?php
namespace App\Exports;
use App\Models\Fieldequip;
use App\Models\Vendor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Facades\Excel;
class testExport implements FromView
{
//style the excel
public function __construct()
{
}
public function view(): View
{
$matchThese = ['type' => '1'];
$vendor = Vendor::pluck('vendor_name');
$data = Fieldequip::where($matchThese)->get();
$data->setFontSize(15);
return view('livewire.expfield',['data' => $data],['vendor' => $vendor]);
}
}
It returns an error which is [Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::setStyle()]
I read the documentation but still ran into this problem. How do I style it correctly?

Codeigniter redirect()->to() doesnt work in __construct()

I have code in my controller
public function __construct()
{
return redirect()->to('/auth');
$this->validation =
\Config\Services::validation();
$this->title = 'Header Menu';
$this->controller = 'user';
}
public function index()
{
$data = [
'title_prefix' => 'Profil',
'title' => $this->title,
'controller' => $this->controller,
'button_add' => false,
'validation' => $this->validation
];
return view('user/index', $data);
}
it still show view('user/index'). How to get to return redirect()->to('/auth') in __construct() ?
sorry i'm not speaking english well
It is an expected behavior that redirect() doesn't work inside a constructor.
redirect() in CI4 doesn't just set headers but return a RedirectResponse object.
Problem is : while being in the constructor of your controller, you can't return an instance of something else. You're trying to construct a Controller not a RedirectResponse.
Good practice is to use Controller Filters
Or you could add the redirect() inside your index function if there's only at this endpoint that you would like to redirect the user.
Here's an example of the filter that would fit your need :
Watch out your CI version. The parameter $arguments is needed since 4.0.4. Otherwise you have to remove it
<?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class AuthFilter implements FilterInterface {
public function before(RequestInterface $request, $arguments = null) {
return redirect()->to('/auth');
}
//--------------------------------------------------------------------
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {
}
}
And in your app/Config/Filters edit those 2 variables in order to activate your filter :
public $aliases = [
'auth' => \CodeIgniter\Filters\AuthFilter::class,
];
public $globals = [
'before' => ['auth' => 'the_routes_you_want_to_redirect']
];
You might want to check this thread aswell : https://forum.codeigniter.com/thread-74537.html
you can't use redirect() inside __construct() function or initController() directly.
But you can use $response parameter in initController() or $this->response attribute.
https://stackoverflow.com/a/65814413/1462903
in controller class
<?php namespace App\Controllers;
class Data extends BaseController
{
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
if($this->session->get('is_loggedin') !== true){
$response->redirect(base_url('login')); // or use $this->response->redirect(base_url('login'));
}
}
public function index()
{
return view('dashboard');
}
}
in BaseController class
/**
* Constructor.
*/
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
//--------------------------------------------------------------------
// Preload any models, libraries, etc, here.
//--------------------------------------------------------------------
$this->session = \Config\Services::session();
}
header("Location: ".site_url().'/dashboard');
die();

Kohana 3 - Save model to DB with relation belongs_to

I have a simple problem but I can not find an answer anywhere - neither in the documentation nor in this forum.
In short - I have 2 models in Kohana 3 - Task and TaskType.
I would like to create a Task model in such a way that I can then refer to the field $ task-> task_type_id-> name.
Unfortunately, when you insert a record into the database, I get the answer:
"General error: 1364 Field 'task_type_id' does not have a default value"
When I add the task_type_id field as a normal field (in $ _table_columns), then I can not refer to the TaskType object in the view via $ task-> task_type_id-> name.
I do not know how to configure the model to save the relation to the database and be able to refer to it in the view.
Please help.
My models code:
class Model_Task extends ORM
{
protected $_table_name = 'task';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'date' => array('data_type'=>'date','is_nullable'=>
'description'=>array('data_type'=>'varchar','is_nullable'=>true)
);
protected $_belongs_to = array(
'task_type_id' => array(
'model' => 'TaskType',
'foreign_key' => 'id'
)
);
}
class Model_TaskType extends ORM
{
protected $_table_name = 'task_type';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'name' => array('data_type'=>'string','is_nullable'=>false)
);
}
Probably it should be:
class Model_Task extends ORM
{
protected $_table_name = 'task';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'date' => array('data_type'=>'date','is_nullable'=>
'description'=>array('data_type'=>'varchar','is_nullable'=>true),
'task_type_id' => array('data_type'=>'int','is_nullable'=>false),
);
protected $_belongs_to = array(
'task_type' => array(
'model' => 'TaskType',
'foreign_key' => 'id'
)
);
}
class Model_TaskType extends ORM
{
protected $_table_name = 'task_type';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'name' => array('data_type'=>'string','is_nullable'=>false)
);
}
and adding by $task->add('task_type', $task_type);

Symfony3 forms not showing input fields when using CollectionType

I'm having problem with displaying form when using CollectionType. It doesn't show newOrderCustomerType inputs, just label "Customer Id". Whats wrong?
newOrderCustomerType
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class newOrderCustomerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->
add('firstname', TextType::class,array('label'=>'Firstname'))->
add('lastname', TextType::class,array('label'=>'Lastname'))->
add('email', TextType::class,array('label'=>'Email'))->
add('login', TextType::class,array('label'=>'Login'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Customer',
));
}
public function getName()
{
return 'app_bundlenew_order_customer_type';
}
}
newOrderType
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class newOrderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('customerId',CollectionType::class,array(
'entry_type'=>newOrderCustomerType::class,
'allow_add' => true,
'by_reference' => false,
'data_class' => 'AppBundle\Entity\Customer',
))
->add('shopOrderId')
->add('orderDate')
->add('postBuyFormMsg')
->add('invoice')
->add('payType')
->add('shipmentType')
->add('payStatus')
->add('save',SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
}
public function getName()
{
return 'app_bundlenew_order_type';
}
}
And in TWIG template
{{ form_start(orderForm) }}
{{ form_widget(orderForm) }}
{{ form_end(orderForm) }}
How to make it show all input fields?
If you have an active "allow_add" option, it is possible to render this input throught the 'prototype' option:
$builder
->add('customerId',CollectionType::class,array(
'entry_type'=>newOrderCustomerType::class,
'allow_add' => true,
'by_reference' => false,
'data_class' => 'AppBundle\Entity\Customer',
'prototype' => true,
))
and then in the form:
{{ form_row(orderForm.customerId.vars.prototype}) }}
It should work.
For more, see the Symfony documentation of prototype option
Try removing 'data_class', I don't think that's part of CollectionType:
->add('customerId',CollectionType::class,array(
'entry_type'=>newOrderCustomerType::class,
'allow_add' => true,
'by_reference' => false,
))
See if that works.

Form: Avoid setting null to non submitted field

I've got a simple model (simplified of source):
class Collection
{
public $page;
public $limit;
}
And a form type:
class CollectionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('page', 'integer');
$builder->add('limit', 'integer');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'FSC\Common\Rest\Form\Model\Collection',
));
}
}
My controller:
public function getUsersAction(Request $request)
{
$collection = new Collection();
$collection->page = 1;
$collection->limit = 10;
$form = $this->createForm(new CollectionType(), $collection)
$form->bind($request);
print_r($collection);exit;
}
When i POST /users/?form[page]=2&form[limit]=20, the response is what i expect:
Collection Object
(
[page:public] => 2
[limit:public] => 20
)
Now, when i POST /users/?form[page]=3, the response is:
Collection Object
(
[page:public] => 3
[limit:public] =>
)
limit becomes null, because it was not submitted.
I wanted to get
Collection Object
(
[page:public] => 3
[limit:public] => 10 // The default value, set before the bind
)
Question: How can i change the form behaviour, so that it ignores non submitted values ?
If is only a problem of parameters (GET parameters) you can define the default value into routing file
route_name:
pattern: /users/?form[page]={page}&form[limit]={limit}
defaults: { _controller: CompanyNameBundleName:ControllerName:ActionName,
limit:10 }
An alternative way could be to use a hook (i.e. PRE_BIND) and update manually that value into this event. In that way you haven't the "logic" spreaded into multi pieces of code.
Final code - suggested by Adrien - will be
<?php
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
class IgnoreNonSubmittedFieldSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_BIND => 'preBind');
}
public function preBind(FormEvent $event)
{
$submittedData = $event->getData();
$form = $event->getForm();
// We remove every child that has no data to bind, to avoid "overriding" the form default data
foreach ($form->all() as $name => $child) {
if (!isset($submittedData[$name])) {
$form->remove($name);
}
}
}
}
Here's a modification of the original answer. The most important benefit of this solution is that validators can now behave as if the form post would always be complete, which means there's no problems with error bubbling and such.
Note that object field names must be identical to form field names for this code to work.
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
class FillNonSubmittedFieldsWithDefaultsSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_BIND => 'preBind');
}
public function preBind(FormEvent $event)
{
$submittedData = $event->getData();
$form = $event->getForm();
// We complete partial submitted data by inserting default values from object
foreach ($form->all() as $name => $child) {
if (!isset($submittedData[$name])) {
$obj = $form->getData();
$getter = "get".ucfirst($name);
$submittedData[$name] = $obj->$getter();
}
}
$event->setData($submittedData);
}
}

Resources