Yii CGridView handle special cases using model function - php

Having some trouble with CGridView on Yii Framework...
I'm looking to replace the contents of a column based on the value it holds.
I need to handle special cases so I added a function into the model to return a value to the GridView.
I get the resulting error " Undefined variable: model ".
I'm sure it's likely something simple. Is it because my dataProvider is not model?
Here is a shortened version of my code:
<?php
/* #var $this BookController */
/* #var $dataProvider CActiveDataProvider */
/* #var $model Book */
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'book-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'userName',
'header'=>'Name',
),
array(
'name'=>'status',
'header'=>'Status',
'type'=>'raw',
'value'=>array($model, 'statusText')
),
)
));
?>
And here is code in models/Book.php
class Book extends CActiveRecord
{
...
...
public function statusText($data, $row) {
$content = '';
if (CHtml::encode($data->status) == "processed") {
$content = "Process completed";
}
else if ($data->status=="") {
$content = "Queued for Processing";
}
else {
$content = CHtml::encode($data->status);
}
return $content;
}
...
...
}

Here is a simplified example from my current project;
<?php
//My controller
class NewsController extends CController {
//The admin action
public function actionAdmin() {
$model = new News;
$this->render('admin', array(
'model' => $model
));
}
}
//In my view file
$this->widget('ext.widgets.MyTbGridView', array(
'dataProvider' => $model->search(),
'columns' => array(
array(
'name' => 'id',
'filter' => false,
),
array(
'name' => 'title',
),
array(
'value' => array($model, 'gridDate')
),
),
));
//My model function
class News extends CActiveRecord {
public function gridDate($data, $row) {
return 'Date formatted!';
}
}
?>
The code 'value' => array($model, 'gridFormatDate'), is important. there are two possibilities here. The function can reside in the controller, in which case it should be 'value' => array($this, 'gridFormatDate'), or it can be in the model, in which case the correct code is given

In stead of array($model, 'statusText'), try '$data->statusText'.
The method in your model should be like this:
public function getStatusText() {
$content = '';
if (CHtml::encode($this->status) == "processed") {
$content = "Process completed";
}
else if ($this->status=="") {
$content = "Queued for Processing";
}
else {
$content = CHtml::encode($this->status);
}
return $content;
}

Related

Can implement dynamic validation on element level?

