Magento, populating checkboxes fields on an admin edit form - magento

I have a form page where I am using
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
to create a list of checkboxes.
The problem is I can't figure out how to get them to populate when editing. Can anyone tell me how to do this?
I am using the checkboxes type so they display as a list instead of in separate rows in the form. If there is a way to create them as separate fields but all in one row I would love to know how.

$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));
Then checkboxes with values "1" and "5" will be checked. For more details you can check lib/Varien/Data/Form/Element/Checkboxes.php

I also attached code or you can follow below link for more help
http://pastebin.com/hKMmryE9
Magento, populating checkboxes fields on an admin edit form
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));

Little improved and verified:
$fieldset->addField('payment_methods', 'checkboxes', array('label' => 'Payment Methods', 'name' => 'payment_methods[]',
'values' => array(
array('value'=>'1', 'label'=>'Cash'),
array('value'=>'2', 'label'=>'Paypal'),
array('value'=>'3', 'label'=>'Authorize.Net'),
array('value'=>'4', 'label'=>'Square'),
),
'required' => true,
'checked' => array('1','4'),
'disabled' => array('1'), ////if you want
));

Create $array like below
Array
(
[0] => Array
(
[value] => 1
[label] => Value 1
)
[1] => Array
(
[value] => 2
[label] => Value 2
)
[2] => Array
(
[value] => 3
[label] => Value 3
)
[3] => Array
(
[value] => 4
[label] => Value 4
)
[4] => Array
(
[value] => 5
[label] => Value 5
)
)
$fieldset->addField('checkboxes', 'checkboxes', array(
'label' => 'Select Value',
'name' => 'checkboxes[]',
'values' => $array,
'onclick' => "",
'onchange' => "",
'disabled' => false,
'after_element_html' => '',
'tabindex' => 1
));

Related

Cakephp Data Validation: Checking if at least one field is populated and multiple rules not validating

Folks,
I'm trying to ensure at least one of two fields (last_name or email) is being populated. Each field also has multiple rules. I'm using CakePHP version 2.4.2.
The problem I have at the moment, after multiple permutations of updating and/or moving around the use 'last', 'allowEmpty', 'required', etc, is that the fields just aren't validating at all, or aren't executing all the rules when a field is populated.
Any advice on how to modify the code below to achieve the following behaviour is much appreciated:
1. One of the two fields must be populated;
2. The other rules attached to each field must validate as well (i.e. if a last name is passed, then it must be between 2 and 45 chars and alphanumeric only)
Here's the model code:
public $validate = array(
'last_name'=> array(
'needOne' => array (
'required' => FALSE,
'allowEmpty' => TRUE,
'last' => TRUE,
'rule' => array('checkOne','last_name'),
'message' => 'You must enter at least a contact name or email address.'
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabets and numbers only'
),
'between' => array(
'rule' => array('between', 2, 45),
'message' => 'Between 2 to 45 characters'
)
),
'email' => array(
'needOne' => array (
'required' => FALSE,
'allowEmpty' => TRUE,
'last' => TRUE,
'rule' => array('checkOne','email'),
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'last' => TRUE,
'rule' => array('email', FALSE),
'message' => 'A valid Email address is required'
)
)
);
// Ensure at least the last name or email field value is provided
function checkOne($field) {
if(!empty($this->data[$this->User]['last_name']) ||
!empty($this->data[$this->User]['email'])){
return true;
} else {
return false;
}
}
Thanks in advance!
Thanks very much vicocamacho. I kept at it and, in addition to your advice, found the solution also lay in adding a 'required' => false in the view.
Here's the working solution for any one else with this problem:
The model:
public $validate = array(
'last_name'=> array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabets and numbers only',
'allowEmpty' => TRUE
),
'between' => array(
'rule' => array('between', 2, 45),
'message' => 'Between 2 to 45 characters',
'allowEmpty' => TRUE
)
),
'email' => array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'rule' => 'email',
'message' => 'A valid Email address is required',
'allowEmpty' => TRUE
)
)
);
// Ensure at least the last name or email field value is provided
public function checkOne($data) {
if(!empty($this->data[$this->alias]['last_name'])
|| !empty($this->data[$this->alias]['email'])) {
return TRUE;
} else {
return FALSE;
}
}
The view/fields (I'm using Bootstrap):
echo $this->Form->input('last_name', array(
'required' => false,
'fieldset' => false,
'label' => false,
'before' => '<label class="control-label">Last Name <span class="one-required">*</span></label>',
'class' => 'form-control',
'placeholder' => 'Last Name',
'div' => 'form-group col-sm-12',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'class' => 'alert alert-danger'
)
)
)
);
echo $this->Form->input('email', array(
'required' => false,
'fieldset' => false,
'label' => false,
'before' => '<label class="control-label">Email <span class="one-required">*</span></label>',
'after' => '',
'class' => 'form-control',
'div' => 'form-group col-sm-12 col-xs-12',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'class' => 'alert alert-danger'
)
)
)
);
Thanks.
Write a custom rule that will check if one of both fields is not empty and use that rule on both fields. And set allowEmpty to true for the other rules.
You should allowEmpty in all the rules that do not check if the fields are present, i updated your code so you can see what i mean, also you need to change the $this->User index in your checkOne to $this->alias otherwise it will always return false:
public $validate = array(
'last_name'=> array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabets and numbers only',
'allowEmpty' => true
),
'between' => array(
'rule' => array('between', 2, 45),
'message' => 'Between 2 to 45 characters',
'allowEmpty' => true
)
),
'email' => array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'allowEmpty' => true,
'rule' => 'email',
'message' => 'A valid Email address is required'
)
)
);
// Ensure at least the last name or email field value is provided
function checkOne($field) {
if(
!empty($this->data[$this->alias]['last_name']) ||
!empty($this->data[$this->alias]['email'])
)
{
return true;
} else {
return false;
}
}

