Using CakePHP form validation with angular JS - validation

I am building a simple invoice module using angular js and cake php.
The items fields are repeated using ng - repeat in my view as seen below
<div ng:controller="ItemsCtrl" ng:app>
<div class="row-fluid items" >
<hr>
<ul class="invoice_items" ng:init="invoice={items:[{serial:'',details:'',qty:0,unit:'',rate:0,discount:0,amount:0}],pf:0}">
<li ng:repeat="item in invoice.items">
<div class="clear"></div>
<div id="items_row">
<div class="field span1">
<?php
echo $this->TwitterBootstrap->input("Number", array(
"input" => $this->Form->text("Item.{{\$index}}.serial" , array('class' => 'serial span1' ,'placeholder' => 'S.No' , 'ng-model' => 'item.serial' , 'value' => '{{ $index + 1 }}' , 'readonly' => 'readonly' ))
)); ?>
</div>
Closing the appropriate tags in the end
I have the following code in my model for validation -
public $validate = array(
'id' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'serial' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'details' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'quantity' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'naturalnumber' => array(
'rule' => array('naturalnumber'),
'message' => 'Please enter a valid quantity'
),
),
'rate' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'numeric' => array(
'rule' => array('numeric'),
),
),
'discount' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'amount' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'numeric' => array(
'rule' => array('numeric'),
),
),
);
The issue is that the fields which are outside of the ng-repeat directive get validated as required , but since the fields inside ng-repeat get initialized on each page load, cakephp validation isnt applied to them .
Do you guys see any work around to this ? May be my entire architecture approach is wrong?

I think you should change the way you are solving the problem.
Use CakePHP to create a REST API and AngularJS for the frontend. It will be much easier (and better if you want to migrate your stack in the future, i.e. Dart or NodeJS).
You might want to read: https://github.com/hantsy/angularjs-cakephp-sample
Hope it helps.

Related

How to Make a Status Field Bydefault Enable in Magento Form?

Following is my Form Field Code.
$fieldset->addField('status','select',
array(
'label' => Mage::helper('synclogin')->__('Eshot Status'),
'name' => 'status',
'values' => array(
array(
'value' => 0,
'label' => Mage::helper('synclogin')->__('Disabled'),
),
array(
'value' => 1,
'label' => Mage::helper('synclogin')->__('Enabled'),
),
),
)
);
How to Make a Status Field Bydefault Enable in Magento Form?
$fieldset->addField('status','select',
array(
'label' => Mage::helper('synclogin')->__('Eshot Status'),
'name' => 'status',
'value' => '1',
'values' => array(
array(
'value' => 0,
'label' => Mage::helper('synclogin')->__('Disabled'),
),
array(
'value' => 1,
'label' => Mage::helper('synclogin')->__('Enabled'),
),
),
)
);

How to have 2 actions to action column in Magento grid?

I've created an action column in my magento grid but I want 2 actions in my action column.
I try this code in my _prepareColumn:
$this->addColumn('action',
array(
'header' => Mage::helper('sterenn_quote')->__('Action'),
'width' => '100px',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('sterenn_quote')->__('Modidier'),
'url' => array(
'base' => '*/*/modifyquotation',
'param' => array('id' => $this->getRequest()->getParam('quotation_id'))
),
'field' => 'id'
),
array(
'caption' => Mage::helper('sterenn_quote')->__('Detail'),
'url' => array(
'base' => '*/*/detail',
'param' => array('id' => $this->getRequest()->getParam('quotation_id'))
),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores'
));
But I obtain this result :
How can I have my two action "Modifier" And "Detail" ?
Thanks for answers !

Yii1: How to force filter list in CGridView to parse html ?

