4

I have created a custom Form and try to store details in databse. but the product url field and image url is not saved in db.

app/code/Dyode/Pricebeat/Controller/User/Post.php

<?php
 namespace Dyode\Pricebeat\Controller\User;
 use Magento\Framework\App\Action\Action;
 use Magento\Framework\App\Action\Context;
 use Dyode\Pricebeat\Model\FormFactory;
 use Magento\Framework\View\Result\PageFactory;
 use Magento\Framework\Exception\LocalizedException;
 use Magento\Framework\Session\SessionManagerInterface;
 use Magento\Framework\HTTP\PhpEnvironment\Request;

 class Post extends Action
  {
protected $_modelFormFactory;
protected $resultPageFactory;
protected $_sessionManager;


public function __construct(
  Context $context,
  FormFactory $modelFormFactory,
  PageFactory $pageFactory,
  SessionManagerInterface $sessionManager)
{
    $this->resultPageFactory = $pageFactory;
    $this->_modelFormFactory = $modelFormFactory;
    $this->_sessionManager = $sessionManager;
    parent::__construct($context);
}

public function execute()
{
    $this->validatedParams();
    $this->getFormData();
    echo "hello from the controller";
    exit();
}

private function validatedParams()
{
    $request = $this->getRequest();
    if (trim($request->getParam('first_name')) === '') {
        throw new LocalizedException(__('First Name is missing'));
    }

    if (trim($request->getParam('last_name')) === '') {
        throw new LocalizedException(__('Last Name is missing'));
    }

    if (false === \strpos($request->getParam('email'), '@')) {
        throw new LocalizedException(__('Invalid email address'));
    }
    if (trim($request->getParam('phonenumber')) === '') {
        throw new LocalizedException(__('Phone Number is missing'));
    }
    if (trim($request->getParam('account_number')) === '') {
        throw new LocalizedException(__('Account Number is missing'));
    }
    if (trim($request->getParam('invoice_number')) === '') {
        throw new LocalizedException(__('Invoice Number is missing'));
    }
    if (trim($request->getParam('product_url')) === '') {
        throw new LocalizedException(__('product url Number is missing'));
    }
    if (trim($request->getParam('product_image_url')) !== '') {
        throw new LocalizedException(__('image is missing'));
    }

    //Add your more validations here
    return $request->getParams();
}
private function getFormData()
{

   $resultRedirect     = $this->resultRedirectFactory->create();
   $FormModel          = $this->_modelFormFactory->create();
   $data               = $this->getRequest()->getPost();
   $date               = date('Y-m-d h:i:sa');

   $FormModel->setData('first_name', $data['first_name']);
   $FormModel->setData('last_name', $data['last_name']);
   $FormModel->setData('email', $data['email']);
   $FormModel->setData('phonenumber', $data['phonenumber']);
   $FormModel->setData('account_number', $data['account_number']);
   $FormModel->setData('invoice_number', $data['invoice_number']);
   $FormModel->setData('product_url', $data['product_url']);
   $FormModel->setData('product_image_url', $data['product_url']);


   $FormModel->setData('created_date', $date);
   $FormModel->setData('status', $data['status']);

   $FormModel->save();

   $this->_redirect('blog/index');
   $this->messageManager->addSuccess(__('The data has been saved.'));
}
}

app/code/Dyode/Pricebeat/view/frontend/templates/pricebeat.phtml

