sfValidator Choice is invalid symfony 1.4 - php

i'm writing an form code with datas of another form. I'm getting an error from the validators with "choices", the erros says: dia_semana [Invalid.] id_programa [Invalid.] but i don't know how to solve this.
This is my code:
public function configure()
{
$this->setWidgets(array(
$this->validatorSchema->setOption('allow_extra_fields', true);
$this->validatorSchema->setOption('filter_extra_fields', true);
$this->setWidgets(array(
'dia_semana' => new sfWidgetFormChoice(array('label' => 'Data da Semana','choices' => array("" => "", "segunda" => "Segunda-Feira","terca" => "Terca-Feira"))),
'id_programa' => new sfWidgetFormChoice(array('label' => 'Programa',
'choices' => Doctrine_Core::getTable('tbprogramas_tv')->getProgramas())),
));
$this->setValidators(array(
'dia_semana' => new sfValidatorChoice(array(
'choices' => array("" => "","segunda" => "Segunda-Feira", "terca" => "Terca-Feira"), 'required' => false)),
'id_programa' => new sfValidatorChoice(array(
'choices' => array_keys(Doctrine_Core::getTable('tbprogramas_tv')->getProgramas()),
'required' => false)),
));
}
Someone can help me?

sfValidatorChoice uses only values of array as valid values, so instead of ['key' => 'value'] pairs you have to provide only ['key']:
public function configure()
{
$this->setWidgets(array(
$this->validatorSchema->setOption('allow_extra_fields', true);
$this->validatorSchema->setOption('filter_extra_fields', true);
$dias = array("" => "", "segunda" => "Segunda-Feira","terca" => "Terca-Feira");
$programas = Doctrine_Core::getTable('tbprogramas_tv')->getProgramas();
$this->setWidgets(array(
'dia_semana' => new sfWidgetFormChoice(array('label' => 'Data da Semana','choices' => $dias)),
'id_programa' => new sfWidgetFormChoice(array('label' => 'Programa',
'choices' => $programas)),
));
$this->setValidators(array(
'dia_semana' => new sfValidatorChoice(array(
'choices' => array_keys($dias), 'required' => false)),
'id_programa' => new sfValidatorChoice(array(
'choices' => array_keys($programas)),
'required' => false)),
));
}

Related

Elastica add documents overrides existing ones

When i try to add new documents to an index type , i loose existing documents which are overwritten by the new added ones . The problem can be related to the id of each added document ??
Here is the code :
$elasticaClient = new \Elastica\Client(array(
'host' => $this->container->getParameter('elastic_host'),
'port' => $this->container->getParameter('elastic_port')
));
$elasticaIndex = $elasticaClient->getIndex('app');
$elasticaIndex->create(
array(
'number_of_shards' => 4,
'number_of_replicas' => 1,
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('lowercase', 'mySnowball')
),
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('standard', 'lowercase', 'mySnowball')
)
),
'filter' => array(
'mySnowball' => array(
'type' => 'snowball',
'language' => 'German'
)
)
)
),
true
);
$elasticaType = $elasticaIndex->getType('type');
$mapping = new \Elastica\Type\Mapping();
$mapping->setType($elasticaType);
$mapping->setParam('index_analyzer', 'indexAnalyzer');
$mapping->setParam('search_analyzer', 'searchAnalyzer');
$mapping->setProperties(array(
'id' => array('type' => 'string'),
'title' => array('type' => 'string'),
'duration' => array('type' => 'string'),
'start' => array('type' => 'string'),
'end' => array('type' => 'string'),
));
// Send mapping to type
$mapping->send();
$documents = array();
foreach($medias as $media) {
$id = uniqid() ;
$documents[] = new \Elastica\Document(
$id,
array(
'id' => $id,
'title' => $media['title'],
'duration' => $media['duration'],
'start' => $media['start'],
'end' => $media['end'],
)
);
}
$elasticaType->addDocuments($documents);
$elasticaType->getIndex()->refresh();
Please i need your help . Thank you
PHP does not recommend using uniqid for this use case. Since you are wanting a random, safe id, let Elasticsearch do it for you. The Elastica Document construct method notes that the id field is optional. So don't pass it and let Elasticsearch issue the id.
Several things
$elasticaIndex->create (....) you only have to enter it once. Index is unique after creating the index that you can comment or generate a different index and other things. I leave an example that works.
class PersistencyElastic
{
private $conection;
public function __construct()
{
$this->conection = new \Elastica\Client(['host' => '127.0.0.1', 'port' => 9200]);
}
public function save($ msg)
{
// $ msg is an array with whatever you want inside
$index = $this->conection->getIndex('googlephotos');
// This is the index I created, it's called googlephotos
// $index->create(array (), true);
$type = $index->getType('googlephotos');
$type->addDocument(new Document (uniqid ('id _', false), $msg, $type, $index));
$index->refresh();
}
}

