File validation is not working in cake php - php

If I don't choose a file, time validation doesn't work and it doesn't show the message "invalid file".
this is action url:
http://localhost/carsdirectory/cars/create_ad
create_ad.ctp(view)
<?php echo $this->Form->create('Car', array('type' => 'file', 'action' => 'create_ad')); ?>
<?php echo $this->Form->file('CarImage.image_path')?>
<?php echo $this->Form->end(array('label' => 'Submit', 'name' => 'Submit', 'div' => array('class' => 'ls-submit')));?>
car_image.php(model)
<?php
class CarImage extends AppModel
{
var $name = 'CarImage';
var $belongsTo = 'Car';
var $validate = array(
'image_path' => array(
'rule' => array('extension', array('jpeg', 'jpg', 'png', 'gif')),
'allowEmpty' => false,
'required' => true,
'message' => 'invaid file'
),
'car_id' => array(
'rule' => "numeric",
'allowEmpty' => false,
'message' => 'Car details could not be saved'
)
);
}
?>
car_images(table)
Field -> 1) id 2) image_path 3)car_id

As I commented and suggested what seems right according to your scheme is that you have these associations:
Car model:
public $hasMany = array(
'CarImage' => array(
'className' => 'CarImage',
'foreignKey' => 'car_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
CarImage model:
public $belongsTo = array(
'Car' => array(
'className' => 'Car',
'foreignKey' => 'car_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Field for form, since you can have many files to uploaded (just make a new line with number increment):
echo $this->Form->file('CarImage.0.image_path');
To validate and upload files, is easier and more consistent making this in CarImage model (I don't sure if you needs car_id in $validate variable, if doesn't work, try remove car_id from $validate):
public $validate = array(
'image_path' => array(
'uploadFile' => array(
'rule' => array('uploadFile'),
'message' => 'Invalid file',
),
),
);
public function uploadFile($params) {
if ($params['image_path']['error'] == 0) {
$file = pathinfo($params['image_path']['name']);
if (in_array($file['extension'], array('jpeg', 'jpg', 'png', 'gif'))) {
$tmp_file = new File($params['image_path']['tmp_name']);
if ($tmp_file->copy('folder_inside_webroot' . DS . $file['basename'])) {
return true;
}
}
}
return false;
}
And your controller needs saveAll instead save:
if ($this->Car->saveAll($this->data)) {
// success
} else {
// failure
}
I hope it helps.

Related

PrestaShop: How to remove 'new' button from my backoffice controller

I want to remove the add button from the bo list view toolbar in prestashop is there any way ,(only for my page which I created as a seperate module
or atleast when I click the add button it must not do anything .
require_once(_PS_MODULE_DIR_.'addsocialmedia/addsocialmedia.php');
require_once(_PS_MODULE_DIR_.'addsocialmedia/classes/SocialMedia.php');
class AdminAddSocialMediaController extends ModuleAdminController
{
public $module;
public $html;
public $tabName = 'renderForm';
public function __construct()
{
$this->tab = 'socialmedia';
$this->module = new addsocialmedia();
$this->addRowAction('edit');
$this->explicitSelect = false;
$this->context = Context::getContext();
$this->id_lang = $this->context->language->id;
$this->lang = false;
$this->ajax = 1;
$this->path = _MODULE_DIR_.'addsocialmedia';
$this->default_form_language = $this->context->language->id;
$this->table = _DB_KITS_PREFIX_.'social_media';
$this->className = 'SocialMedia';
$this->identifier = 'id_social_media';
$this->allow_export = true;
$this->_select = '
id_social_media,
name_social_media,
social_media_url,
status
';
$this->name = 'SocialMedia';
$this->bootstrap = true;
$this->initList();
parent::__construct();
}
private function initList()
{
$this->fields_list =
array(
'id_social_media' => array(
'title' => $this->l('Social Media ID'),
'width' => 25,
'type' => 'text',
),
'name_social_media' => array(
'title' => $this->l('Social Media Name'),
'width' => 140,
'type' => 'text',
),
'social_media_url' => array(
'title' => $this->l('Social Media Url'),
'width' => 140,
'type' => 'text',
),
'status' => array(
'title' => $this->l('Enabled'),
'align' => 'text-center',
'status' => 'status',
'type' => 'bool',
'orderby' => false,
'callback' => 'changeStatus',
),
);
$helper = new HelperList();
$helper->shopLinkType = '';
$helper->simple_header = true;
// Actions to be displayed in the "Actions" column
$helper->actions = array('edit');
$helper->identifier = 'code';
$helper->show_toolbar = true;
$helper->title = 'HelperList';
$helper->table = $this->name.'check';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
return $helper;
}
public function initPageHeaderToolbar()
{
$this->page_header_toolbar_title = $this->l('Edit Social Media Image and url/link');
parent::initPageHeaderToolbar();
}
public function initToolbar()
{
parent::initToolbar();
$this->context->smarty->assign('toolbar_scroll', 1);
$this->context->smarty->assign('show_toolbar', 1);
$this->context->smarty->assign('toolbar_btn', $this->toolbar_btn);
}
public function postProcess()
{
parent::postProcess();
$id = (int)Tools::getValue('id_social_media');
$file = Tools::fileAttachment('social_media_image_name');
if (!empty($file['name']) && $id > 0)
{
if (ImageManager::validateUpload($file, Tools::convertBytes(ini_get('upload_max_filesize'))))
die('Image size exceeds limit in your Bigticket Back Office settings');
if (!is_dir('../modules/addsocialmedia/social_media_img'))
#mkdir('../modules/addsocialmedia/social_media_img', 0777, true);
if (!is_dir('../modules/addsocialmedia/social_media_img/'.$id))
#mkdir('../modules/addsocialmedia/social_media_img/'.$id, 0777, true);
$path = '../modules/addsocialmedia/social_media_img/'.$id.'/';
$absolute_path = $path.$file['name'];
move_uploaded_file($file['tmp_name'], $absolute_path);
$imgPath = 'social_media_img/'.$id.'/'.$file['name'];
//Save in DB if needed
Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.''._DB_KITS_PREFIX_.'social_media`
SET `social_media_image_name` = "'.pSQL($imgPath).'"
WHERE `id_social_media` = '.(int)$id);
}
if (Tools::isSubmit('changeStatusVal') && $this->id_object) {
if ($this->tabAccess['edit'] === '1') {
$this->action = 'change_status_val';
d("chnage");
} else {
$this->errors[] = Tools::displayError('You do not have permission to change this.');
}
}
}
//Call back for change status
public function changeStatus($value, $socialMedia)
{
return '<a class="list-action-enable '.($value ? 'action-enabled' : 'action-disabled').'" href="index.php?'.htmlspecialchars('tab=AdminAddSocialMedia&id_social_media='
.(int)$socialMedia['id_social_media'].'&changeStatusVal&token='.Tools::getAdminTokenLite('AdminAddSocialMedia')).'">
'.($value ? '<i class="icon-check"></i>' : '<i class="icon-remove"></i>').
'</a>';
}
/**
* Toggle the Social media to Enabled or Disabled flag- Here the update action occurs
*/
public function processChangeStatusVal()
{
d("hii");
$socialMedia = new SocialMedia($this->id_object);
if (!Validate::isLoadedObject($socialMedia)) {
$this->errors[] = Tools::displayError('An error occurred while updating the Status .');
}
d("going to change");
$socialMedia->status = $socialMedia->status ? 0 : 1;
if (!$socialMedia->update()) {
$this->errors[] = Tools::displayError('An error occurred while updating Social Media Status .');
}
Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
}
//When a winner is deleted , delete the image
public function processDelete()
{
$ob=parent::processDelete();
PrestaShopLogger::addLog(sprintf($this->l('%s DELETED social media IMAGE', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int)$id, true, (int)$this->context->employee->id);
if ($ob->deleted==1){
$id = (int)Tools::getValue('id_social_media');
$unlink_path = '../modules/addsocialmedia/social_media_img/'.$id.'/';
unlink($unlink_path); // this must delete the img folder from the winners module dir
//log the delete
PrestaShopLogger::addLog(sprintf($this->l('%s DELETED social media IMAGE', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int)$id, true, (int)$this->context->employee->id);
}
}
//If the user updates the image delete the old image from server
public function processUpdate(){
//d("updating..");
$id = (int)Tools::getValue('id_social_media');
$file = Tools::fileAttachment('social_media_image_name');
if (!empty($file['name']) && $id > 0)
{
$get_previous_image_sql = 'SELECT social_media_image_name FROM `'._DB_PREFIX_._DB_KITS_PREFIX_.'social_media`
where id_social_media='.$id.'';
$get_previous_image_path = Db::getInstance()->getValue($get_previous_image_sql) ;
//d($get_previous_image_path);
$unlink_path = '../modules/addsocialmedia/'.$get_previous_image_path.'';
unlink($unlink_path); // this must delete the img folder from the winners module dir
//log the delete when deleting
PrestaShopLogger::addLog(sprintf($this->l('%s DELETED social media IMAGE while updating new image', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int)$id, true, (int)$this->context->employee->id);
}
}
// This form is populated when add or edit is clicked
public function renderForm()
{
$firstArray =
array(array(
'type' => 'text',
'label' => $this->l('Name'),
'name' => 'name_social_media',
'required' => true,
'col' => '4',
'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()##"°{}_$%:'
),
array(
'type' => 'text',
'label' => $this->l('Social Media URL'),
'name' => 'social_media_url',
'required' => true,
'col' => '4',
'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()##"°{}_$%:'
),
array(
'type' => 'file',
'label' => $this->l('Social Media Image'),
'name' => 'social_media_image_name',
'display_image' => true,
'required' => false,
'desc' => $this->l('Add .JPG or .PNG File Format.Prefered Width:381 pixels and Height 285 pixels.Prefered File size -50KB .'),
'hint' => array(
$this->l('Add .JPG or .PNG File Format.Prefered Width:381 pixels and Height 285 pixels.Prefered File size -50KB .')
)
),
array(
'type' => 'switch',
'label' => $this->l('Enable Or Disable this Social Media'),
'name' => 'status',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'status_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'status_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
'hint' => $this->l('Enable or disable this Social Media From front END.')
),
);
if (Tools::getIsset('addkits_social_media') ){
$secondArray = array(
array(
'type' => 'hidden',
'label' => $this->l('Add Date'),
'name' => 'date_add',
'col' => '4',
'values'=>date("Y-m-d H:i:s"),
'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()##"°{}_$%:'
),
array(
'type' => 'hidden',
'label' => $this->l('Winner Image Name'),
'name' => 'social_media_image_name',
'col' => '4',
'values'=>"default_value"
)
);
$mainArray = array_merge($firstArray,$secondArray);
}
if (Tools::getIsset('updatekits_social_media') ){
$thirdArray = array(
array(
'type' => 'hidden',
'label' => $this->l('Update Date'),
'name' => 'date_upd',
'col' => '4',
'values'=>date("Y-m-d H:i:s"),
'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()##"°{}_$%:'
),
array(
'type' => 'hidden',
'label' => $this->l('Winner Image Name'),
'name' => 'social_media_image_name',
'col' => '4',
'values'=>"default_value"
));
$mainArray = array_merge($firstArray,$thirdArray);
}
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => $this->l('Configure your Social Media Image and URL'),
'icon' => 'icon-user'
),
'input' => $mainArray
);
//Assign value to hidden
$this->fields_value['date_add'] = $date = date("Y-m-d H:i:s");
$this->fields_value['social_media_image_name'] ="default_image.jpg";
$this->fields_form['submit'] = array(
'title' => $this->l('Save'),
);
$date = date("Y-m-d H:i:s");
$this->addJqueryUI('ui.datepicker');
return parent::renderForm();
}
You have to override the initToolbar method:
public function initToolbar() {
parent::initToolbar();
unset( $this->toolbar_btn['new'] );
}
Cheers ;)
Yes it is at the helper of your module. Could you put the code of the page so I am indicating or remove. Otherwise solution less "clean" applied CSS to hide this button.
Regards,

How to Add Custom Attribute on product Grid in magento 1.9.2 community edition

I am having an issue while showing my custom attribute as column in grid.
First I created an attribute programmatically using update script, here is the code :
$installer = $this;
$installer->startSetup();
// Set data:
$attributeName = 'Owners'; // Name of the attribute
$attributeCode = 'owners'; // Code of the attribute
$attributeGroup = 'General'; // Group to add the attribute to
$attributeSetIds = array(4); // Array with attribute set ID's to add this attribute to. (ID:4 is the Default Attribute Set)
// Configuration:
$data = array(
'type' => 'varchar', // Attribute type
'input' => 'text', // Input type
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
'required' => false, // Is this attribute required?
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'used_in_product_listing' => true,
'default_value_yesno' => '0',
// Filled from above:
'label' => $attributeName
);
// Create attribute:
// We create a new installer class here so we can also use this snippet in a non-EAV setup script.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->addAttribute('catalog_product', $attributeCode, $data);
$installer->addAttributeToSet(
'catalog_product', 'Default', $attributeGroup, $attributeCode
); //Default = attribute set, General = attribute group
$installer->endSetup();
This code is working fine for me and it is creating an attribute.
Than I override the grid.php successfully and put my code their but it is not working column is visible in grid but data is not coming.
code of grid.php
public function __construct()
{
parent::__construct();
$this->setId('productGrid');
$this->setDefaultSort('entity_id');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
$this->setVarNameFilter('product_filter');
}
protected function _getStore()
{
$storeId = (int) $this->getRequest()->getParam('store', 0);
return Mage::app()->getStore($storeId);
}
protected function _prepareCollection()
{
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id')
->addAttributeToSelect('owners');
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
$collection->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left');
}
if ($store->getId()) {
//$collection->setStoreId($store->getId());
$adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
$collection->addStoreFilter($store);
$collection->joinAttribute(
'name',
'catalog_product/name',
'entity_id',
null,
'inner',
$adminStore
);
$collection->joinAttribute(
'custom_name',
'catalog_product/name',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'status',
'catalog_product/status',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'visibility',
'catalog_product/visibility',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'price',
'catalog_product/price',
'entity_id',
null,
'left',
$store->getId()
);
}
else {
$collection->addAttributeToSelect('price');
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
}
$this->setCollection($collection);
parent::_prepareCollection();
$this->getCollection()->addWebsiteNamesToResult();
return $this;
}
protected function _addColumnFilterToCollection($column)
{
if ($this->getCollection()) {
if ($column->getId() == 'websites') {
$this->getCollection()->joinField('websites',
'catalog/product_website',
'website_id',
'product_id=entity_id',
null,
'left');
}
}
return parent::_addColumnFilterToCollection($column);
}
protected function _prepareColumns()
{
$this->addColumn('entity_id',
array(
'header'=> Mage::helper('catalog')->__('ID'),
'width' => '50px',
'type' => 'number',
'index' => 'entity_id',
));
$this->addColumn('name',
array(
'header'=> Mage::helper('catalog')->__('Name'),
'index' => 'name',
));
$store = $this->_getStore();
if ($store->getId()) {
$this->addColumn('custom_name',
array(
'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
'index' => 'custom_name',
));
}
$this->addColumn('type',
array(
'header'=> Mage::helper('catalog')->__('Type'),
'width' => '60px',
'index' => 'type_id',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
));
$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
->load()
->toOptionHash();
$this->addColumn('set_name',
array(
'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
'width' => '100px',
'index' => 'attribute_set_id',
'type' => 'options',
'options' => $sets,
));
$this->addColumn('sku',
array(
'header'=> Mage::helper('catalog')->__('SKU'),
'width' => '80px',
'index' => 'sku',
));
$store = $this->_getStore();
$this->addColumn('price',
array(
'header'=> Mage::helper('catalog')->__('Price'),
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'price',
));
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
$this->addColumn('qty',
array(
'header'=> Mage::helper('catalog')->__('Qty'),
'width' => '100px',
'type' => 'number',
'index' => 'qty',
));
}
$this->addColumn('visibility',
array(
'header'=> Mage::helper('catalog')->__('Visibility'),
'width' => '70px',
'index' => 'visibility',
'type' => 'options',
'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
));
$this->addColumn('status',
array(
'header'=> Mage::helper('catalog')->__('Status'),
'width' => '70px',
'index' => 'status',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
));
$this->addColumn('owners',
array(
'header'=> Mage::helper('catalog')->__('Owner'),
'width'=>'200px',
'index' => 'owners',
));
return parent::_prepareColumns();
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('websites',
array(
'header'=> Mage::helper('catalog')->__('Websites'),
'width' => '100px',
'sortable' => false,
'index' => 'websites',
'type' => 'options',
'options' => Mage::getModel('core/website')->getCollection()->toOptionHash(),
));
}
$this->addColumn('action',
array(
'header' => Mage::helper('catalog')->__('Action'),
'width' => '50px',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('catalog')->__('Edit'),
'url' => array(
'base'=>'*/*/edit',
'params'=>array('store'=>$this->getRequest()->getParam('store'))
),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
));
if (Mage::helper('catalog')->isModuleEnabled('Mage_Rss')) {
$this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS'));
}
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('entity_id');
$this->getMassactionBlock()->setFormFieldName('product');
$this->getMassactionBlock()->addItem('delete', array(
'label'=> Mage::helper('catalog')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('catalog')->__('Are you sure?')
));
$statuses = Mage::getSingleton('catalog/product_status')->getOptionArray();
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('catalog')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('catalog')->__('Status'),
'values' => $statuses
)
)
));
if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')){
$this->getMassactionBlock()->addItem('attributes', array(
'label' => Mage::helper('catalog')->__('Update Attributes'),
'url' => $this->getUrl('*/catalog_product_action_attribute/edit', array('_current'=>true))
));
}
Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this));
return $this;
}
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array(
'store'=>$this->getRequest()->getParam('store'),
'id'=>$row->getId())
);
}
credits #Raphael at Digital Pianism
Yeah, this is a common problem.
So the problem here is that, as your custom grid extends the original grid class
Mage_Adminhtml_Block_Catalog_Product_Grid,
when you call the following code:
return parent::_prepareColumns();
It goes to the original _prepareColumns method and thus, overwrite your custom columns.
To fix that you need to call the parent's parent method:
return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
Same goes for the
_prepareCollection method.
To avoid that kind of problems, I also sugget that you use event observers instead of rewriting blocks: https://magento.stackexchange.com/a/5986/2380
You have missed the renderer because of which it is not able to load data.
Try this:
$this->addColumn('owners',
array(
'header'=> Mage::helper('catalog')->__('Owner'),
'width'=>'200px',
'index' => 'owners',
'renderer' => 'adminhtml/widget_grid_column_renderer_owner'
));
Change the renderer path according to your requirement.

