CakePHP model validation for checkboxes - validation

I have a Job model where I want to submit a form to my other action (not to save the data directly into the db just to pass it to my other action) and I should check whether the user has selected at least 1 checkbox.
Here is my view's snippet:
echo '<tr class="v-top">
<td colspan=2>'.$this->Form->input('system_codes_filter', array(
'type' => 'select',
'multiple' => 'checkbox',
'label' => '',
'name' => 'system.codes.filter',
'class' => 'floatLeft',
'options' => array('System codes' => $this->Submit->getSystemCodes($systemcodes)),
'selected' => array('*'))).
'</td></tr>';
Here is my model's snippet:
public $validate = array(
'system_codes_filter' => array(
'rule' => array('multiple', array(
'min' => 1,
'message' => 'Please select at least on system code!'
)
)
)
);
As far as I know the validation is called when I want to use $this->Job->save(), but I don't use this in my action.
Unfortunately this does not work. When I submit my form without selecting any checkboxes, it let's me do that and if I look at the $this->validationErrors array, it is empty. What am I doing wrong? Thanks in advance.

Related

Magento issue with adding validation into multi-select checkbox

I have a form in magento admin panel. In the form i have checkboxes which i can select multiple options or one. The issue is i am unable to put validations for that. Because without selecting any option i can save records. My code is as in below:
$fieldset-> addField('time_ranges', 'checkboxes', array(
'label' => Mage::helper('CheckoutTime')->__('Time Ranges'),
'required' => true,
'class' => 'required-entry',
'name' => 'time_ranges[]',
'values' => array(
array(
'label' => Mage::helper('CheckoutTime')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('CheckoutTime')->__('Investment'),
)
),
));
Can anyone please tell me how to add validations into this form.
Thank You
Try by changing
'name' => 'time_ranges[]'
to
'name' => 'time_ranges'
This way is correct. There were some issues in other places in my coding. That's why it didn't work early. Or else this is the correct way to do that.

Fieldset in system configuration - Magento Admin module

I am creating an admin module. I have set of fields and i want to create a fieldset each of 3 fields in system configuration, i have created fields but wanted to add fieldset in it.
Any help would be appreciated. Thank you
You haven't given much information i.e Layout of your module, whether you are adding fields in code or .phtml but this is how I am adding fields to a field set:
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('form_settings', array('legend'=>Mage::helper('mymodule')->__('Module Settings')));
$newFieldset = $fieldset->addFieldset('form_settings_test', array('legend'=>Mage::helper('khaosconnect')->__('Order Settings')));
$newFieldset->addField('mysetting1', 'text', array(
'label' => Mage::helper('mymodule')->__('Setting 1'),
'class' => 'required-entry',
'required' => true,
'name' => 'mysetting1',
'value' => "val1",
'style' => 'width:500px'
));
$newFieldset->addField('mysetting2', 'text', array(
'label' => Mage::helper('mymodule')->__('Setting 2'),
'class' => 'required-entry',
'required' => true,
'name' => 'mysetting2',
'value' => "val2",
'style' => 'width:500px'
));
}
EDIT: Updated to show nested fieldsets.

how can i set validation for multi select list in cakephp

I have a form with multi select box:
echo $this->Form->input('emp_id', array( 'options' => array( $arr),
'empty' => '(choose one)',
'div'=>'formfield',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Team Members',
'type' => 'select', 'multiple' => true,
'style' => 'width:210px; height:125px;'
));
I selected multiple values from this list box and click the SAVE button.
But it displays the validation message.
How can i solve this?
class TravancoDSRGroup extends AppModel {
var $name = 'TravancoDSRGroup';
var $useTable = 'dsr_group'; // This model uses a database table 'exmp'
var $validate = array(
'emp_id' => array(
'rule' => 'notEmpty',
'message' => 'The employee field is required'
)
);
}
This is the model code....
If it is possible...?
You don't have to explicitly specify
'type' => 'select'
if you set 'emps' from the controller, it will allow cake's automagic to work. just add
$this->set(compact('emps'));
Post output of debug($this->request->data) for better understanding of problem.
Have you defined hasMany or HABTM relationships to the emp ? To have multiple values saved you will have to define hasMany or HABTM relations, if you wish to save it as CSV then you will have to do the processing yourself in 'beforeSave'.

Drupal Form APi Checkbox states

So I have create my checkbox in my form
$form['existing_customer'] = array(
'#type' => 'checkbox',
'#title' => t('Are you an existing customer'),
'#ajax' => array(
'callback' => 'checkbox_selected',
'wrapper' => 'subject',
),);
This calls my function and changes the values in my checkbox
The problem is I cannot get it to switch back if it is unchecked
function checkbox_selected(&$form, &$form_state) {
if ($form_state['values']['existing_customer'] == 1) {
$my_options = array( 'select' => t('Select'), 'mr' => t('Mr'), 'mrs' => t('Mrs'), 'miss' => t('Miss'), 'ms' =>t('Ms'), 'sir' =>t('Sir'), 'dr' => t('Dr'), 'prof' => t('Prof') );
}
elseif ($form_state['values']['existing_customer'] == 0){
$my_options = array( 'seconfZ' => t('jimmy'), 'mr' => t('Mr'), );
}
$form['subject'] = array(
'#type' => 'select',
'#title' => t('Subject'),
'#options' => $my_options//$form['subject_options']['#value']
);
return $form['subject'];
}
I thought I could do a switch on the checkbox value or state but no joy?
Oftentimes, when you use the Form API #ajax system, the wrapper that you specify is actually replaced with another element AFTER drupal_html_id() has been called again on the element wrapper. So I would check the markup of the "subject" element in Firebug/Web Inspector after your AJAX stuff happens--I'm betting that the wrapper div is now something like "subject--1".
To fix this, you need to manually set a wrapper div on the item you are replacing--one that won't change when the form is rebuilt. For example, in your form builder:
$form['existing_customer'] = array(
'#type' => 'checkbox',
'#title' => t('Are you an existing customer'),
'#ajax' => array(
'callback' => 'checkbox_selected',
'wrapper' => 'subject-wrapper',
),
);
$form['subject'] = array(
'#prefix' => '<div id="subject-wrapper">',
...
`#suffix' => '</div>',
);
Hope that helps!

Cakephp model alias works, but view doesn't show

I read your answer about cakephp model alias and I did on my model what you wrote, I debuged my view and it list what I want, but it doesn't show the list on the respective field, do you have any idea what is wrong?
My model:
'ResponsavelManutencao' => array(
'className' => 'Operador',
'foreignKey' => 'responsavel_manutencao_id',
'conditions' => '',
'fields' => '',
'order' => '')
My controller:
function add(
$responsavelManutencao = $this->Dentista->ResponsavelManutencao->find('list');
$this->set(compact('responsavelManutencao'));)
My view:
echo $this->Form->input('responsavel_relacionamento_id', array(
'class' => 'field text small', 'empty' => '',
'after' => 'Funcionario responsavel por manter o contato comercial.'));
debug($responsavelRelacionamento);
I guess there is something wrong with the CakePHP singular/plural rules for your language. I think you should name your view variable "responsavelRelacionamentos" for it to work.
However, the safest way to accomplish your goal without inflection magic would be:
echo $this->Form->input('responsavel_relacionamento_id', array(
'options' => $responsavelRelacionamento,
'class' => 'field text small', 'empty' => '',
'after' => 'Funcionario responsavel por manter o contato comercial.'
));

Resources