Cakephp Form Validation empty indeces

Okay, so, in short, I'm trying to validate a form, as I've done a million times before with no trouble. However, I'm finding that on logging the validation errors, all the invalidated fields have an index in the validationErrors array but the messages are empty.
Yes, I've definitely set the validation messages in the model so I'm unsure why it's empty.
Here are my model validations:
public $validate = array(
'effective_policy_date' => array(
'date' => array(
'rule' => array('date'),
'message' => 'Invalid date format',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Policy date required',
),
),
'business_address' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Business address required',
),
),
'city' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'City required',
),
),
'zip_code' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Zip code required',
),
),
'state_id' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'State required',
),
),
'contact_person' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Contact person required',
),
),
'phone' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Phone number required',
),
),
'email' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Email address required',
),
'email' => array(
'rule' => array('email'),
'message' => 'Invalid email format',
),
),
);
Here's the output of the validationErrors array after submitting the form with empty fields:
[AccountsRequest] => Array
(
[effective_policy_date] => Array
(
)
[policy_number_one] => Array
(
)
[policy_number_two] => Array
(
)
[policy_number_three] => Array
(
)
[policy_number_four] => Array
(
)
[business_address] => Array
(
)
[city] => Array
(
)
[zip_code] => Array
(
)
[state_id] => Array
(
)
)
For completeness sake, here's my Form->create array I use in the view and the controller action responsible for handling the form submission:
Form create() method
<?php
echo $this->Form->create(
'Request',
array(
'novalidate' => 'novalidate',
'action' => 'new_request',
'inputDefaults' => array(
'label' => false,
'div' => false,
'error' => array(
'attributes' => array(
'wrap' => 'label', 'class' => 'error'
)
)
)
)
);
?>
Controller action
public function new_request()
{
$this->page_id = 'requester_newform';
if($this->request->is('post'))
{
if($this->Request->saveAll($this->request->data, array('deep' => true, 'validate' => 'only')))
{
$this->Session->setFlash('Request saved successfully', 'flash/success');
}
else
{
$this->Session->setFlash('Could not save request. Please correct validation errors', 'flash/error');
}
}
}
You'll see some indeces in the validation array that aren't in the validationErrors, that's simply because I haven't quite finished converting the raw HTML to CakePHP forms.
Problem: The validationErrors array shouldn't be empty. It should contain the messages for the notEmpty rules and as a result there are empty error validation elements on the Form's frontend. Any ideas about what I'm doing wrong?
Aarg, how annoying. I've figured it out and it's a lesson well learnt.
For anyone having a similar issue in the future, make sure that your input fields conform to the relationships of the current form's Model.
For instance, in this example, my form's model is 'Request'. Request hasMany 'AccountsRequest'. So my form inputs were something like:
AccountsRequest.effective_policy_date
where it should have been
AccountsRequest.0.effective_policy_date
With this change, my model validation messages are now showing without issue.
I'd still love to know, however, why CakePHP even picked up those fields as invalid and further, if it was intelligent enough to pick up those fields as invalid why it didn't give me validation messages.
Oh well.....

