X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   form getDefaultParams is not working (https://forum.x-cart.com/showthread.php?t=72594)

Roman Prisiazniuk 08-13-2015 05:40 AM

Handling forn
 
How can I add a form handler to my module that adds record in database? after adding record it must redirect to same page.

Roman Prisiazniuk 08-15-2015 08:27 AM

form getDefaultParams is not working
 
I have this model form:
PHP Code:

<?php
namespace XLite\Module\Romas\ProductActivation\View\Model;

class 
OrderItemActivation extends \XLite\View\Model\AModel{

    protected 
$schemaDefault = array(
        
'credit' => array(
            
self::SCHEMA_CLASS    => 'XLite\View\FormField\Input\Text',
            
self::SCHEMA_LABEL    => 'Credit',
            
self::SCHEMA_REQUIRED => true,
        ),
    );

    public function 
getModelId()
    {
        return 
false;
    }

    protected function 
getDefaultModelObject()
    {
        return new \
XLite\Module\Romas\ProductActivation\Model\OrderItemActivation;
    }

    protected function 
getFormClass()
    {
        return 
'\XLite\Module\Romas\ProductActivation\View\Form\Model\OrderItemActivation';
    }
}


PHP Code:

<?php

namespace XLite\Module\Romas\ProductActivation\View\Form\Model;

class 
OrderItemActivation extends \XLite\View\Form\AForm
{
    protected function 
getDefaultTarget()
    {
        return 
'add_activation';
    }

    protected function 
getDefaultAction()
    {
        return 
'update';
    }

    protected function 
getDefaultParams()
    {
        return array(
            
'date' => time(),
        );
    }
}


This code adds form to page with credit field and with hidden date field,
but when I submit form it does not insert date field to sql query, it just uses null, and I get this error
An exception occurred while executing 'INSERT INTO xc_order_item_activations (order_item_id, date, credit, activation_code, ip_address) VALUES (?, ?, ?, ?, ?)' with params [null, null, "2", null, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'date' cannot be null

Roman Prisiazniuk 08-15-2015 08:32 AM

Re: form getDefaultParams is not working
 
Also tell me please next:
I added multiple forms(as above) on my order page, every order item has its own form, how can I add hidden field to this form that must have valu of order_item_id?

qualiteam 08-18-2015 11:42 PM

Re: form getDefaultParams is not working
 
Quote:

Originally Posted by Roman Prisiazniuk
when I submit form it does not insert date field to sql query, it just uses null, and I get this error
An exception occurred while executing 'INSERT INTO xc_order_item_activations (order_item_id, date, credit, activation_code, ip_address) VALUES (?, ?, ?, ?, ?)' with params [null, null, "2", null, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'date' cannot be null


This is because your $schemaDefault describes the "credit" field only, so X-Cart uses "null" value for the rest of the entity fields.

You may try to set the missing properties in the View\Model\OrderItemActivation ::setModelProperties() method.


Quote:

Originally Posted by Roman Prisiazniuk
Also tell me please next:
I added multiple forms(as above) on my order page, every order item has its own form, how can I add hidden field to this form that must have valu of order_item_id?



You can add hidden fields to a form by declaring them in the getDefaultParams() method of your form class that extends \XLite\View\Form\AForm.

For example, check the \XLite\View\Form\Profile class.


All times are GMT -8. The time now is 12:11 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.