ZF2 form validation is empty validations always fails

Hiho,
i have a problem with a ZF2 Form.
Every time I submit it I got the following error:
(result of $form->isValid() and var_dump($form->getMessages());
array (size=1)
'imagecode' =>
array (size=1)
'isEmpty' => string 'Value is required and can't be empty' (length=36)
The following is the 'imagecode' - formfieldcode:
public function __construct($name = null)
{
parent::__construct('advert');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'imagecode',
'type' => 'Zend\Form\Element\Textarea',
'attributes' => array(
'required' => 'required',
),
'options' => array(
'label' => 'Bannercode:'
),
));
And the Validator:
public function getInputFilter()
{
if (!$this->_inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
...
$inputFilter->add($factory->createInput(array(
'name' => 'imagecode',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => '3',
'max' => '5000',
),
),
),
)));
$this->_inputFilter = $inputFilter;
}
return $this->_inputFilter;
}
The other fields working correct and are correct validated but not the Textarea.
At last the ControllerCode:
$advert = $service->getAdvertById($id);
$form = $service->getAdvertForm();
$request = $this->getRequest();
$form->bind($advert);
if ($request->isPost()) {
$filter = new AdvertFilter();
$form->setData($request->getPost());
$form->setInputFilter($filter->getInputFilter());
After this the validation is failing and I don't know why.
I hope that any one can help me.
Look this line :
'required' => 'required,
and....
change to:
'required' => 'required',
I have figured it out.
I trying to input html code so that the
array('name' => 'StripTags')
removed it.
Sometimes it is so simple ._.'

YiiBooster. TbEditableColumn. Property "attribute" should be defined

I have a problem with TbEditedableColumn in YiiBooster 4.0.1
View:
$this->widget(
'application.extensions.booster.widgets.TbGridView',
array(
'type' => 'striped bordered',
'dataProvider' => new CActiveDataProvider('Stats'),
'columns' => array(
'pid',
array(
'class' => 'application.extensions.booster.widgets.TbEditableColumn',
'name' => 'login',
'sortable' => false,
'editable' => array(
//'model' => $model,
//'attribute' => 'login',
'url' => $this->createUrl('stats/editableSaver'),
'placement' => 'right',
'inputclass' => 'span3'
)
)
),
)
);
Controller:
public function actionEditableSaver()
{
Yii::import('application.extensions.booster.components.TbEditableSaver');
$es = new TbEditableSaver('Stats');
$es->update();
}
When I try to save the edited fields, I got this exception: Property "attribute" should be defined.
$es->attributes is empty.
How to fix that? Thanks.
From the source code, TbEditableSaver::update() obtains the attribute from a post or get parameter name:
$this->attribute = yii::app()->request->getParam('name');
$this->value = yii::app()->request->getParam('value');
//checking params
if (empty($this->attribute)) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
}
In order for this parameter to be sent in the update request it needs to be defined in the editable array. To fix this:
'class' => 'application.extensions.booster.widgets.TbEditableColumn',
'name' => 'login',
'sortable' => false,
'editable' => array(
'name' => 'login',
'url' => $this->createUrl('stats/editableSaver'),
'placement' => 'right',
'inputclass' => 'span3'
)