How to add a featured image for CMS page in Prestashop

I want to add an image from back-end for each CMS page I'm adding in Prestashop, just like we add featured images for posts/page in Wordpress.
I couldn't find any codes/modules which support this feature in prestashop.
It is possible, but it isn't straightforward. Here are the steps you need to do to implement image upload to the CMS page module. This approach is not the most elegant way to implement this in PrestaShop, but I hope it helps you to move forward.
Step 1, Update the model so it can contain the image:
First override 'classes/CMS.php' to 'override/classes/CMS.php'.
class CMS extends CMSCore
{
// add a public field to store the CMS image
public $CMS_IMG;
/**
* #see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'cms',
'primary' => 'id_cms',
'multilang' => true,
'fields' => array(
'id_cms_category' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'position' => array('type' => self::TYPE_INT),
'active' => array('type' => self::TYPE_BOOL),
// Lang fields
'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
'content' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999),
// add one image per page
'CMS_IMG' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999), ),
);
}
Step 2, Implement the code needed to upload the image in the backoffice:
Override 'controllers/admin/AdminCmsController.php' in 'override/controllers/admin/AdminCmsController.php'
class AdminCmsController extends AdminCmsControllerCore
{
public function renderForm()
{
$this->display = 'edit';
$this->toolbar_btn['save-and-preview'] = array(
'href' => '#',
'desc' => $this->l('Save and preview')
);
$this->initToolbar();
if (!$this->loadObject(true))
return;
$categories = CMSCategory::getCategories($this->context->language->id, false);
$html_categories = CMSCategory::recurseCMSCategory($categories, $categories[0][1], 1, $this->getFieldValue($this->object, 'id_cms_category'), 1);
// Add code to get image url
$image_url = '';
$imgName = $this->getImageValue($this->object);
if($imgName) {
$image = _PS_IMG_DIR_ . 'cms/' . $imgName;
$image_url = ImageManager::thumbnail($image, $this->table.'_'.(int)$this->object->id.'.'.$this->imageType, 350,
$this->imageType, true, true);
}
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => $this->l('CMS Page'),
'image' => '../img/admin/tab-categories.gif'
),
'input' => array(
// custom template
array(
'type' => 'select_category',
'label' => $this->l('CMS Category'),
'name' => 'id_cms_category',
'options' => array(
'html' => $html_categories,
),
),
array(
'type' => 'text',
'label' => $this->l('Meta title:'),
'name' => 'meta_title',
'id' => 'name', // for copy2friendlyUrl compatibility
'lang' => true,
'required' => true,
'class' => 'copy2friendlyUrl',
'hint' => $this->l('Invalid characters:').' <>;=#{}',
'size' => 50
),
array(
'type' => 'text',
'label' => $this->l('Meta description'),
'name' => 'meta_description',
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}',
'size' => 70
),
array(
'type' => 'tags',
'label' => $this->l('Meta keywords'),
'name' => 'meta_keywords',
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}',
'size' => 70,
'desc' => $this->l('To add "tags" click in the field, write something, then press "Enter"')
),
array(
'type' => 'text',
'label' => $this->l('Friendly URL'),
'name' => 'link_rewrite',
'required' => true,
'lang' => true,
'hint' => $this->l('Only letters and the minus (-) character are allowed')
),
array(
'type' => 'textarea',
'label' => $this->l('Page content'),
'name' => 'content',
'autoload_rte' => true,
'lang' => true,
'rows' => 5,
'cols' => 40,
'hint' => $this->l('Invalid characters:').' <>;=#{}'
),
/* Add an fileupload component to the form */
array(
'type' => 'file',
'label' => $this->l('Page image'),
'name' => 'CMS_IMG',
'desc' => $this->l('Upload an image for this page'),
'lang' => true,
'display_image' => true,
'image' => $image_url ? $image_url : false,
),
array(
'type' => 'radio',
'label' => $this->l('Displayed:'),
'name' => 'active',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
),
'submit' => array(
'title' => $this->l(' Save '),
'class' => 'button'
)
);
if (Shop::isFeatureActive())
{
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Shop association:'),
'name' => 'checkBoxShopAsso',
);
}
$this->tpl_form_vars = array(
'active' => $this->object->active
);
return AdminControllerCore::renderForm();
}
public function postProcess()
{
$languages = Language::getLanguages(false);
$update_images_values = false;
foreach ($languages as $lang)
{
if (isset($_FILES['CMS_IMG'])
&& isset($_FILES['CMS_IMG']['tmp_name'])
&& !empty($_FILES['CMS_IMG']['tmp_name']))
{
if ($error = ImageManager::validateUpload($_FILES['CMS_IMG'], 4000000))
return $error;
else
{
$ext = substr($_FILES['CMS_IMG']['name'], strrpos($_FILES['CMS_IMG']['name'], '.') + 1);
$file_name = md5($_FILES['CMS_IMG']['name']).'.'.$ext;
if (!move_uploaded_file($_FILES['CMS_IMG']['tmp_name'],
_PS_IMG_DIR_ .'cms'.DIRECTORY_SEPARATOR.$file_name))
return Tools::displayError($this->l('An error occurred while attempting to upload the file.'));
else
{
$values['CMS_IMG'][$lang['id_lang']] = $file_name;
}
}
$update_images_values = true;
$cms = new CMS((int)Tools::getValue('id_cms'));
$cms->CMS_IMG = $file_name;
$cms->update();
}
}
parent::postProcess();
}
public function getImageValue()
{
$db = Db::getInstance();
$sql = 'SELECT CMS_IMG FROM '._DB_PREFIX_.'cms_lang WHERE id_cms = ' . $this->object->id;
return $db->getValue($sql);
}
}
Step 3, Implement the code for the frontend
Overide 'controllers/front/CmsController.php' in 'override/controllers/front/CmsController.php'
class CmsController extends CmsControllerCore
{
/**
* Assign template vars related to page content
* #see CmsControllerCore::initContent()
*/
public function initContent()
{
if(!empty($this->cms->CMS_IMG)) {
$this->context->smarty->assign('cms_image', _PS_IMG_ . 'cms/' . $this->cms->CMS_IMG);
}
parent::initContent();
}
}
Step 4, use your image in the template
Now you can, e.g. in cms.tpl use the following code:
{if $cms_image != ''}
<img src="{$cms_image}">
{/if}
Based on: https://www.prestashop.com/forums/topic/141903-add-custom-field-to-cms-module/
Adding all these as separate answer, since some moderators seem to reject even most simplest fix/change to original answer as "This edit deviates from the original intent of the post" or "This edit was intended to address the author of the post" ..right.
Improving answer by 11mb
Step 0, Note that new field needs to be manually added in SQL table (use SQL or PhpMyAdmin structure edit):
ALTER TABLE `ps_cms_lang` ADD `CMS_IMG` TEXT NULL DEFAULT NULL;
Step 1.2, Class cache file needs to be deleted for changes to be active:
/cache/class_index.php
Step 4, use your image in the template
..
Or more universal form (also usable in category subpages - use CMS_IMG attribute (-> or . depends on code):
{if isset($cms->CMS_IMG) && $cms->CMS_IMG}
<img src="{$img_ps_dir}/cms/{$cms->CMS_IMG}">
{/if}
1. For a non-multiligual field - make these adjustments:
ALTER TABLE `ps_cms` ADD `CMS_IMG` TEXT NULL DEFAULT NULL;
'CMS_IMG' => array('type' => self::TYPE_STRING, 'lang' => false, ..
'lang' => false,
$sql = 'SELECT CMS_IMG FROM '._DB_PREFIX_.'cms WHERE id_cms = ' . $this->object->id;
+And remove all that lang stuff in postProcess()
2. Multiple fixes to postProcess()
code does not correctly save data - should not use $cms->update(); in postProcess() as triggers exception on adding new page, due partial data = just set $_POST['CMS_IMG'] value..
should generate unique filename (start with id) for each page.
should check for isSubmit to avoid processing in wrong chain (if same field name used in CMS categories).
Updated code:
public function postProcess()
{
if ( (Tools::isSubmit('submitAddcms') || Tools::isSubmit('viewcms'))
&& isset($_FILES['CMS_IMG'])
&& isset($_FILES['CMS_IMG']['tmp_name'])
&& !empty($_FILES['CMS_IMG']['tmp_name']))
{
$id = (int)Tools::getValue('id_cms');
if ($error = ImageManager::validateUpload($_FILES['CMS_IMG'], 4000000))
return $error;
else
{
$ext = substr($_FILES['CMS_IMG']['name'], strrpos($_FILES['CMS_IMG']['name'], '.') + 1);
$file_name = $id .'_cms_'. md5($_FILES['CMS_IMG']['name']).'.'.$ext;
if (!move_uploaded_file($_FILES['CMS_IMG']['tmp_name'],
_PS_IMG_DIR_.'cms'.DIRECTORY_SEPARATOR.$file_name))
return Tools::displayError($this->l('An error occurred while attempting to upload the file.'));
else {
$_POST['CMS_IMG'] = $file_name;
#chmod(_PS_IMG_DIR_.'cms'.DIRECTORY_SEPARATOR.$file_name, 0666);
}
}
}
parent::postProcess();
}
The above solutions just do not fit with PrestaShop's philosophy.
Adding a featured image for a CMS page should mimic existing mecanisms (see creation/edition of categories or suppliers logo).
Following is my solution, working for PrestaShop 1.6.1.7. It will add an image field, upload image to img/cms folder and create all thumbnails.
I did not figure out how to delete the image yet, you are more than welcome to comment my answer.
Be careful when updating PrestaShop, files out of the override folder may be updated.
1/ Add the following to defines.inc.php (after line 137):
define('_PS_CMS_IMG_DIR_', _PS_IMG_DIR_.'cms/');
2/ Add the following to defines_uri.inc.php (after line 61):
define('_THEME_CMS_DIR_', _PS_IMG_.'cms/');
3/ Add rules to .htaccess file:
RewriteRule ^cms/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/cms/$1$2$3.jpg [L]
RewriteRule ^cms/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/cms/$1$2.jpg [L]
4/ Override classes/CMS.php to override/classes/CMS.php:
<?php
class CMS extends CMSCore
{
// Add an id_image attribute to allow further tests in cms.tpl
public $id_image = 'default';
public function __construct($id = null, $id_lang = null, $id_shop = null)
{
parent::__construct($id, $id_lang, $id_shop);
$this->id_image = ($this->id && file_exists(_PS_CMS_IMG_DIR_.(int)$this->id.'.jpg')) ? (int)$this->id : false;
$this->image_dir = _PS_CMS_IMG_DIR_;
}
}
?>
5/ Override classes/ImageType.php to override/classes/ImageType.php
Add a public attribute to the class: public $cms
Append to $definition=>fields: 'cms' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool')
In getByNameNType, append to $types var: 'cms'
6/ Override classes/Link.php to override/classes/Link.php:
class Link extends LinkCore
{
public function getCmsImageLink($name, $id_cms, $type = null)
{
if ($this->allow == 1 && $type) {
$uri_path = __PS_BASE_URI__.'cms/'.$id_cms.'-'.$type.'/'.$name.'.jpg';
} else {
$uri_path = _THEME_CAT_DIR_.$id_cms.($type ? '-'.$type : '').'.jpg';
}
return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
}
}
7/ Override controllers/AdminCmsController.php to override/controllers/AdminCmsController.php:
Append the following to the __contruct method, right before parent::__construct():
$this->fieldImageSettings = array(
'name' => 'banner_img',
'dir' => 'cms',
);
Append the following to the postProcess method, right after line 351 (in one of the elseif blocks, look for a condition on submitAddcms and submitAddcmsAndPreview):
$object = $this->loadObject();
$image_is_posted = $this->postImage($object->id);
if (! $image_is_posted) {
throw new PrestaShopException("image not posted...");
}
In the renderForm method, start with:
if (!($obj = $this->loadObject(true))) {
return;
}
$image = _PS_CMS_IMG_DIR_.$obj->id.'.jpg';
$image_url = ImageManager::thumbnail($image, $this->table.'_'.$obj->id.'.'.$this->imageType, 350,
$this->imageType, true, true);
$image_size = file_exists($image) ? filesize($image) / 1000 : false;
if (Validate::isLoadedObject($this->object)) {
$this->display = 'edit';
} else {
$this->display = 'add';
}
and append the following to the attribute $this->fields_form, for example after the content field:
array(
'type' => 'file',
'label' => $this->l('Banner image'),
'name' => 'banner_img',
'display_image' => true,
'image' => $image_url ? $image_url : false,
'size' => $image_size,
'hint' => $this->l('Upload a banner image from your computer.')
)
Add and afterImageUpload method:
protected function afterImageUpload()
{
$return = true;
$generate_hight_dpi_images = (bool)Configuration::get('PS_HIGHT_DPI');
$object = $this->loadObject(true);
/* Generate image with differents size */
if (($object->id = (int)Tools::getValue('id_cms')) &&
isset($_FILES) && count($_FILES) && file_exists(_PS_CMS_IMG_DIR_.$object->id.'.jpg')) {
$images_types = ImageType::getImagesTypes('cms');
foreach ($images_types as $k => $image_type) {
$file = _PS_CMS_IMG_DIR_.$object->id.'.jpg';
if (!ImageManager::resize($file, _PS_CMS_IMG_DIR_.$object->id.'-'.stripslashes($image_type['name']).'.jpg', (int)$image_type['width'], (int)$image_type['height'])) {
$return = false;
}
if ($generate_hight_dpi_images) {
if (!ImageManager::resize($file, _PS_CMS_IMG_DIR_.$object->id.'-'.stripslashes($image_type['name']).'2x.jpg', (int)$image_type['width']*2, (int)$image_type['height']*2)) {
$return = false;
}
}
}
$current_logo_file = _PS_CMS_IMG_DIR_.'cms_mini_'.$object->id.'_'.$this->context->shop->id.'.jpg';
if (file_exists($current_logo_file)) {
unlink($current_logo_file);
}
}
return $return;
}
8/ Override controllers/admin/AdminImagesController.php to override/controllers/admin/AdminImagesController.php
In the __construct method, append to $this->fields_list: 'cms' => array('title' => $this->l('CMS'), 'align' => 'center', 'type' => 'bool', 'callback' => 'printEntityActiveIcon', 'orderby' => false)
In the __contruct method, append to $this->fields_form=>input:
array(
'type' => 'switch',
'label' => $this->l('Pages CMS'),
'name' => 'cms',
'required' => false,
'is_bool' => true,
'hint' => $this->l('This type will be used for CMS banner images.'),
'values' => array(
array(
'id' => 'cms_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'cms_off',
'value' => 0,
'label' => $this->l('Disabled')
),
)
),
In initRegenerate method, append to $types var: 'cms' => $this->l('CMS pages')
In the _regenerateThumbnails method, append to $process array: array('type' => 'cms', 'dir' => _PS_CMS_IMG_DIR_)
9/ Update your CMS page template cms.tpl with something like this:
<!-- Check out the default theme's category_header.tpl for original PrestaShop code -->
<div class="cms_banner"{if $cms->id_image} style="background:url({$link->getCmsImageLink($cms->link_rewrite, $cms->id_image, 'large')|escape:'html':'UTF-8'}) right center no-repeat; background-size:cover; "{/if}>
<div class="container">
<!-- Your content here -->
</div>
</div>
To prevent the loose of the image when you're updating the datas. (This appends with the 1.7) I suggest the following improvement in the postProcess method :
public function postProcess()
{
$languages = Language::getLanguages(false);
$update_images_values = false;
$cms = new CMS((int)Tools::getValue('id_cms'));
foreach ($languages as $lang)
{
if (isset($_FILES['CMS_IMG'])
&& isset($_FILES['CMS_IMG']['tmp_name'])
&& !empty($_FILES['CMS_IMG']['tmp_name']))
{
if ($error = ImageManager::validateUpload($_FILES['CMS_IMG'], 4000000))
return $error;
else
{
$ext = substr($_FILES['CMS_IMG']['name'], strrpos($_FILES['CMS_IMG']['name'], '.') + 1);
$file_name = md5($_FILES['CMS_IMG']['name']).'.'.$ext;
if (!move_uploaded_file($_FILES['CMS_IMG']['tmp_name'],
_PS_IMG_DIR_ .'cms'.DIRECTORY_SEPARATOR.$file_name))
return Tools::displayError($this->l('An error occurred while attempting to upload the file.'));
else
{
$values['CMS_IMG'][$lang['id_lang']] = $file_name;
}
}
$update_images_values = true;
$cms->CMS_IMG = $file_name;
$cms->update();
}
else
{
$_POST['CMS_IMG'] = $cms->CMS_IMG;
}
}
self::saveProducts();
parent::postProcess();
}
That adds an else condition to the original one
// add one image per page
'CMS_IMG' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999), ),
Above argument can not create a field on cms_lang table.
So system can gives the following error
Unknown column 'cms_image' in 'field list'
if ($webservice_call && $errno) {
$dbg = debug_backtrace();
WebserviceRequest::getInstance()->setError(500, '[SQL Error] '.$this->getMsgError().'. From '.(isset($dbg[3]['class']) ? $dbg[3]['class'] : '').'->'.$dbg[3]['function'].'() Query was : '.$sql, 97);
} elseif (_PS_DEBUG_SQL_ && $errno && !defined('PS_INSTALLATION_IN_PROGRESS')) {
if ($sql) {
throw new PrestaShopDatabaseException($this->getMsgError().'<br /><br /><pre>'.$sql.'</pre>');
}
throw new PrestaShopDatabaseException($this->getMsgError());
}
}