Create Magento coupons progmatically with expiry dates of 30 days from now

I need to Create Coupons in Magento on Action of account Create.
I have created observer which is executing properly on account Create but i am Struggling with Coupons create. I have fixed coupon names on of fixed amount on applicable on cart subtotal.
I found many ans but none of them has anything about expiry of coupon
No replies yet :(
Though I found the solution.
$customer = $observer->getCustomer()->getData();
$email = $customer['email'];
$email100 = $email.'-100-1';
$datei=date('d/m/Y');
$datee = date('d/m/Y', strtotime('1 day'));
$firstname=$customer['firstname'];
$couponcode1=$email.'100-1';
$data = array(
'product_ids' => null,
'name' => sprintf('coupon-100-1', Mage::getSingleton('customer/session')->getCustomerId()),
'description' => null,
'is_active' => 1,
'website_ids' => array(1),
'customer_group_ids' => array(1),
'coupon_type' => 2,
'coupon_code' => $email100,
'uses_per_coupon' => 1,
'uses_per_customer' => 1,
'from_date' => $datei,
'to_date' => $datee,
'sort_order' => null,
'is_rss' => 1,
'test' => 'test',
'rule' => array(
'conditions' => array(
'1' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
),
'1--1' => array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
),
'1--1--1' =>array(
'type' => 'salesrule/rule_condition_address',
'attribute' => 'base_subtotal',
'operator' => '>=',
'value' => '400'
),
'1--1--2' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 0
),
'1--1--2--1' =>array(
'type' => 'salesrule/rule_condition_product_found',
'value' => 1,
'aggregator' => 'all'
),
'1--1--2--1--1' => array(
'type' => 'salesrule/rule_condition_product',
'attribute' => 'special_price',
'operator' => '>=',
'value' => 0
)
),
'actions' => array(
'1' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => null
),
)
),
'simple_action' => 'cart_fixed',
'discount_amount' => 100,
'discount_qty' => 0,
'discount_step' => null,
'apply_to_shipping' => 0,
'simple_free_shipping' => 0,
'stop_rules_processing' => 0,
'store_labels' => array()
);
$model = Mage::getModel('salesrule/rule');
$validateResult = $model->validateData(new Varien_Object($data));
if ($validateResult !== true) {
foreach($validateResult as $errorMessage) {
// print_r($errorMessage);
$session->addError($errorMessage);
}
}
if (isset($data['rule']['conditions'])) {
$data['conditions'] = $data['rule']['conditions'];
}
if (isset($data['rule']['actions'])) {
$data['actions'] = $data['rule']['actions'];
}
unset($data['rule']);
//print_r($data);
$model->loadPost($data);
$model->save();
Here the name of coupon code is email id followed by -100-1.
$datei and $datee are the initial date and the expiry date. I have customized the coupon code so that it is not being used if the cart contains special price item.
Fixed amount discount is applied on the complete cart subtotal.
In Case of query do respond.

How to select multiple check box in edit grid section of magento

