Cakephp model alias works, but view doesn't show - model-view-controller

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.'
));

Related

added fields are not working in edit or create mode in backpack cms

In Backpack CMS framework I have created a table using migration fields like : 'parent_id' and 'is_active'.
I have added these fields in the crud controller using the following command:
$this->crud->addFields(array(
['name' => 'title',
'label' => 'Title',
'type'=>'text'],
['name' => 'parent_id',
'label' => 'Parent ID',
'type'=> 'number'],
['name' => 'is_active',
'label' => 'Is Active',
'type'=>'number']
), 'update/create/both');
It should work in edit mode and create mode like my other tables. It shows the defined fields in the create and update form but unfortunately they doesn't work and always return the default value or previous value in the record.
I've tried adding the field in single format but there was no use.
The problem is here: 'update/create/both' you should pick only one of the three options.
What you want is to use only both. But since it is the default value, you don't really need to add it to the end of the addFields function.
This will work:
$this->crud->addFields([[
'name' => 'title',
'label' => 'Title',
'type' => 'text'
], [
'name' => 'parent_id',
'label' => 'Parent ID',
'type' => 'number'
], [
'name' => 'is_active',
'label' => 'Is Active',
'type' => 'number'
]
]);
The problem was with the fillable variable..... Only the 'title' field was in the fillable variable of the related model.

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.

CakePHP model validation for checkboxes

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.

cakephp validation message not showing

i am new to cakephp and am trying to build an app in it. I have some textfields, i wish to have validation on them. I following cakephp tutorials in cakephp.org and did the following but i cant see the validation messages near the textfields. Following is my code:
ctp:
<?php echo $this->Form->text('Rideoffer.PickFrom',
array('class' => 'address-text',
'value' => $dropFrom)); ?>
model:
public $validate = array(
'PickFrom' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
),
//'PickFrom' => 'notEmpty',
'DropAt' => 'notEmpty',
// 'born' => 'date'
);
where am i getting wrong? how do i solve it?
You have used comma in dropat
public $validate = array(
'PickFrom' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
),
//'PickFrom' => 'notEmpty',
'DropAt' => 'notEmpty'
// 'born' => 'date'
);
You can use this
Form->text('Rideoffer.PickFrom',
array('class' => 'address-text',
'value' => $dropFrom,
'error'=>'Cannot leave this field blank.')); ?>

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'.

Resources