Magento insert and filter multi-select values in custom admin-grid module - forms

I have created a custom module with Module Creator (v 1.7).
Have one multi-select admin form field.
As the multiselect field on submitting gives array , the same value (i.e Array) is stored in database.
To avoid this on saving the submitted value , I just manipulated the code by storing it in e.g a,b,c form.
Through this the data is saved successfully.
Now in grid , I want to filter it , as we have for Status part [1=>Enabled 2=>Disabled].
please suggest How would I achieve this .

Take a look at magento filter_condition_callback option
$this->addColumn('categories', array(
....
'filter_condition_callback' => array($this, '_applyMyFilter'),
..
)
);
protected function _filterCategoriesCondition($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return;
}
$this->getCollection()->addFieldToFilter('categories', array('finset' => $value));
}
See
Filtering a text-type column with MySQL-computed values in Magento Admin Grid
Magento: How to search or filter by multiselect attribute in admin grid?

Related

gravity forms Form Entry - display label, not value for checkboxes, select

I've got checkboxes and selects where label is different to its value.
In the DB the value is saved ok, however, when viewing Form Entries (and pre-submit form data) for all the checkboxes and selects the values are displayed not labels. So I end up with not so informative IDs rather than names.
Is there a way to display labels instead of values in the Form Entries screen?
I came across your question while looking to do the same thing. I have a field called Account Type that stores the account type ID as the value. In the entries list I want to show the account type name, not the ID.
Here's the solution:
In your theme's functions.php file add the following filter:
add_filter( 'gform_entries_column_filter', 'modify_entry_view', 10, 5 );
You'll find the documentation for it here: https://docs.gravityforms.com/gform_entries_column_filter/
Then add the function code:
function modify_entry_view( $value, $form_id, $field_id, $entry, $query_string ){
//- Check the field ID to make sure you only change that one
if( $field_id == 14 ){
return 'modified value';
};
return $value;
}
In my case I'm using a custom field that I created called account_type that prepopulates a select menu with choices corresponding to each of the account types in our system. I used the following call to check the field type instead of checking based on field id since the id will change from form to form:
if( RGFormsModel::get_field( $form_id, $field_id )->type == 'account_type' ){
You could do the same thing but use the label property instead of type, like:
if( RGFormsModel::get_field( $form_id, $field_id )->label == 'Field Label' ){
In my case the value saved is a term id from a custom taxonomy I set up called account_type, so I simply use:
return get_term_by( 'id', $value, 'account_type' )->name;
to replace the account id with the account name.
FYI: The process is the same for the entry detail, use the filter documented here: https://docs.gravityforms.com/gform_entry_field_value/

F3 ORM add new record omitting some fields

I have a postgres table in which I have some "defaulted" fields like date_created which automatically receives a current_timestamp as default.
or the ID field which gets it's value from a sequence defined in the database.
What would be (if possible) the syntax to tell the ORM module to not include these two fields when generating an INSERT statement ?
You can use a function as 2nd parameter to remove the fields:
$this->copyfrom('POST',function($val) {
unset($val['ID']);
unset($val['date_created']);
return $val
});
or to only copy allowed fields from the POST array:
$this->copyfrom('POST',function($val) {
return array_intersect_key($val, array_flip(array('name','age')));
});
Assuming you are using an HTML form to add new records into the tables, follow the steps below;
In the form, omit these 'defaulted' fields, i.e. add only the fields that you want to submit
Create a model with a function similar to below
public function add() {
$this->copyFrom ( 'POST' );
$this->save ();
}
Create a route that links the form to this function

SugarCRM restriction on custom field

i'm totally new to SugarCRM module development, however i have very good knowledge of PHP, ajax and database programming.
Here is my task:I have been asked to create a restriction on a custom field from the clients module.
There is a custom field called identification number, what i need to do is avoid a new client to be saved into the database based on that field, in other words the client has to be unique. It has to display a pop up window saying "that client already exists"
Copy editviewdefs.php of Accounts to the custom folder and change the custom field definition for id_number to this
array(
'name' => 'id_number',
'displayParams' =>
array (
'field' =>
array (
'onChange' => 'check_is_duplicate(this);',
),
),
),
Create a javascript function check_is_duplicate
function check_is_duplicate(obj) {
// Call a script via Ajax. Pass values for id and id_number with the request.
if (o.responseText > 0) {
alert('duplicate');
document.getElementById('SAVE').disabled = true;
} else {
document.getElementById('SAVE').disabled = false;
}
}
You would have to create the script which would be called via ajax request.
In that script you would have to run a query like -
SELECT COUNT(*) AS count FROM accounts
WHERE deleted = 0 AND id != {$record} AND id_number = {$id_number}
Execute the query and return the count.
Afterwards on saving check for duplicates from server side using the beforeSave logic hook.
You can create a custom duplicate check by the following code:
$dictionary['Account']['duplicate_check']['FilterDuplicateCheck']['filter_template'] = array(
array(
'$and' => array(
array('identification_number' => array('$equals' => '$identification_number')),
),
),
);
Add this code in the file: /custom/Extension/modules/Accounts/Ext/Vardefs/duplicate_check.php.
Then, perform a quick repair/rebuild.
How it works:
When you enter the identification number and press save, SugarCRM will perform a duplicate check. If duplicates are found, SugarCRM will list the duplicates and you can choose to use the duplicate or ignore.

Does the symfony form fields initialize to null if not visible

I have few fields on the form like name, description, timestamp.
Now in the form I am only displaying name and description but not timestamp.
public function __construct()
{
$this->setTimestamp(new \DateTime());
}
Now in my database, it is coming as null.
Either doctrine is not executing constructor or those fields are set to null when displayed in form.
Even though I am not displaying them.
You need put the timestamp field in your FormType.
If you don't need to show it, just hide the field and set default value.
Something like that:
$builder->add('timestamp', 'hidden', array(
'data' => new \DateTime(),
));
Don't forget {{form_rest(form)}} at end of the twig template to send all hidden fields.

Symfony : entity field validator and ajax dynamic content

I have 2 entities (A and B) with a Many to One relationship between them.
I create my form with the A entity and i use an entity field (dropdown list) to display the rows in the B entity. I use a query builder to filter them. If don't change the values in the list (ie. with ajax), everything is working fine.
But if I change dynamicly the values in the dropdown, when I submit the form I have this error "This value is invalid"
It's because the submitted value isn't included in the "array" returned by the query builder.
It seems that this validation is automatic in symfony for entity field (I don't use any asserts on this field). I'd like to get rid of this. But how ?
To answer my question a bit more explicitly :
The PRE_BIND form event can be redefined with an event listener in the function BuildForm like this example :
$factory = $builder->getFormFactory();
$builder->addEventListener(FormEvents::PRE_BIND, function($event) use ($factory) {
$form = $event->getForm();
$case = $event->getData();
$id = $case['id'];
if ($case) {
$form->remove('id');
$form->add($factory->createNamed('hidden', 'id',$id, array()));
}
});
For Symfony 2.3 you need to add the auto_initialize = false and change the order of params:
$form->add($factory->createNamed('id', 'hidden', $id, array('auto_initialize' => false)));