Can implement dynamic validation on element level? I used this example to implement validation of one element dependent on the value of the other element. But for this purpose I'll need to implement this for every single form where I use this element (comment) with this validation. I have many forms like that. Is there way to do the following:
to take this filter/validation logic to the element level using some kind of "data-comment-for" attribute and retrieving the value of the element on which it depends from the parent form.
This is my current code (but I need to have it for every form now. It does not look elegant at all) :
class CompetencyAdvanceHumanRightsAndJusticeFormFilter extends InputFilter
{
public function isValid($context = null)
{
$figradeCommentName = 'applJusticeFIGrade'.'Comment';
$forGrade = $this->get('applJusticeFIGrade');
$gradeComment = $this->get($figradeCommentName);
$applJusticeFIGradeRawValue = $forGrade->getRawValue('applJusticeFIGrade');
if(is_numeric($applJusticeFIGradeRawValue)){
$gradeValue = intval($applJusticeFIGradeRawValue);
}else{
$gradeValue = $applJusticeFIGradeRawValue;
}
if ($gradeValue != 'na' && $gradeValue > 0) {
$gradeComment->setRequired(true);
$validatorChain = new Validator\ValidatorChain();
$validatorChain->attach(
new Validator\NotEmpty(),
true
);
$gradeComment->setValidatorChain($validatorChain);
}
return parent::isValid($context);
}
public function __construct(){
$this->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$this->add(array(
'name' => 'studEvalId',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
}
}
EDIT:
I added code for the custom element to the question. There are some "leftovers" of my attempts to place this logic to the element level.
Comment Element
class Comment extends Element implements InputProviderInterface
{
/**
* #var ValidatorInterface
*/
protected $validator;
// set its type
protected $attributes = array(
'type' => 'comment'
);
public function init()
{
if (null === $this->validator) {
$validator = new StringLength();
$validator->setMax(10);
$validator->setMessage('The comment should not exceed 1000 letters!', StringLength::INVALID);
$this->validator = $validator;
}
}
/**
* Get a validator if none has been set.
*
* #return ValidatorInterface
*/
public function getValidator()
{
return $this->validator;
}
/**
* #param ValidatorInterface $validator
* #return $this
*/
public function setValidator(ValidatorInterface $validator)
{
$this->validator = $validator;
return $this;
}
/**
* remove require and validator defaults because we have none
*
* #return array
*/
public function getInputSpecification()
{
// return array(
// 'name' => $this->getName(),
// 'required' => false,
// 'validators' => array(
// $this->getValidator(),
// ),
// 'filters' => array(
// new FIGradeCommentDynamicBufferFilter()
// ),
// );
return array(
'name' => $this->getName(),
'required' => false,
'filters' => array(
array('name' => 'Zend\Filter\StringTrim'),
),
'validators' => array(
$this->getValidator(),
),
);
}
// tell it where to find its view helper, so formRow and the like work correctly
public function getViewHelperConfig()
{
return array('type' => '\OnlineFieldEvaluation\View\Helper\FormComment');
}
}
You could make a base abstract input-filter class and an interface and make all your form filters extend the base class that implements the interface with the methods you expect inside your form classes to make the thing work correctly.
Make an interface with the methods:
interface GradeCommentFormFilterInterface()
{
protected function getGradeInput();
protected function getCommentInput();
}
Then you move the common code to your base class:
abstract class BaseGradeCommentFormFilter extends InputFilter implements GradeCommentFormFilterInterface
{
protected function getGradeInput()
{
return $this->get(static::GRADE_NAME);
}
protected function getCommentInput()
{
return $this->get(static::GRADE_NAME . 'Comment');
}
public function isValid($context = null)
{
$gradeInput = $this->getGradeInput();
$commentInput = $this->getCommentInput();
$rawValue = $this->getRawValue($gradeInput);
if(is_numeric($rawValue))
{
$gradeValue = intval($rawValue);
}
else
$gradeValue = $rawValue;
if ($gradeValue != 'na' && $gradeValue > 0) {
$commentInput->setRequired(true);
$validatorChain = new Validator\ValidatorChain();
$validatorChain->attach(
new Validator\NotEmpty(),
true
);
$commentInput->setValidatorChain($validatorChain);
}
return parent::isValid($context);
}
}
Now you can use your abstract class like this:
class CompetencyAdvanceHumanRightsAndJusticeFormFilter extends BaseGradeCommentFormFilter
{
const GRADE_NAME = 'applJusticeFIGrade';
//... other code
}
I quickly tried to make it work for your case, but this isn't tested, and probably there are ways to optimize this, but it gives you an idea of what you can do.

I do not see the save button on my form even though I have set it up in Admin Area Magento

I am new to mangeto framework and i am learning to create a form in admin area section. However, it has been over hours that I could not figure out the error i am receiving:
Recoverable Error: Argument 1 passed to Mage_Adminhtml_Controller_Action::_addContent() must be an instance of Mage_Core_Block_Abstract, boolean given, called in /vagrant/magento/app/code/local/MasteringMagento/Example/controllers/Adminhtml/EventController.php on line 12.
The following is my Edit.php file as well as my Form.php file
Edit.php:
class MasteringMagento_Example_Adminhtml_EventController extends Mage_Adminhtml_Controller_Action{
public function indexAction(){
$this->loadLayout();
$this->_addContent(
$this->getLayout()->createBlock('example/adminhtml_event_edit'));
//go straight to the php file to render the form. otherwise this will not perfomed.
$this->renderLayout();
}
public function saveAction(){
$eventID = $this->getRequest()->getParam('event_id');
$eventModel = Mage::getModel('example/event')->load($eventID);
if($data = $this->getRequest()->getPost()){
try{
$eventModel->addData($data)->save();
Mage::getSingleton('adminhtml/session')->addSuccess(
$this->__('Your event has been saved')
);
}catch(Exception $e){
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
$this->_redirect('*/*/index');
}
}
}
and Form.php file:
class MasteringMagento_Example_Block_Adminhtml_Event_Edit_Form extends Mage_Adminhtml_Block_Widget_Form{
protected function _prepareForm(){
$form = new Varien_Data_Form(array('id'=>'edit_form',
'action'=>$this->getData('action'), 'method'=>'post'));
$fieldset = $form->addFieldset('base_fieldset',
array('legend'=>Mage::helper('example')->__('General Information'),
'class'=>'fieldset-wide'));
$fieldset->addField('name', 'text', array(
'name' => 'name',
'label' => Mage::helper('example')->__('Event Name'),
'title' => Mage::helper('example')->__('Event Name'),
'required' => true
));
$dateFormatIso = Mage::app()->getLocale()->getDateFormat(
Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField('start', 'date', array(
'name' => 'start',
'format' => $dateFormatIso,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'label' => Mage::helper('example')->__('Start Date'),
'title' => Mage::helper('example')->__('Start Date'),
'required' => true
));
$fieldset->addField('end', 'date', array(
'name' => 'end',
'format' => $dateFormatIso,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'label' => Mage::helper('example')->__('End Date'),
'title' => Mage::helper('example')->__('End Date'),
'required' => true
));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
The error i think is from my Controller. However, if i direct the url link to the form, it will display. But if i direct to its container which is Edit.php, the error above would occurs:
class MasteringMagento_Example_Adminhtml_EventController extends Mage_Adminhtml_Controller_Action{
public function indexAction(){
$this->loadLayout();
$this->_addContent(
$this->getLayout()->createBlock('example/adminhtml_event_edit'));
//go straight to the php file to render the form. otherwise this will not perfomed.
$this->renderLayout();
}}
This is my config.xml. I did include the base class for Magento Blocks:
<blocks>
<example>
<class>MasteringMagento_Example_Block</class>
</example>
</blocks>
Please help me to identify the problem. Thanks
createBlock() is an abstract factory pattern for Blocks inside of Magento. Whenever Magento cannot resolve a factory class from this method, a boolean is returned... which is the case in your example, as the error message says.
Check your class MasteringMagento_Example_Block_Adminhtml_Event_Edit for spelling, casing, or class related errors. Also ensure that your class file is located at app/code/local/MasteringMagento/Example/Block/Adminhtml/Event/Edit.php.
In magento, every admin form block is loaded first by a form container.
Here, you call MasteringMagento_Example_Block_Adminhtml_Event_Edit class :
$this->getLayout()->createBlock('example/adminhtml_event_edit')
This class, located in app/code/local/MasteringMagento/Example/Block/Adminhtml/Post/Edit.php, should look to something like this :
<?php
/**
* MasteringMagento_Example_Block_Adminhtml_Post_Edit
*/
class MasteringMagento_Example_Block_Adminhtml_Post_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
// $this->_objectId = 'id';
parent::__construct();
$this->_blockGroup = 'example';
$this->_controller = 'adminhtml_post';
$this->_mode = 'edit';
$modelTitle = $this->_getModelTitle();
$this->_updateButton('save', 'label', $this->_getHelper()->__("Save $modelTitle"));
$this->_addButton('saveandcontinue', array(
'label' => $this->_getHelper()->__('Save and Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
$this->_formScripts[] = "
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
";
}
protected function _getHelper(){
return Mage::helper('example');
}
protected function _getModel(){
return Mage::registry('exemple_youmodel');
}
protected function _getModelTitle(){
return 'Post';
}
public function getHeaderText()
{
$model = $this->_getModel();
$modelTitle = $this->_getModelTitle();
if ($model && $model->getId()) {
return $this->_getHelper()->__("Edit $modelTitle (ID: {$model->getId()})");
}
else {
return $this->_getHelper()->__("New $modelTitle");
}
}
/**
* Get URL for back (reset) button
*
* #return string
*/
public function getBackUrl()
{
return $this->getUrl('*/*/index');
}
public function getDeleteUrl()
{
return $this->getUrl('*/*/delete', array($this->_objectId => $this->getRequest()->getParam($this->_objectId)));
}
}
As you can see, all your buttons are set in the __construct() method.
Hope it helps.

Select element from other table on the form zf2

i have 2 table with ManyToOne relation on the database between client and sale and i want to select the id_client on the Sale Form . for that o used that .
SaleForm :
public function __construct(ClientTable $table)
{
parent::__construct('vente');
$this->setAttribute('method', 'post');
$this->clientTable = $table;
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(
array(
'name' => 'id_client',
'type' => 'Select',
'attributes' => array(
'id' => 'id_client'
),
'options' => array(
'label' => 'Catégory',
'value_options' => $this->getClientOptions(),
'empty_option' => '--- Sélectionnez une categorie---'
),
)
);
public function getClientOptions()
{
$data = $this->clientTable->fetchAll()->toArray();
$selectData = array();
foreach ($data as $key => $selectOption) {
$selectData[$selectOption["id"]] = $selectOption["nom_client"];
}
return $selectData;
}
}
SaleController:
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Caisse\Model\Sale;
use Caisse\Form\SaleForm;
class SaleController extends AbstractActionController
{
protected $saleTable;
protected $clientTable;
public function addAction()
{
$form = new SaleForm($this->clientTable);
$form->get('submit')->setValue('Ajouter');
$request = $this->getRequest();
if ($request->isPost()) {
$vente = new Sale();
$form->setInputFilter($sale->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$vente->exchangeArray($form->getData());
$this->getSaleTable()->saveSale($sale);
return $this->redirect()->toRoute('sale');
}
}
return array('form' => $form);
}
}
But every Time i had this issue:
Catchable fatal error: Argument 1 passed to
Caisse\Form\SaleForm::__construct() must be an instance of
Admin\Model\ClientTable, null given.
Is this the good method to do it, any reference for same example will be welcome.
Thank you
Inside your controller, function addAction, you never set the variable clientTable maybe you forgot to initialize it.
Like this
public function addAction()
{
$this->clientTable = $this->getServiceLocator()->get('Client\Model\ClientTable');
$form = new SaleForm($this->clientTable);
// ...
}
About
public function getClientOptions()
{
$data = $this->clientTable->fetchAll();
$selectData = array();
foreach ($data as $row) {
$selectData[$row->id] = $row->nom_client;
}
return $selectData;
}

Yii CGridview hide rows

I have three rows in my yii CGridview and having two type of user logins.I have to hide one of the three rows depending on the type of user.Please help.
in your model add public static method
for example:
class Post extends CActiveRecord {
public function tableName() {
return 'posts';
}
public function rules() {
return array();
}
public function attributeLabels() {
return array();
}
...
public static function rulesUser() {
if ( Yii::app()->user->id = 1 ) {
return True;
} else {
return False;
}
}
add in your gridview for row:
$this->widget('zii.widgets.grid.CGridView',
array(
'id' => 'posts-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'emptyText' => '',
'columns' => array(
'id',
'title',
'post',
'date_create',
array(
'name' => 'status',
'visible' => Posts::rulesUser(),
)
array(
'class' => 'CButtonColumn',
),
),
)
);
Use the Conditional statements like this:
public function newsearch()
{
$id= Yii::app()->user->id;
if($id = Your conditon){
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
Your Criteria to display
} else {
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
Your Criteria to display
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
And Call this dataprovider in your GridView data provider.
or add in model
public $visible = True;
in you search add if or switch
public function search()
{
$criteria = new CDbCriteria;
...
if (any if){
$this->visible = 1;
}
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
in your gridview
...
array(
'name' => 'value',
'visible'=>$model->visible,
)
...
I have solved the task by conditioning the CDbCriteria as follows,
if(!Yii::app()->session["admin"])
{
$criteria->condition='t.unique_id!="i-8874c6e3"';
}
Thanks all.

How to create form inputs/elements in ZF2

EDIT : My main question has now become 'How do I get the ServiceManager with the doctrine entity manager into the hands of my form, element, and input classes in some clean way?' Read on to see the full post.
I'm going to try and ask by example here so bear with me. Let me know where I'm going wrong/right or where I could improve
I'm trying to create a registration form. I could use ZfcUser module but I want to do this on my own. I'm using ZF2 with Doctrine2 as well so that leads me away from that module a bit.
My strategy was this,
Create a form class called registration form
Create separate 'element' classes for each element where each element will have an input specification
Since each element is a separate class from the form I can unit test each one separately.
All seemed fine until I wanted to add a validator to my username element that would check that the username is NOT is use yet.
Here is the code thus far
namepsace My\Form;
use Zend\Form\Form,
Zend\Form\Element,
Zend\InputFilter\Input,
Zend\InputFilter\InputFilter,
/**
* Class name : Registration
*/
class Registration
extends Form
{
const USERNAME = 'username';
const EMAIL = 'email';
const PASSWORD = 'password';
const PASS_CONFIRM = 'passwordConfirm';
const GENDER = 'gender';
const CAPTCHA = 'captcha';
const CSRF = 'csrf';
const SUBMIT = 'submit';
private $captcha = 'dumb';
public function prepareForm()
{
$this->setName( 'registration' );
$this->setAttributes( array(
'method' => 'post'
) );
$this->add( array(
'name' => self::USERNAME,
'type' => '\My\Form\Element\UsernameElement',
'attributes' => array(
'label' => 'Username',
'autofocus' => 'autofocus'
)
)
);
$this->add( array(
'name' => self::SUBMIT,
'type' => '\Zend\Form\Element\Submit',
'attributes' => array(
'value' => 'Submit'
)
) );
}
}
I removed a lot that I think isn't necessary. Here is my username element below.
namespace My\Form\Registration;
use My\Validator\UsernameNotInUse;
use Zend\Form\Element\Text,
Zend\InputFilter\InputProviderInterface,
Zend\Validator\StringLength,
Zend\Validator\NotEmpty,
Zend\I18n\Validator\Alnum;
/**
*
*/
class UsernameElement
extends Text
implements InputProviderInterface
{
private $minLength = 3;
private $maxLength = 128;
public function getInputSpecification()
{
return array(
'name' => $this->getName(),
'required' => true,
'filters' => array(
array( 'name' => 'StringTrim' )
),
'validators' =>
array(
new NotEmpty(
array( 'mesages' =>
array(
NotEmpty::IS_EMPTY => 'The username you provided is blank.'
)
)
),
new AlNum( array(
'messages' => array( Alnum::STRING_EMPTY => 'The username can only contain letters and numbers.' )
)
),
new StringLength(
array(
'min' => $this->getMinLength(),
'max' => $this->getMaxLength(),
'messages' =>
array(
StringLength::TOO_LONG => 'The username is too long. It cannot be longer than ' . $this->getMaxLength() . ' characters.',
StringLength::TOO_SHORT => 'The username is too short. It cannot be shorter than ' . $this->getMinLength() . ' characters.',
StringLength::INVALID => 'The username is not valid.. It has to be between ' . $this->getMinLength() . ' and ' . $this->getMaxLength() . ' characters long.',
)
)
),
array(
'name' => '\My\Validator\UsernameNotInUse',
'options' => array(
'messages' => array(
UsernameNotInUse::ERROR_USERNAME_IN_USE => 'The usarname %value% is already being used by another user.'
)
)
)
)
);
}
}
Now here is my validator
namespace My\Validator;
use My\Entity\Helper\User as UserHelper,
My\EntityRepository\User as UserRepository;
use Zend\Validator\AbstractValidator,
Zend\ServiceManager\ServiceManagerAwareInterface,
Zend\ServiceManager\ServiceLocatorAwareInterface,
Zend\ServiceManager\ServiceManager;
/**
*
*/
class UsernameNotInUse
extends AbstractValidator
implements ServiceManagerAwareInterface
{
const ERROR_USERNAME_IN_USE = 'usernameUsed';
private $serviceManager;
/**
*
* #var UserHelper
*/
private $userHelper;
protected $messageTemplates = array(
UsernameNotInUse::ERROR_USERNAME_IN_USE => 'The username you specified is being used already.'
);
public function isValid( $value )
{
$inUse = $this->getUserHelper()->isUsernameInUse( $value );
if( $inUse )
{
$this->error( UsernameNotInUse::ERROR_USERNAME_IN_USE, $value );
}
return !$inUse;
}
public function setUserHelper( UserHelper $mapper )
{
$this->userHelper = $mapper;
return $this;
}
/**
* #return My\EntityRepository\User
*/
public function getUserHelper()
{
if( $this->userHelper == null )
{
$this->setUserHelper( $this->getServiceManager()->get( 'doctrine.entitymanager.orm_default' )->getObjectRepository( 'My\Entity\User') );
}
return $this->userHelper;
}
public function setServiceManager( ServiceManager $serviceManager )
{
echo get_class( $serviceManager );
echo var_dump( $serviceManager );
$this->serviceManager = $serviceManager;
return $this;
}
/**
*
* #return ServiceManager
*/
public function getServiceManager( )
{
return $this->serviceManager;
}
}
Why did this seem like a good idea to me?
It seemed like a good testability/re-use choice to make since I could re-use the elements separately across my application if need be.
I could unit test each Input generated by each element to make sure it correctly accepts/rejects input.
This is the example of my unit test for the element
public function testFactoryCreation()
{
$fac = new Factory();
$element = $fac->createElement( array(
'type' => '\My\Form\Registration\UsernameElement'
) );
/* #var $element \My\Form\Registration\UsernameElement */
$this->assertInstanceOf( '\My\Form\Registration\UsernameElement',
$element );
$input = $fac->getInputFilterFactory()->createInput( $element->getInputSpecification() );
$validators = $input->getValidatorChain()->getValidators();
/* #var $validators \Zend\Validator\ValidatorChain */
$expectedValidators = array(
'Zend\Validator\StringLength',
'Zend\Validator\NotEmpty',
'Zend\I18n\Validator\Alnum',
'My\Validator\UsernameNotInUse'
);
foreach( $validators as $validator )
{
$actualClass = get_class( $validator['instance'] );
$this->assertContains( $actualClass, $expectedValidators );
switch( $actualClass )
{
case 'My\Validator\UsernameNotInUse':
$helper = $validator['instance']->getUserHelper();
//HAVING A PROBLEM HERE
$this->assertNotNull( $helper );
break;
default:
break;
}
}
}
The problem I'm having is that the validator can't fetch the UserHelper properly, which is really a UserRepository from doctrine. The reason this is happening is because the validators only get access to the ValidatorPluginManager as a ServiceManager rather than having access to the application wide ServiceManager.
I get this error for the Validator portion, although if I call the same get method on the general service manager it works with no problems.
1) Test\My\Form\Registration\UsernameElementTest::testFactoryCreation
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for doctrine.entitymanager.orm_default
The var_dump( $serviceManager ) in validator shows me it is of the class ValidatorPluginManager.
I tried putting a factory in the service_manager entry like so
'service_manager' => array(
'factories' => array(
'My\Validator\UsernameNotInUse' => function( $sm )
{
$validator = new \My\Validator\UsernameNotInUse();
$em = $serviceManager->get( 'doctrine.entitymanager.orm_default' );
/* #var $em \Doctrine\ORM\EntityManager */
$validator->setUserHelper( $em->getRepository( '\My\Entity\User' ) );
return $validator;
}
)
but that didn't work because it's not consulting the application level service manager.
So, overall, here are my questions :
Is this strategy of separating the form and elements a good one? Should I keep going this way? What are alternatives? ( I'm for breaking stuff up for the sake of testability ) I was going to test ONLY the form itself originally with a combination of ALL the inputs but it seemed like I'd be trying to do too much.
How do I resolve the issue I have above?
Should I be using the Form/Element/Input parts of Zend in some other way that I'm not seeing?
this is my validator, using a static method to inject the entityManager and working with any doctine entity.
<?php
namespace Base\Validator;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Doctrine\ORM\EntityManager;
class EntityUnique extends AbstractValidator
{
const EXISTS = 'exists';
protected $messageTemplates = array(
self::EXISTS => "A %entity% record already exists with %attribute% %value%",
);
protected $messageVariables = array(
'entity' => '_entity',
'attribute' => '_attribute',
);
protected $_entity;
protected $_attribute;
protected $_exclude;
protected static $_entityManager;
public static function setEntityManager(EntityManager $em) {
self::$_entityManager = $em;
}
public function getEntityManager() {
if (!self::$_entityManager) {
throw new \Exception('No entitymanager present');
}
return self::$_entityManager;
}
public function __construct($options = null)
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($token);
}
if (is_array($options)) {
if (array_key_exists('entity', $options)) {
$this->_entity = $options['entity'];
}
if (array_key_exists('attribute', $options)) {
$this->_attribute = $options['attribute'];
}
if (array_key_exists('exclude', $options)) {
if (!is_array($options['exclude']) ||
!array_key_exists('attribute', $options['exclude']) ||
!array_key_exists('value', $options['exclude'])) {
throw new \Exception('exclude option must contain attribute and value keys');
}
$this->_exclude = $options['exclude'];
}
}
parent::__construct(is_array($options) ? $options : null);
}
public function isValid($value, $context = null)
{
$this->setValue($value);
$queryBuilder = $this->getEntityManager()
->createQueryBuilder()
->from($this->_entity, 'e')
->select('COUNT(e)')
->where('e.'. $this->_attribute . ' = :value')
->setParameter('value', $this->getValue());
if ($this->_exclude) {
$queryBuilder = $queryBuilder->andWhere('e.'. $this->_exclude['attribute'] . ' != :exclude')
->setParameter('exclude', $this->_exclude['value']);
}
$query = $queryBuilder->getQuery();
if ((integer)$query->getSingleScalarResult() !== 0) {
$this->error(self::EXISTS);
return false;
}
return true;
}
}
ie. i'm using it for theese form elements which are also tested and working fine:
<?php
namespace User\Form\Element;
use Zend\Form\Element\Text;
use Zend\InputFilter\InputProviderInterface;
class Username extends Text implements InputProviderInterface
{
public function __construct() {
parent::__construct('username');
$this->setLabel('Benutzername');
$this->setAttribute('id', 'username');
}
public function getInputSpecification() {
return array(
'name' => $this->getName(),
'required' => true,
'filters' => array(
array(
'name' => 'StringTrim'
),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
'isEmpty' => 'Bitte geben Sie einen Benutzernamen ein.',
),
),
),
),
);
}
}
When creating a new user
<?php
namespace User\Form\Element;
use Zend\InputFilter\InputProviderInterface;
use User\Form\Element\Username;
class CreateUsername extends Username implements InputProviderInterface
{
public function getInputSpecification() {
$spec = parent::getInputSpecification();
$spec['validators'][] = array(
'name' => 'Base\Validator\EntityUnique',
'options' => array(
'message' => 'Der name %value% ist bereits vergeben.',
'entity' => 'User\Entity\User',
'attribute' => 'username',
),
);
return $spec;
}
}
when editin an existing user
<?php
namespace User\Form\Element;
use Zend\InputFilter\InputProviderInterface;
use User\Form\Element\Username;
class EditUsername extends Username implements InputProviderInterface
{
protected $_userId;
public function __construct($userId) {
parent::__construct();
$this->_userId = (integer)$userId;
}
public function getInputSpecification() {
$spec = parent::getInputSpecification();
$spec['validators'][] = array(
'name' => 'Base\Validator\EntityUnique',
'options' => array(
'message' => 'Der name %value% ist bereits vergeben.',
'entity' => 'User\Entity\User',
'attribute' => 'username',
'exclude' => array(
'attribute' => 'id',
'value' => $this->_userId,
),
),
);
return $spec;
}
}

Categories