http://magento.localhost.com/index.php/arithmetic/adminhtml_arithmetic/edit/id/5/key/c03c12d4c338a2e4cdbb93c3d9e511a93401d19b21a13ea77cffda20cac94577/
This is what my link looks like. I am getting all values by the ID, in the edit grid page
there is a section for multiple check boxes. How can I select all the check boxes according to the result array
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
'class' => 'required-one',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
'class' => 'required-one',
)
),
));
Thanks
Hi at the time of storing array field value we are storing as string after converting array to string,
So at the time of setValues() Magento looking for that same input field value as array to check the check boxes
Trick is that convert that stored string value into array and assign to that column field that will work
Package_Arithmetic_Block_Adminhtml_Arithmetic_Edit_Tab_Form
protected function _prepareForm()
{
$id = $this->htmlEscape(Mage::registry('arithmetic_data')->getIn_user_id());
$model = Mage::getModel("arithmeti/newuser")->load($id);
/* here is the stuff witch converting the stored string to array and set in st_user_interest */
$interest = $model->getSt_user_interest();
$model->setSt_user_interest(explode(',',$interest));
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('arithmetic_form', array('legend'=>Mage::helper('arithmetic')->__('User information')));
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
)
),
));
if ( Mage::getSingleton('adminhtml/session')->getArithmeticData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getArithmeticData());
Mage::getSingleton('adminhtml/session')->setArithmeticData(null);
} elseif ( $model->getData() ) {
//Mage::registry('arithmetic_data')->getData(); /* removing this line and adding $model->getData() inslde the $form->setValues() */
$form->setValues($model->getData());
}
return parent::_prepareForm();
}

Why doesn't the view use the database table I defined in hook_views_data()?

I have a problem with my module views integration. I need to provide information about user who added video and timestamp. Video field is a CCK Embedded Media Field and it stores in content_field_3d_party_video table.
The schema is defined from the following code.
function MODULE_schema() {
$schema = array();
$schema['video_data'] = array(
'description' => t('Users and timestamps for video field'),
'fields' => array(
'value' => array(
'description' => t('Emfield value'),
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
),
'uid' => array(
'description' => t('User id'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'timestamp' => array(
'description' => t('Timestamp'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('value'),
'indexes' => array(
'timestamp' => array('timestamp'),
'uid' => array('uid'),
),
);
return $schema;
}
The hook_views_data() implementation is the following.
function MODULE_views_data() {
$data = array();
$data['video_data']['table']['group'] = t('Video Data');
$data['video_data']['table']['join'] = array(
'node' => array(
'left_table' => 'content_field_3d_party_video',
'left_field' => 'field_3d_party_video_value',
'field' => 'value',
),
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
$data['video_data']['value'] = array(
'title' => t('Video value'),
'relationship' => array(
'base' => 'content_field_3d_party_video',
'base field' => 'field_3d_party_video_value',
'field' => 'value',
'handler' => 'views_handler_relationship',
'label' => t('Video value'),
),
);
$data['video_data']['uid'] = array(
'title' => t('User id'),
'relationship' => array(
'base' => 'users',
'base field' => 'uid',
'field' => 'uid',
'handler' => 'views_handler_relationship',
'label' => t('User id'),
),
);
$data['video_data']['timestamp'] = array(
'title' => t('Timestamp field'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
return $data;
}
The table isn't included in the SQL query generated for the view.
SELECT node.nid AS nid,
node.title AS node_title,
node.language AS node_language,
node_data_field_3d_party_video.field_3d_party_video_embed AS node_data_field_3d_party_video_field_3d_party_video_embed,
node_data_field_3d_party_video.field_3d_party_video_value AS node_data_field_3d_party_video_field_3d_party_video_value,
...
node.type AS node_type,
node.vid AS node_vid
FROM node node
LEFT JOIN content_field_3d_party_video content_field_3d_party_video_video_data ON value = content_field_3d_party_video_video_data.field_3d_party_video_value
LEFT JOIN users users_video_data ON uid = users_video_data.uid
LEFT JOIN content_field_3d_party_video node_data_field_3d_party_video ON node.vid = node_data_field_3d_party_video.vid
WHERE ...
May you help me?
This is the solution the OP previously posted in the question.
To correctly join the table used for a CCK field, we must specify node_data_field_3d_party_video as left_table, instead of content_field_3d_party_video.
$data['video_data']['table']['join'] = array(
'node' => array(
'left_table' => 'node_data_field_3d_party_video',
'left_field' => 'field_3d_party_video_value',
'field' => 'value',
),
'users' => array(
'left table' => 'users',
'left_field' => 'uid',
'field' => 'uid',
),
);

Resources