<div class="wk-mp-design">
<fieldset class="fieldset info wk-mp-fieldset">
    <legend class="legend">
        <span><?php echo __('Pricebeat') ?></span>
    </legend>
    <form action="<?php echo $block->getUrl('pricebeat/user/post', ['_secure' => $this->getRequest()->isSecure()]) ?>" enctype="multipart/form-data" method="post" id="quote-request" data-form="quote-request" data-mage-init='{"validation":{}}' data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
        <div class="field required">
            <label class="label"><?php echo __('First Name') ?>:</label>
            <div class="control">
                    <input type="text" id="first_name" name="first_name" title="First Name" class="input-text" class="input-text" data-validate="{required:true}"/>
            </div>
        </div>
        <div class="field required">
            <label class="label"><?php echo __('Last Name') ?>:</label>
            <div class="control">
                    <input type="text" id="last_name" name="last_name" title="Last Name" class="input-text" class="input-text" data-validate="{required:true}"/>
            </div>
        </div>
        <div class="field required">
            <label class="label"><?php echo __('Email Address') ?>:</label>
            <div class="control">
                    <input type="email" id="email" name="email" title="Email Address" class="input-text" class="input-text" class="input-text" class="input-text" class="input-text" data-validate="{required:true, 'validate-email':true}"/>
            </div>
        </div>
        <div class="field required">
            <label class="label"><?php echo __('Phone Number') ?>:</label>
            <div class="control">
                    <input type="text" id="phonenumber" name="phonenumber" title="Phone Number" class="input-text" class="input-text" class="input-text" class="input-text" data-validate="{required:true, 'validate-number':true}"/>
            </div>
        </div>
        <div class="field required">
            <label class="label"><?php echo __('Account Number') ?>:</label>
            <div class="control">
                    <input type="text" id="account_number" name="account_number" title="Account Number" class="input-text" class="input-text" class="input-text" class="input-text" data-validate="{required:true, 'validate-number':true}"/>
            </div>
        </div>
        <div class="field required">
            <label class="label"><?php echo __('Invoice/Order Number') ?>:</label>
            <div class="control">
                    <input type="text" id="invoice_number" name="invoice_number" title="Invoice Number" class="input-text" class="input-text" class="input-text" class="input-text" data-validate="{required:true, 'validate-number':true}"/>
            </div>
        </div>
            <label class="label"><?php echo __('Product Url') ?>:</label>
            <div class="control">
            <input type="text" id="product_url" name="product_url" title="Product Url" class="input-text" class="input-text" data-validate="{required:true}"/>
            </div>
            <?php echo __("Product image") ?>
            <div class="control">
                    <input type="file" id="product_image_url" name="product_image_url"  title="product_image_url" class="input-text" class="input-text" data-validate="{required:true}"/>
            </div>
        <button class="button wk-mp-btn" title="<?php echo __('Submit') ?>" type="submit" id="submit-btn">
            <span><span><?php echo __('Submit') ?></span></span>
        </button>
    </form>
</fieldset>
</div>
<script type="text/x-magento-init">
{
    "#quote-request": {
        "validation": {}
    }
}
</script>

app/code/Dyode/Pricebeat/Setup/InstallSchema.php

<?php

 namespace Dyode\Pricebeat\Setup;

 class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
  {
  /**
   * install tables
   *
   * @param \Magento\Framework\Setup\SchemaSetupInterface $setup
   * @param \Magento\Framework\Setup\ModuleContextInterface $context
   * @return void
   * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
   */
public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
{
    $installer = $setup;
    $installer->startSetup();
    if (!$installer->tableExists('dyode_pricebeat_form')) {
        $table = $installer->getConnection()->newTable(
            $installer->getTable('dyode_pricebeat_form')
        )
        ->addColumn(
            'form_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            [
                'identity' => true,
                'nullable' => false,
                'primary'  => true,
                'unsigned' => true,
            ],
            'Form ID'
        )
        ->addColumn(
            'first_name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable => false'],
            'First Name'
        )
        ->addColumn(
            'last_name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Last Name'
        )
        ->addColumn(
            'email',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Email'
            )
        ->addColumn(
            'phonenumber',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            255,
            [],
            'Phone Number'
        )
        ->addColumn(
            'account_number',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            255,
            [],
            'Account Number'
        )
        ->addColumn(
            'invoice_number',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            255,
            [],
            'Invoice Number'
        )
        ->addColumn(
            'product_url',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            1,
            ['nullable => false'],
            'Product Url'
        )
        ->addColumn(
            'product_image_url',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            1,
            ['nullable => false'],
            'Product Image Url'
        )

        // ->addColumn(
        //     'imagethumbnail',
        //     \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        //     255,
        //     [],
        //     'Image Thumbnail'
        // )
        // ->addColumn(
        //     'show_in_frontend',
        //     \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        //     1,
        //     ['nullable => false'],
        //     'form Show in Frontend'
        // )
        ->addColumn(
            'status',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            1,
            ['nullable => false'],
            'form Status'
        )
        ->addColumn(
            'created_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            [],
            ' Created At'
        )
        ->addColumn(
            'updated_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            [],
            ' Updated At'
        )
        ->setComment('Form Table');
        $installer->getConnection()->createTable($table);

        $installer->getConnection()->addIndex(
            $installer->getTable('dyode_pricebeat_form'),
            $setup->getIdxName(
                $installer->getTable('dyode_pricebeat_form'),
                ['first_name','last_name','email','phonenumber','account_number','product_url','product_image_url'],
                \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
            ),
            ['first_name','last_name','email','phonenumber','account_number','product_url','product_image_url'],
            \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
        );
    }
    $installer->endSetup();
}
}
  • have got any error at system and exception log? – Amit Bera Sep 17 '18 at 14:46
  • no error at system – Nismath vi Sep 17 '18 at 14:51
  • Please check magento system.log and exception.log file> – Amit Bera Sep 17 '18 at 14:54
0

You have an error in

$FormModel->setData('product_image_url', $data['product_url']);

Change that to:

$FormModel->setData('product_image_url', $data['product_image_url']);

Plus, it's not so easy to upload a file and store the URL. You'll need to save the file and extract the filename, see http://php.net/manual/en/features.file-upload.post-method.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.