Jump to content

How to add default values in renderform()


dwojod

Recommended Posts

Hi,

 

I decided create module:)

And i try add to default vaules in text field in controller module

 

array(
                    'type' => 'text',
                    'label' => $this->l('Vocher:'),
                    'name' => 'nr_voucher',
                    'disabled' => true,
                    'desc' => $this->l('Automatic generate code'),
                    'value'=> 'test',
                ),
 
this solution not working
 
generally i want to add in 'values' function results, which generate random string
 
 
Link to comment
Share on other sites

ok, I know what you mean but:

 

it will be grerat if I can implement that functions like in AdminCartRules -> generate code in js

 

but it will be impossiblle, I want to create some random string in PHP, and when actions is 'add', display in this field

 

i need some help 

 

regards

Link to comment
Share on other sites

ok, I find solution

 

I focus on file mymodule/views/templates/admin/mymodule/helpers/form/form.tpl and I add piece of code,

button with id="bt_" generate random string, I use jQuery function in this same file

{extends file="helpers/form/form.tpl"}

{block name="input"}
    {if $input.name == 'nr_voucher'}
        <div class="row">
            <div class="input-group col-lg-4">
                <input
                        id="{if isset($input.id)}{$input.id}{else}{$input.name}{/if}"
                        type="text"
                        name="{$input.name}"
                        value="{$fields_value[$input.name]|escape:'html':'UTF-8'}" />
				<span class="input-group-addon">
					<i class="icon-puzzle-piece"></i>
				</span>
				<span class="input-group-btn">
					<a id="bt_" class="btn btn-default"><i class="icon-random"></i>{l s='Generate'}</a>
				</span>
            </div>
        </div>
    {else}
        {$smarty.block.parent}
    {/if}
{/block}
{block name=script}
    {* Add your scripts here *}
	
	...
{/block}
Link to comment
Share on other sites

  • 6 years later...

Solution tested in PS version= 1.7.7 , in your module controller extended of ModuleAdminController :

public function renderForm()
{        
    //....fields definitons 
    $this->fields_form = array(...);
	
    // Set or create properties values of 'object' to establish default values 
    $this->object->nr_vouche = someLogicOrValue()  ; //  nr_vouche is defined in $this->fields_form
   
    $this->object->other_defined_field = = someLogicOrValue() ;
   
    return parent::renderForm();


}

 

Explanation:  see  the behavior in classes/controller/AdminController.php , method  renderForm(), when calls $this->getFieldsValue($this->object); 

 

Link to comment
Share on other sites

  • 2 years later...

I want to share one alternate solution to add a default value for the field in PrestaShop when creating a table of custom modules or alter the table SQL query when installing a module.

This way new field has default for the column in the database. Below is the example SQL statement.

ALTER TABLE `ps_sfkseoschema`
ADD `sfk_MerchantReturn_Country` varchar(100) DEFAULT 'FR';

ALTER TABLE `ps_sfkseoschema`
ADD `sfk_MerchantReturn_Days` varchar(100) DEFAULT '60';

If the field is not created inside the database table of the module then we can use the below code

// Set default values for your module's fields when it's installed.
 Configuration::updateValue('YOUR_MODULE_FIELD', 'default_value');

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...