I have a grid, in this grid I have filter. Grid code is:
$this->widget('booster.widgets.TbGridView', array(
'id' => 'sam',
'type' => 'striped bordered condensed',
'dataProvider' => $dataProvider,
'responsiveTable' => true,
'enableHistory' => true,
'filter' => $asset,
'columns' => array(
array(
'name' => 'id',
'header' => '#',
'filter' => false,
'type' => 'text',
),
array(
'name' => 'user',
'header' => 'Registered By',
'type' => 'text',
'value' => '$data["assignedBy"]',
),
array(
'name' => 'createdAt',
'header' => 'Created At',
'type' => 'datetime',
),
array(
'name' => 'serial',
'header' => 'Serial',
),
array(
'name' => 'brand',
'header' => 'Brand',
),
array(
'name' => 'model',
'header' => 'Model',
),
array(
'name' => 'assetType',
'type' => 'text',
'filter' => \wsi\it\model\AssetType::getRepository()->getTypeTree(),
'value' => '$data["assetTypeName"]',
'header' => 'Type',
),
array(
'name' => 'assigned',
'value' => '(isset($data["assignedTo"]))? $data["assignedTo"]:null',
'header' => 'Assigned To',
),
array(
'name' => 'location',
'filter' => \wsi\hr\Facade::getInstance()->getLocations(),
'value' => '$data["locationName"]',
'header' => 'Location',
),
array(
'name' => 'status',
'header' => 'Status',
'filter' => \wsi\it\model\Asset::$statusOptionList,
'value' => '\wsi\it\model\Asset::$statusOptionList[$data["status"]]',
),
array(
'class' => 'booster.widgets.TbButtonColumn',
'template' => '{view} {update} {delete}',
'header' => '',
'buttons' => array(
'update' => array(
'url' => '\Yii::app()->controller->createUrl("asset/create", array("id"=>$data["id"]))',
),
'view' => array(
'url' => '\Yii::app()->controller->createUrl("asset/view", array("id"=>$data["id"]))',
),
'delete' => array(
'url' => '\Yii::app()->controller->createUrl("asset/delete", array("id"=>$data["id"]))',
),
),
),
)
)
);
When Grid is rendered in the browser you can see that the filter list box contains html which is not parsed fully!
I had this problem with Yii-Booster before and I solved it with an option which I passed to that widget ('htmlOptions' => 'encode' => false) and It prevents to be treated as string, so browsers would parse it as space. That code which You can see below does not work for grid filter!
$form->dropDownListGroup($formModel, 'segmentList', array(
'wrapperHtmlOptions' => array(
'class' => 'col-md-6'
),
'widgetOptions' => array(
'data' => $segmentTreeArray,
'htmlOptions' => array(
>>> 'encode' => false, <<<
)
),
'hint' => "Press CTRL to add another item, otherwise others will be deselected",
));
BUT I am sure that I have to pass same "encode" => false to filter list too, I just can not find under what key I should pass it (htmlOptions did not work).

Display Action labes in single row instead of combobox in grid on admin side magento

I want to show Display Action labes in single row instead of combobox in grid on admin side magento .My code for combobox is
$this->addColumn('action',
array(
'header' => Mage::helper('mymodule')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('mymodule')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
),
array(
'caption' => Mage::helper('mymodule')->__('Delete'),
'url' => array('base'=> '*/*/delete'),
'confirm' => Mage::helper('mymodule')->__('Are you sure?'),
'field' => 'id'
),
array(
'caption' => Mage::helper('mymodule')->__('View'),
'url' => array('base'=> '*/*/view'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
See image for combobox style.
Checkout this code
$this->addColumn('actions', array(
'header' => Mage::helper('adminnotification')->__('Actions'),
'width' => '250px',
'sortable' => false,
'renderer' => 'adminhtml/notification_grid_renderer_actions',
));
under class Mage_Adminhtml_Block_Notification_Grid
and also see the class Mage_Adminhtml_Block_Notification_Grid_Renderer_Actions
If you have still any problem let me know.

How to change comments data with onchange function in addfield in magento

I created custom module and now from admin side on edit form i added extra field select type.
I want to change comments with onchange function for this specific field.See below my code.
$eventElem = $fieldset->addField('banner_type', 'select', array(
'label' => Mage::helper('multibanners')->__('Banner Style'),
'required' => false,
'onchange' => 'checkSelectedItem(this.value)',
'name' => 'banner_type',
'values' => array(
array(
'value' => 'Banner 1',
'label' => 'AnySlider',
),
array(
'value' => 'Banner 2',
'label' => 'Content Slider',
),
));
$eventElem->setAfterElementHtml("<script type=\"text/javascript\">function checkSelectedItem(selectElement){}</script>");
This is my code i alert the value and i got my value but it cannot show it in comments area .Did someone one know how to fix it ?
Thanks
This will update the comment (onchange) with the current selected option
$fieldset->addField('banner_type', 'select', array(
'label' => Mage::helper('multibanners')->__('Banner Style'),
'required' => false,
'onchange' => 'checkSelectedItem(this.value)',
'name' => 'banner_type',
'values' => array(
array(
'value' => 'Banner 1',
'label' => 'AnySlider',
),
array(
'value' => 'Banner 2',
'label' => 'Content Slider',
),
)
))->setAfterElementHtml("<small id='banner_type_comment'>Comments</small>
<script type=\"text/javascript\">
function checkSelectedItem(selectElement){
$('banner_type_comment').update($('banner_type')[$('banner_type').selectedIndex].text);
}
</script>");

Resources