0

I have created a custom form in magento2 admin via below code and it's working fine, but my form is too long, so I want to set two or more fields in one row And can divide fields in multiple tabs, Just like in Product edit form in admin. Here is my code-

 protected function _prepareForm()
{
 $model = $this->_coreRegistry->registry('booking_address');
 $form = $this->_formFactory->create(
 ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
 );
 $form->setHtmlIdPrefix('address_');
 $fieldset = $form->addFieldset(
 'base_fieldset',
 ['legend' => __('General Information'), 'class' => 'fieldset-wide']
 );
 if ($model->getId()) {
 $fieldset->addField('address_id', 'hidden', ['name' => 'address_id']);
 }
 $fieldset->addField(
 'plz',
 'text',
 ['name' => 'plz', 'label' => __('PLZ'), 'title' => __('PLZ'), 'required' => true]
 );
 $fieldset->addField(
 'street',
 'text',
 ['name' => 'street', 'label' => __('Street'), 'title' => __('Street'), 'required' => true]
 );
 $fieldset->addField(
 'latitude',
 'text',
 ['name' => 'latitude', 'label' => __('Latitude'), 'title' => __('Latitude'), 'required' => true]
 );
 $fieldset->addField(
 'longitude',
 'text',
 ['name' => 'longitude', 'label' => __('Longitude'), 'title' => __('Longitude'), 'required' => true]
 );
 $fieldset->addField(
 'code',
 'text',
 ['name' => 'code', 'label' => __('Code'), 'title' => __('Code'), 'required' => false]
 );
 //=START=========Want to show these fields in new tab and two in each row==============
 $fieldset->addField(
 'sunday',
 'multiselect',
 ['name' => 'sunday', 'label' => __('Sunday'), 'values' => $this->getLocationTimes()]
 );
 $model->setData('sunday', $sunday);
 $fieldset->addField(
 'monday',
 'multiselect',
 ['name' => 'monday', 'label' => __('Monday'), 'values' => $this->getLocationTimes()]
 );
 $model->setData('monday', $monday);
 $fieldset->addField(
 'tuesday',
 'multiselect',
 ['name' => 'tuesday', 'label' => __('Tuesday'), 'values' => $this->getLocationTimes()]
 );
 $model->setData('tuesday', $tuesday);
 $fieldset->addField(
 'wednesday',
 'multiselect',
 ['name' => 'wednesday', 'label' => __('Wednesday'), 'values' => $this->getLocationTimes()]
 );
 $model->setData('wednesday', $wednesday);
 $fieldset->addField(
 'thrusday',
 'multiselect',
 ['name' => 'thrusday', 'label' => __('Thrusday'), 'values' => $this->getLocationTimes()]
 );
 $model->setData('thrusday', $thrusday);
 $fieldset->addField(
 'friday',
 'multiselect',
 ['name' => 'friday', 'label' => __('Friday'), 'values' => $this->getLocationTimes()]
 );
 $model->setData('friday', $friday);
 $fieldset->addField(
 'saturday',
 'multiselect',
 ['name' => 'saturday', 'label' => __('Saturday'), 'values' => $this->getLocationTimes()]
 );
 $model->setData('saturday', $saturday);
 //==END========Want to show these fields in new tab and two in each row==============
 $form->setValues($model->getData());
 $form->setUseContainer(true);
 $this->setForm($form);
 return parent::_prepareForm();
}

Please let me know how I can do this.

Thanks

asked Jul 18, 2018 at 10:13
1
  • did you get any solution ? Commented Oct 12, 2019 at 8:52

1 Answer 1

0

Use this Serialize

private $serializer;
public function __construct(
 \Magento\Backend\App\Action\Context $context,
 \Magento\Framework\Serialize\SerializerInterface $serializer
)
{
 parent::__construct($context);
 $this->serializer = $serializer;
}
public function execute()
{
 $options = array();
 $post = $this->getRequest()->getPostValue();
 foreach($post as $key => $option){
 $options[$key] = $option;
 }
 $finalOptions = $this->serializer->serialize($options);
 //Save $finalOptions in your db
 // $x->setData('rowname', $finalOptions)
 // $x->save();
}
answered Jul 18, 2018 at 10:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.