Zend Form Duplicate Entry Email Exception

Having a registration zend form in view looks like this :
<?php
$form = $this->form;
if(isset($form)) $form->prepare();
$form->setAttribute('action', $this->url(NULL,
array('controller' => 'register', 'action' => 'process')));
echo $this->form()->openTag($form);
?>
<dl class="form-signin">
<dd><?php
echo $this->formElement($form->get('name_reg'));
echo $this->formElementErrors($form->get('name_reg'));
?></dd>
<dd><?php
echo $this->formElement($form->get('email_reg'));
echo $this->formElementErrors($form->get('email_reg'));
?></dd>
<dd><?php
echo $this->formElement($form->get('password_reg'));
echo $this->formElementErrors($form->get('password_reg'));
?></dd>
<dd><?php
echo $this->formElement($form->get('confirm_password_reg'));
echo $this->formElementErrors($form->get('confirm_password_reg'));
?></dd>
<br>
<dd><?php
echo $this->formElement($form->get('send_reg'));
echo $this->formElementErrors($form->get('send_reg'));
?></dd>
<?php echo $this->form()->closeTag() ?>
And RegisterController as following.
<?php
namespace Test\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Test\Form\RegisterForm;
use Test\Form\RegisterFilter;
use Test\Form\LoginFormSm;
use Test\Form\LoginFilter;
use Test\Model\User;
use Test\Model\UserTable;
class RegisterController extends AbstractActionController
{
public function indexAction()
{
$this->layout('layout/register');
$form = new RegisterForm();
$form_sm = new LoginFormSm();
$viewModel = new ViewModel(array(
'form' => $form,
'form_sm' =>$form_sm,
));
return $viewModel;
}
public function processAction()
{
$this->layout('layout/register');
if (!$this->request->isPost()) {
return $this->redirect()->toRoute(NULL,
array( 'controller' => 'index'
)
);
}
$form = $this->getServiceLocator()->get('RegisterForm');
$form->setData($this->request->getPost());
if (!$form->isValid()) {
$model = new ViewModel(array(
'form' => $form,
));
$model->setTemplate('test/register/index');
return $model;
}
// Creating New User
$this->createUser($form->getData());
return $this->redirect()->toRoute(NULL, array (
'controller' => 'auth' ,
));
}
protected function createUser(array $data)
{
$userTable = $this->getServiceLocator()->get('UserTable');
$user = new User();
$user->exchangeArray($data);
$userTable->saveUser($user);
return true;
}
}
Also a RegisterForm where are declared all variables shown in index. Also RegisterFilter as following:
<?php
namespace Test\Form;
use Zend\InputFilter\InputFilter;
class RegisterFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'email_reg',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags',
),
array(
'name' => 'StringTrim',
),
),
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
'messages' => array(
\Zend\Validator\EmailAddress::INVALID_FORMAT => 'Email address format is invalid'
),
),
),
array(
'name' => 'AbstractDb',
'options' => array(
'domain' => true,
'messages' => array(
\Zend\Validator\Db\AbstractDb::ERROR_RECORD_FOUND => 'Current Email Already registered'
),
),
),
),
));
$this->add(array(
'name' => 'name_reg',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags',
),
array(
'name' => 'StringTrim',
),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 140,
),
),
),
));
$this->add(array(
'name' => 'password_reg',
'required' => true,
'validators' => array(
array(
'name' => 'StringLength',
'options' =>array(
'encoding' => 'UTF-8',
'min' => 6,
'messages' => array(
\Zend\Validator\StringLength::TOO_SHORT => 'Password is too short; it must be at least %min% ' . 'characters'
),
),
),
array(
'name' => 'Regex',
'options' =>array(
'pattern' => '/[A-Z]\d|\d[A-Z]/',
'messages' => array(
\Zend\Validator\Regex::NOT_MATCH => 'Password must contain at least 1 digit and 1 upper-case letter'
),
),
),
),
));
$this->add(array(
'name' => 'confirm_password_reg',
'required' => true,
'validators' => array(
array(
'name' => 'Identical',
'options' => array(
'token' => 'password_reg', // name of first password field
'messages' => array(
\Zend\Validator\Identical::NOT_SAME => "Passwords Doesn't Match"
),
),
),
),
));
}
}
Problem
All i need is to throw a message when somebody tries to register and that e-mail is already registered. Tried with \Zend\Validator\Db\AbstractDb and added following validator to email in RegisterFilter as following
array(
'name' => 'AbstractDb',
'options' => array(
'domain' => true,
'messages' => array(
\Zend\Validator\Db\AbstractDb::ERROR_RECORD_FOUND => 'Current Email Already registered'
),
),
),
But that seems not to work.
Question: Is there a way to implement this validator in RegisterController?
Additional.
I've deleted from RegisterFilert validator and put inside Module.ph
'EmailValidation' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$validator = new RecordExists(
array(
'table' => 'user',
'field' => 'email',
'adapter' => $dbAdapter
)
);
return $validator;
},
And call it from RegisterController
$validator = $this->getServiceLocator()->get('EmailValidation');
if (!$validator->isValid($email)) {
// email address is invalid; print the reasons
$model = new ViewModel(array(
'error' => $validator->getMessages(),
'form' => $form,
));
$model->setTemplate('test/register/index');
return $model;
}
And when i use print_r inside view to check for it shows.
Array ( [noRecordFound] => No record matching the input was found ).
I want just to echo this 'No record matching the input was found'.
Fixed
Modified in RegisterController as following
$validator = $this->getServiceLocator()->get('EmailValidation');
$email_ch = $this->request->getPost('email_reg');
if (!$validator->isValid($email_ch)) {
// email address is invalid; print the reasons
$model = new ViewModel(array(
'error' => 'Following email is already registered please try another one',
'form' => $form,
));
$model->setTemplate('test/register/index');
return $model;
}
I had compared
if (!$validator->isValid($email_ch))
Before with nothing and that's why i needed to add first
$email_ch = $this->request->getPost('email_reg');
You don't have to do that in your controller, just try this in your RegisterFilter class :
First : Add $sm as parameter of the _construct() method :
public function __construct($sm) {....}
Second: Replace e-mail validation by this one :
$this->add ( array (
'name' => 'email_reg',
'required' => true,
'validators' => array(
array(
'name' => 'Zend\Validator\Db\NoRecordExists',
'options' => array(
'table' => 'user',
'field' => 'email',
'adapter' => $sm->get ( 'Zend\Db\Adapter\Adapter' ),
'messages' => array(
NoRecordExists::ERROR_RECORD_FOUND => 'e-mail address already exists'
),
),
),
),
)
);
When you call the RegisterFilter don't forget to pass the serviceLocator as parameter in your controller :
$form->setInputFilter(new RegisterFilter($this->getServiceLocator()));
Supposing you have this in your global.php File (config\autoload\global.php) :
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
To your previous request, ("I want just to echo this 'No record matching the input was found'")
$validator = $this->getServiceLocator()->get('EmailValidation');
$email_ch = $this->request->getPost('email_reg');
if (!$validator->isValid($email_ch)) {
// email address is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
$msg = $message;
}
$model = new ViewModel(array(
'error' => $msg,
'form' => $form,
));
$model->setTemplate('test/register/index');
return $model;
}

