I'm new to magento and my plan is to use input fields (for multiple columns and rows) in a grid and send their content via POST to the controller.
In my grid class (which extends Mage_Adminhtml_Block_Widget_Grid) I've added the input fields in the following way:
$this->addColumn('name[]',
array(
'header'=> Mage::helper('mymodule')->__('Name'),
'index' => 'name',
'type' => 'input',
'name' => 'name[]'
));
Now I'm looking for an easy method to send the entered data to the controller (and to retrieve that data via $postData = $this->getRequest()->getPost());
Thank you for your help.
Magento submits this grid using grid.js
You can override grid.js, capture all input values corresponding to checked rows and then submit the form.
Here you can put input field names and values.
this.formHiddens.update('');
new Insertion.Bottom(this.formHiddens, this.fieldTemplate.evaluate({name: fieldName, value: value}));
Related
I am quite new to Symfony 2.7. I am facing a problem with Symfony form.
Inside my form i have a form field as follows
Builder->add('passDate', 'hidden')
->add('issueDate','hidden')
Inside my controller function, i need to pass some fix value to database. So here is my form action
$entity->setIssueDate('1950/01/01');
$entity->setPassDate('1950/01/01');
But when i submit it, it show me the error as that these form fields are required, although i set as default date as 1950/01/01. Please help me. Thank you
You can either remove the NotBlank symfomy validator mapping from the two fields or use the empty_data option which will populate default values if the hidden fields weren't filled via JavaScript like this:
$builder->add('passDate', 'hidden', array(
'data' => null,
'empty_data' => '1950/01/01'
)
I'm using Zend_Form for handling a form and I have a problem with localization.
I use following field declaration:
$this->addElement('text', 'area', array(
'label' => 'Area:',
'required' => true,
'filters' => array('StringTrim', 'NormalizedToLocalized')
));
The problem is, I use pl_PL locale and in this locale the decimal point separator is "," not ".". But database (MySQL) stores float with "." separator.
I added NormalizedToLocalized to convert e.g. 40.12 into 40,12 in my html form.
But when passing 40,12 in POST request I want Zend_Form to automatically convert back value 40,12 into 40.12 so that it can be passed to DB (I collect values from form using $form->getValues() method). By now it renders 40,12 successfully but in $form->getValues() I get localized, not not normalized value.
So my question is, whether it is possible to create different filter for rendering and getting value of field in Zend Framework.
Thanks in advance for your help!
The filters are not applied until the form is submitted so 'NormalizedToLocalized' will not have any effect until the form is posted. So if you want decimal points and not commas in your data for storage remove the filter (or use 'LocalizedToNormalized') from the form and apply the filter using Zend_Filter for any data you need to display.
If this field always expects a float type you can use a validator to enforce the value and then use the filter to enforce formating for the database.
$this->addElement('text', 'area', array(
'label' => 'Area:',
'required' => true,
'validators' => array('Float'),
'filters' => array('StringTrim', 'LocalizedToNormalized')
));
I'm not familiar with Zend Framework but looking on filter name, shouldn't it be 'LocalizedToNormalized' ?
I have a pretty simple form with some fields from a doctrine model.
$this->widgetSchema['fields'] = new sfWidgetFormDoctrineChoice(array(
'model' => 'FieldModel',
'expanded' => true,
'multiple' => true,));
$this->validatorSchema['fields'] = new sfValidatorDoctrineChoice(array(
'model' => 'FieldModel',
'multiple' => true,));
The fields are rendered in the form as checkboxes and I'm able to check and save correctly. This 'fields'-field is converted to a json-structure and saved to the database as text. So far so good.
NOTE: The field 'fields' is stored as TEXT in the database, but the user should be able to select values from a list of checkboxes.
The problem arise when I want to have some of the checkboxes checked by default.
I tried to do:
$this->setDefault('fields', array('key1','key2','key3'));
Where 'keyX' correspond to the actual value of the primary key (string) for Field in the database.
If I do a
$this->getDefault('fields');
I get back exactly what I put in previously.
However, symfony is not outputing any of the checkboxes as checked. I have even tried to remove both the 'expanded' and 'multiple' options to the choice-widget so I get a simple SELECT-box and the provide only one value as default selected.
Setting default values for other widgets (text-inputs, choice, etc) work.
Btw; The Field-model is i18n. Don't know if that matters here, since both storing / retrieving works as expected.
Also; the form is rendered as part of another form by means of include_partial(). Can that sabotage anything? In the 'parent' form class:
$this->embedRelation('TheRelationThatBugsMe');
And then in the _form.php for the 'parent':
include_partial('the_relation_that_bugs_me/form', array('form' => $form['TheRelationThatBugsMe']));
Does anyone have an idea where I might have gone wrong, or at least can give me some pointers as to where I should start digging?
[UPDATE]
If I create a new field in the form 'fields2' (that does not exist as a field in the database) and use the exact same code to create widget, validator and set defaults, then the defaults are rendered correctly. How come it doesn't work setting defaults for a field mapped to a column in the database?
If you're calling setDefault before updateDefaultFromObject gets called in sfDoctrineForm, then the object's values will override form defaults if the object exists. updateDefaultsFromObject contains the relevant logic. You'll have to call setDefault later, or override the method.
I'd like to use forms generated by propel
( propel:generate-module --with-show --non-verbose-templates frontend user users )
but I'd like do modify them a little bit. e.q. I'd like to remove fields that are foreign keys. Is it possible, or do I have to create my own forms?
EDIT
in file: project_name/lib/form/form_file.class.php there is an empty class which derives from some base class. If put there something like this:
$this->setWidgets(array(
'name' => new sfWidgetFormInput()
));
All the default fields disappear and there is only this 'name' field in the form, which is not what I'm looking for.
If you are using admin generator as you indicated, you can edit your forms via a generator.yml file. With this file you can do any number of things include set which widgets you want to appear, order of entry, actions, etc.
The generator.yml file is located in /apps/app_name/modules/module_name/config
You can read more about it in the symfony docs.
-- Edit --
If you are not using the generator.yml file, you can edit the form class directly, read this article relating to symfony forms for more info.
Example widget manipulation:
//-----
//Remove Unwanted
//-----
unset(
$this['created_at'],
$this['updated_at'],
$this['ingredient_list'] //Will be embedded due to extra meta data
);
//-----
// Add a select menu using model to populate values
$this->widgetSchema['state_list'] = new sfWidgetFormPropelChoice(array('model' => 'State', 'multiple' => true, 'order_by' => array('name', 'asc')));
// Add matching validator
$this->validatorSchema['state_list'] = new sfValidatorPropelChoice(array('model' => 'State', 'column' => 'id', 'multiple' => true));
// I can also force widget presentation order
$this->getWidgetSchema()->moveField('country_list', sfWidgetFormSchema::AFTER, 'state_list');
// You can also add a callback function when the form is submitted
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this, 'dupeCheck')))
);
If you're not using admin generator, you can just edit the templates generated in the folder /app/modulename/templates/.
For example, modify the indexSuccess.php file contained in that folder to change the structure/info of the table created which by default will display all the info that exists in your DB table.
I can add an element to a form like this:
$form->addElement($element);
However, that will put the element at the end of the form, I would like to prepend an element (put it at the beginning of the form).
Why? The form has dynamically generated fields (number of text fields and their labels are generated based on parameters from request) so the form class looks like this:
class Form1 extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$submit = new Zend_Form_Element_Submit('submit1', array(
'label' => 'Submit',
'class' => 'input-submit'
));
$this->addElements(array(
$submit
));
}
}
There is only the submit button because I don't know how many text fields and with what labels there will be yet.
From the ZF manual on Zend_Form Metadata and Attributes
Zend_Form_Element handles a variety of attributes and element metadata. Basic attributes include:
order: the index at which an element should appear in the form. Uses the setOrder() and getOrder() accessors.
So you would set the button to a very high order number, e.g. 1000 and then add the dynamic elements starting with an order number of 1 (or any number lower than the button's order number).
However, when there is nothing but the button in the form at all, then why not just create the entire form on the fly and append the submit button once you are done attaching the elements from the request.