Populate values in Zend Form multi-select option

So my form is
class Form_Group_ShareGroup extends Form_Abstract
{
public function init()
{
$this->setAttrib('class', 'shareForm');
//Row 1
$this->addElement('select', 'groups', array(
'decorators' => $this->getElementDecorators(),
'label' => Zend_Registry::get('Zend_Translate')->translate('Groups_Colon'),
'style' => 'width:320px;',
'multiple' => true,
'data-help' => Zend_Registry::get('Zend_Translate')->translate('Please_Select_Groups_Share')
));
$this->addElement('select', 'users', array(
'decorators' => $this->getElementDecorators(),
'label' => Zend_Registry::get('Zend_Translate')->translate('Users_Colon'),
'style' => 'width:320px;',
'multiple' => true,
'data-help' => Zend_Registry::get('Zend_Translate')->translate('Select_Users_To_Share_Group_With')
));
//Row 2
$this->addElement('button', 'share', array(
'decorators' => $this->buttonDecorators,
'required' => false,
'ignore' => true,
'type' => 'submit',
'label' => 'Share',
'class' => 'btn primary'
));
}
}
and I want to populate some user data into "users" element.
example could be :
array('10000105' => 'aamaddy1 m aamaddy1','10000106' => 'aamaddy2 m aamaddy2' )
How can I achieve this ?
I have use populate method but it didnt worked out.
to view the form I am using :
$usersGroup = new Form_Group_ShareGroup();
$this->view->usersGroup = $usersGroup;
Please help !
Thanks for reading .
Try this:
//$data = array('10000105' => 'aamaddy1 m aamaddy1','10000106' => 'aamaddy2 m aamaddy2' );
$usersGroup = new Form_Group_ShareGroup();
$usersGroup->getElement('users')->setMultiOptions($data);

How to check if an email already registered in form

I am writing a Registration code and i am looking for email uniqueness. If user enters a email that is already registered, the form should thrown error.
I have try both NoRecordExists, and RecordExists. None is working here
I have implemented "Db\NoRecordExists", but it is not working. It is not throwing error nor it is checking email in Db.
My controller is like
$sm = $this->getServiceLocator();
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
if ($request->isPost()) {
#Filter the Input
$form->setInputFilter(new RegisterStepFirstFilter($dbAdapter));
$form->setData($request->getPost());
if (!$form->isValid()) {
#error comes
$this->flashMessenger()->addMessage('Oops an error is occured');
}else{
#No error.Proceed with Registration
}
}
My Registration Filter Class is like
namespace User\Form;
use Zend\InputFilter\InputFilter;
class RegisterStepFirstFilter extends InputFilter
{
private $dbAdapter;
public function __construct($dbAdapter) {
$this->add(array(
'name' => 'user_email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
array('name' => 'HtmlEntities'),
),
'validators' => array(
array('name' => 'EmailAddress'),
array('name' => 'StringLength', 'options' => array('encoding' => 'UTF-8', 'min' => 1, 'max' => 200,),
array('name' => 'Db\NoRecordExists', 'options' => array('table' => 'y2m_user','field' => 'user_email', 'adapter' => $dbAdapter),),
),
),
));
}
}}
The Registration form works perfectly with all other Validators. But it is not checking Email uniqueness not it is throwing any error(I have already enable the error)
Any suggestion will be appreciated.
The Issue was because of Type.I forgot to put a Comma after in a filter .Strange, zf2 was not given any error
I have update validator
'validators' => array(
array('name' => 'EmailAddress'),
array('name' => 'StringLength', 'options' => array('encoding' => 'UTF-8', 'min' => 1, 'max' => 200,),),
array('name' => 'Db\NoRecordExists', 'options' => array('table' => 'y2m_user','field' => 'user_email', 'adapter' => $dbAdapter),),
),
You use the wrong validator, you need to use Db\NoRecordExists.

Categories