Passing non-form data before save (CakePHP)

Course HasMany Lesson.
I am trying to save a lesson in a Course view, but want to automatically assign the current course_id without them having to select it.
How do I do that?
Here is my Courses view controller:
public function view($id = null) {
if (!$this->Course->exists($id)) {
throw new NotFoundException(__('Invalid course'));
}
$options = array('conditions' => array('Course.' . $this->Course->primaryKey => $id));
$this->set('course', $this->Course->find('first', $options));
//Adding Lesson from Course View
if ($this->request->is('post')) {
$this->Course->Lesson->create();
if ($this->Course->Lesson->save($this->request->data)) {
$this->Session->setFlash(__('The lesson has been saved.'), array ('class' => 'alert alert-success'));
return $this->redirect(array('controller'=> 'lessons', 'action' => 'view', $this->Course->Lesson->id));
} else {
$this->Session->setFlash(__('The lesson could not be saved. Please, try again.'), array ('class' => 'alert alert-danger'));
}
}
$courses = $this->Course->Lesson->Course->find('list');
$this->set(compact('courses'));
}
Currently, it's saving the lesson but not passing the course_id to the new lesson.
Lesson Model:
public $belongsTo = array(
'Course' => array(
'className' => 'Course',
'foreignKey' => 'course_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Course Model:
public $hasMany = array(
'Lesson' => array(
'className' => 'Lesson',
'foreignKey' => 'course_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
This is basically a duplicate of CakePHP: Securely setting a default value on a form.
Check my answer there or read the blog article I've wrote after that answer.

Categories