<?php
namespace Moto\Application\Menus\InputFilter; use Moto\InputFilter\AbstractInputFilter; use Zend\InputFilter\Exception; use Moto; class Save extends AbstractInputFilter { protected $_name = 'menus.save'; protected $_defaultsValue = array(); public function init() { $this->add(array( 'name' => 'id', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( "name" => "Digits", 'break_chain_on_failure' => true, ), array( 'name' => 'Db\RecordExists', 'options' => array( 'table' => Moto\Config::get('database.prefix') . 'menus', 'field' => 'id', 'adapter' => Moto\Config::get('databaseAdapter'), ) ) ), )); $this->_addElementName(); $this->add(array( 'name' => 'items', 'required' => false, 'filters' => array( array('name' => 'Moto\Application\Menus\Filter\MenuTree') ), 'validators' => array( array( 'name' => 'Moto\Validator\Unique', 'break_chain_on_failure' => true, ), array( 'name' => 'Moto\Application\Menus\Validator\MenuTree', ), ) )); } protected function _addElementName($currentId = 0) { $this->remove('name'); $this->add(array( 'name' => 'name', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 200, ), 'break_chain_on_failure' => true, ), array( 'name' => 'Db_NoRecordExists', 'options' => array( 'table' => Moto\Config::get('database.prefix') . 'menus', 'field' => 'name', 'adapter' => Moto\Config::get('databaseAdapter'), 'exclude' => array( 'field' => 'id', 'value' => (int)$currentId, ) ) ) ), )); } public function setData($data) { if (!empty($data['id'])) { $this->_addElementName($data['id']); } $result = parent::setData($data); return $result; } }