Cakephp 3 make a search form - search-engine

I'm a beginner in cakephp and I want to make a search form with "Search Box", "Select" and "Radio Buttons", I found some plugins, but I get lost when I try to implement it, I hope you can help me with some plug-ins and tutorials.
Edit
Form:
<div>
<?= $this->Form->create($multimedia, array('controller' => 'multimedia', 'action' => 'search', 'type' => 'get')); ?>
<?= $this->Form->input('category_id', ['class' => 'electdown',
'label' => false,
'empty' => 'Categories',
'options' => $categories
]); ?>
<?= $this->Form->input('buscador', array('placeholder' => 'Search...', 'label' => false)); ?>
<?= $this->Form->submit('login_BDP_logo.png', array('type' => 'button', 'id' => 'buscar', 'div' => false)); ?>
<?= $this->Form->end(); ?>
</div>
MultimediaController:
public function search(){
$multimedia = array();
if (!empty($this->params['Multimedia'])) {
$query = $this->params['query']['buscador'];
$query = $this->params['query']['category_id'];
$conditions = array(
'conditions' => array(
'or' => array(
'Multimedia.title LIKE' => "%$query%",
'Multimedia.description LIKE' => "%$query%",
'Multimedia.mime_type LIKE' => "%$query%",
'Multimedia.filename LIKE' => "%$query%",
'Multimedia.url LIKE' => "%$query%",
'Multimedia.language_id LIKE' => "%$query%",
'Multimedia.category_id LIKE' => "%category_id%",
'Multimedia.created_by LIKE' => "%$query%"
)
)
);
$multimedia = $this->Multimedia->find('all', $conditions);
}
$this->set('negocios', $negocios);
debug($this->params);
}
Update:
This woks:
public function search(){
$multimedia = array();
if (!empty($this->request->query)) {
$query = $this->request->query['search'];
$category_id = $this->request->query['category_id'];
//$language_id = $this->request->query['language_id'];
$conditions = array(
'conditions' => array(
'and' => array()));
if ($query) $conditions['conditions']['and']['Multimedia.title LIKE'] = "%$query%";
if ($category_id) $conditions['conditions']['and']['Multimedia.category_id'] = $category_id ;
//if ($language_id) $conditions['conditions']['and']['Multimedia.language_id'] = $language_id ;
/*
'Multimedia.title LIKE' => "%$query%",
// 'Multimedia.description LIKE' => "%$query%",
// 'Multimedia.mime_type LIKE' => "%$query%",
// 'Multimedia.filename LIKE' => "%$query%",
// 'Multimedia.url LIKE' => "%$query%",
// 'Multimedia.language_id LIKE' => "%$query%",
'Multimedia.category_id' => $category_id
// 'Multimedia.created_by LIKE' => "%$query%"
)
)
); */
$multimedia = $this->Multimedia->find('all', $conditions);
$this->set('multimedia', $multimedia);
$languages = $this->Multimedia->Languages->find('list', ['limit' => 200]);
$categories = $this->Multimedia->Categories->find('list', ['limit' => 200]);
$users = $this->Multimedia->Users->find('list', ['limit' => 200]);
$this->set(compact('multimedia', 'languages', 'categories', 'users'));
$this->set('_serialize', ['multimedia']);
}
}

Related

Adding Form field dynamically in helper form Prestashop

I have a back office admin controller in which I am using the helper form in renderform() to generate a form and it is working form but I want a functionality to add a field in that form dynamically.
I have done this by helper list, while rendering the form I am returning a render list also, check renderAdditionalOptionsList() usage
public function renderForm() {
$id_citydelivery = (int) Tools::getValue('id_citydelivery');
$citydeliveryObj = new CityPortModel($id_citydelivery);
if (Validate::isLoadedObject($citydeliveryObj)) {
$_legends = $citydeliveryObj->legend;
$_legend_array = explode(',', $_legends);
foreach ($_legend_array as $_legend) {
$this->fields_value['legend_' . $_legend] = $_legend;
}
}
$temp = $this->fields_value;
$fields_form_1 = array(
'form' => array(
'legend' => array('title' => $this->l('Price and Delivery Destination Manager'), 'icon' => 'icon-cogs'),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Country'),
'name' => 'id_country',
'required' => false,
'lang' => false,
'options' => array(
'query' => $this->getCountries(),
'id' => 'value',
'name' => 'name'
)),
array(
'type' => 'text',
'label' => $this->l('Portname'),
'name' => 'portname',
'size' => 255),
array(
'type' => 'hidden',
'name' => 'id_citydelivery'
),
array(
'type' => 'text',
'label' => $this->l('Per m3'),
'name' => 'perm3',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Service charges'),
'name' => 'servicecharges',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Insurance'),
'name' => 'insurance',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Inspection'),
'name' => 'inspection',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Certificate'),
'name' => 'certificate',
'size' => 255),
array(
'type' => 'checkbox',
'label' => $this->l('Service legend'),
'name' => 'legend',
'required' => false,
'lang' => false,
'values' => array(
'query' => $this->getLegends(),
'id' => 'value',
'name' => 'name'
)),
array(
'type' => 'switch',
'label' => $this->l('Active'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'values' => array(array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Active')), array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Inactive')))),
),
'submit' => array(
'title' => $this->l('Save'),
'type' => 'submit'
),
'buttons' => array(
array(
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite($this->name),
'title' => $this->l('Cancle'),
'icon' => 'process-icon-cancel'
)
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->toolbar_scroll = true;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitAddcitydelivery';
$helper->currentIndex = AdminController::$currentIndex;
$helper->token = Tools::getAdminTokenLite($this->name);
$helper->tpl_vars = array(
'fields_value' => $this->getFormValues($citydeliveryObj, $temp),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
$_1 = $helper->generateForm(array($fields_form_1));
$return = $_1;
if ($this->display != 'add') {
$return .= $this->renderAdditionalOptionsList($id_citydelivery);
}
return $return;
}
public function initPageHeaderToolbar() {
if ($this->display == 'edit' || $this->display == 'add') {
$this->page_header_toolbar_btn['back_to_list'] = array(
'href' => Context::getContext()->link->getAdminLink('AdminGCardeliverycity', true),
'desc' => $this->l('Back to list', null, null, false),
'icon' => 'process-icon-back'
);
}
parent::initPageHeaderToolbar();
}
function getFormValues($citydeliveryObj, $temp) {
$arrTwo = array(
'id_citydelivery' => Tools::getValue('id_citydelivery', $citydeliveryObj->id_citydelivery),
'id_country' => Tools::getValue('id_country', $citydeliveryObj->id_country),
'portname' => Tools::getValue('portname', $citydeliveryObj->portname),
'perm3' => Tools::getValue('perm3', $citydeliveryObj->perm3),
'servicecharges' => Tools::getValue('servicecharges', $citydeliveryObj->servicecharges),
'insurance' => Tools::getValue('insurance', $citydeliveryObj->insurance),
'inspection' => Tools::getValue('inspection', $citydeliveryObj->inspection),
'certificate' => Tools::getValue('certificate', $citydeliveryObj->certificate),
'active' => Tools::getValue('active', $citydeliveryObj->active),
);
return array_merge($temp, $arrTwo);
}
protected function renderAdditionalOptionsList($id_citydelivery) {
$this->fields_list = array(
'service_name' => array(
'width' => 'auto',
'orderby' => false,
'title' => $this->l('Service Name'),
'type' => 'text',
'search' => false,
),
'service_desc' => array(
'type' => 'text',
'title' => $this->l('Service Description'),
'search' => false,
'orderby' => false,
),
'charge' => array(
'title' => $this->l('Service Charge'),
'type' => 'text',
'search' => false,
'orderby' => false,
),
'active' => array(
'title' => $this->l('Status'),
'active' => 'status',
'type' => 'bool',
'search' => false,
'orderby' => false
)
);
$linkToAdditionServiceController = Context::getContext()->link->getAdminLink('AdminAdditionalService', false);
$helperList = new HelperList();
$helperList->table = 'additional_service';
$helperList->shopLinkType = '';
$helperList->simple_header = false; //For showing add and refresh button
$helperList->identifier = 'id_additional_service';
$helperList->actions = array('edit', 'delete');
$helperList->show_toolbar = false;
$helperList->toolbar_btn['new'] = array(
'href' => $linkToAdditionServiceController . '&addadditional_service&id_citydelivery=' . $id_citydelivery . '&token=' . Tools::getAdminTokenLite('AdminAdditionalService'),
'desc' => $this->l('Add new')
);
$helperList->title = "Additional Option Manager";
$helperList->currentIndex = $linkToAdditionServiceController;
$helperList->token = Tools::getAdminTokenLite('AdminAdditionalService');
$content = $this->getListContent($id_citydelivery);
$helperList->listTotal = count($content);
return $helperList->generateList($content, $this->fields_list);
}

Change password of logged in user in zf2

I am new to Zf2. I have user and admin module. When user is logged in I made profile in admin where he change his profile and also password.
Now I am working in change password, but I cannot understand, how to code for change password. My code is given below:
My ProfileController is:
<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Profile;
use Admin\Form\ProfileForm;
class ProfileController extends AbstractActionController
{
protected $profileTable;
public function getAuthService()
{
$this->authservice = $this->getServiceLocator()->get('AuthService');
return $this->authservice;
}
public function indexAction()
{
$this->layout("layout/layout_admin");
/* this is used to show user email of logined user. */
$user_email = $this->getAuthService()->getStorage()->read();
// below condition is new and for back browser validation
if (!$this->getServiceLocator()
->get('AuthService')->hasIdentity()) {
return $this->plugin(redirect)->toRoute('login', array('action' => 'index'));
}
return new ViewModel(array(
'admin_profile' => $this->getProfileTable()->fetchAll($user_email),
));
}
public function editAction()
{
$this->layout("layout/layout_admin");
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('login', array(
'action' => 'index'
));
}
// Get the Profile with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$profile = $this->getProfileTable()->getProfile($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('profile', array(
'action' => 'index'
));
}
$form = new ProfileForm();
$form->bind($profile);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($profile->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getProfileTable()->saveProfile($profile);
// Redirect to profile
return $this->redirect()->toRoute('profile');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
public function changepasswordAction()
{
$this->layout("layout/layout_admin");
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('login', array(
'action' => 'index'
));
}
// Get the Profile with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$profile = $this->getProfileTable()->getProfile($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('profile', array(
'action' => 'index'
));
}
$form = new ProfileForm();
$form->bind($profile);
$form->get('submit')->setAttribute('value', 'Change');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($profile->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
echo "form is valid now save the data";
}else echo "form is invalid, don't save data";
}
return array(
'id' => $id,
'form' => $form,
);
}
public function getProfileTable()
{
if (!$this->profileTable) {
$sm = $this->getServiceLocator();
$this->profileTable = $sm->get('Admin\Model\ProfileTable');
}
return $this->profileTable;
}
}
ProfileForm.php:
<?php
namespace Admin\Form;
use Zend\Form\Form;
class ProfileForm extends Form
{
public function __construct($name = null)
{
parent::__construct('profile');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'required' => 'required',
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'text',
'required' => 'required',
),
'options' => array(
'label' => 'Email',
),
));
$this->add(array(
'name' => 'old_password',
'attributes' => array(
'type' => 'password',
'required' => 'required',
),
'options' => array(
'label' => 'Old Password ',
),
));
$this->add(array(
'name' => 'new_password',
'attributes' => array(
'type' => 'password',
'required' => 'required',
),
'options' => array(
'label' => 'New Password ',
),
));
$this->add(array(
'name' => 'confirm_password',
'attributes' => array(
'type' => 'password',
'required' => 'required',
),
'options' => array(
'label' => 'Confirm Password ',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
Model/Profile.php:
<?php
namespace Admin\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Profile implements InputFilterAwareInterface
{
public $id;
public $name;
public $email;
public $password;
protected $inputFilter;
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->name = (!empty($data['name'])) ? $data['name'] : null;
$this->email = (!empty($data['email'])) ? $data['email'] : null;
$this->password = (!empty($data['password'])) ? $data['password'] : null;
}
public function __set($name, $value) {
$method = 'set' . $name;
if (!method_exists($this, $method)) {
throw new Exception('Invalid Method');
}
$this->$method($value);
}
public function __get($name) {
$method = 'get' . $name;
if (!method_exists($this, $method)) {
throw new Exception('Invalid Method');
}
return $this->$method();
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->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' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Model/ProfileTable.php:
<?php
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\Adapter;
class ProfileTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll($user_email)
{
$resultSet = $this->tableGateway->select(array('email' =>$user_email));
return $resultSet;
}
public function getProfile($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current('id');
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveProfile(Profile $profile)
{
$data = array(
'name' => $profile->name,
'email' => $profile->email,
);
$id = (int) $profile->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getProfile($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Profile id does not exist');
}
}
}
}
view/changepassword.phtml:
<div class="jumbotron">
<?php
$title = 'Change your password Here:';
$this->headTitle($title);
?>
<h2 align="center"><?php echo $this->escapeHtml($title); ?></h2>
<div align="right">
View Profile
</div>
<br/>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('profile',
array(
'action' => 'changepassword',
'id' => $this->id,
)
));
$form->prepare();
?>
<div align="center">
<?php
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('old_password'));
echo "<br/>";
echo $this->formRow($form->get('new_password'));
echo "<br/>";
echo $this->formRow($form->get('confirm_password'));
echo "<br/>";
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
Cancel
</div>
</div>
I complete this with myself. Answer and best solution for change password is as follow:
changepassword.phtml:
<div class="jumbotron">
<h2 align="center">Change Your Password Here:</h2>
<div align="right">
View Profile
</div>
<br/>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('profile',
array(
'action' => 'changepassword',
'id' => $this->id,
)
));
$form->prepare();
?>
<?php echo $error; ?>
<div align="center">
<?php
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('old_password'));
echo "<br/>";
echo $this->formRow($form->get('new_password'));
echo "<br/>";
echo $this->formRow($form->get('confirm_password'));
echo "<br/>";
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
Cancel
</div>
</div>
Add this code in Profile.php
public function getInputFilter2()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'old_password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'new_password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'confirm_password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
Add below function in ProfileTable.php
public function updatePassword($password,Profile $profile)
{
$data = array(
'password' => md5($password),
);
$id = (int) $profile->id;
if ($this->getProfile($id))
{
$this->tableGateway->update($data, array('id' => $id));
}
else
{
throw new \Exception('Profile id does not exist');
}
}
Add the following code in ProfileController.php
public function changepasswordAction()
{
$this->layout("layout/layout_admin");
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('login', array(
'action' => 'index'
));
}
// below condition is new and for back browser validation
if (!$this->getServiceLocator()
->get('AuthService')->hasIdentity()) {
return $this->plugin(redirect)->toRoute('login', array('action' => 'index'));
}
$error = "";
$password="";
// Get the Profile with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$profile = $this->getProfileTable()->getProfile($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('profile', array(
'action' => 'index'
));
}
$form = new ProfileForm();
$form->bind($profile);
$form->get('submit')->setAttribute('value', 'Change');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($profile->getInputFilter2());
$form->setData($request->getPost());
if(($profile->password == md5($this->params()->fromPost('old_password')))AND($form->isValid()))
{
if($this->params()->fromPost('new_password') == $this->params()->fromPost('confirm_password'))
{
$password=$this->params()->fromPost('confirm_password');
$this->getProfileTable()->updatePassword($password,$profile);
//Redirect to profile
return $this->redirect()->toRoute('profile');
}
else
{
$error="<b>Error: </b>New Password and Confirm password didn't match";
}
}
else
{
$error="<b>Error: </b>Old Password didn't match";
}
}
return array(
'id' => $id,
'form' => $form,
'error' => $error,
);
}

Zend 2 form validaton password and password-retype

If I fill in only password or only password-retype then it passes the validation.
If I fill in password and password-retype and they differ then it does not pass the validation.
What do i do wrong?!
$inputFilter->add(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 6,
'max' => 128,
),
),
),
));
$inputFilter->add(array(
'name' => 'retype-password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 6,
'max' => 128
),
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'password'
)
)),
));
Form:
$form = $this->form;
$form->setAttribute('action', $this->url(
'users_update',
array(
'action' => 'edit',
'id' => $this->id,
)
));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('email'));
echo $this->formRow($form->get('password'));
echo $this->formRow($form->get('retype-password'));
echo '<br />';
echo $this->formSubmit($form->get('submit'));
echo '<br />';echo '<br />';
echo $this->form()->closeTag();
update method:
public function updateAction() {
$form = new UsersForm();
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('admin');
}
$user = $this->getUsersTable()->getUser( $id );
$request = $this->getRequest();
if ( $request->isPost()) {
if( $request->getPost('password') == '' || $request->getPost('retype-password') == '' ){
$request->getPost()->set('password' , $user->password);
$request->getPost()->set('retype-password' , $user->password);
}
$users = new Users();
$form->setInputFilter($users->getInputFilter());
$form->setData($request->getPost());
$form->isValid();
if ($form->isValid()) {
$users->exchangeArray($form->getData());
$this->getUsersTable()->saveUser($users , $id);
return $this->redirect()->toRoute('users_list');
}
}
$form->bind($user);
$form->get('submit')->setValue('update');
return array(
'id' => $id,
'form' => $form
);
}
So there you have it. This piece
if ($request->getPost('password') == '' || $request->getPost('retype-password') == '' )
{
$request->getPost()->set('password' , $user->password);
$request->getPost()->set('retype-password' , $user->password);
}
causes the symptom. You should have && instead of ||.

select a SF2 choice option

My code for my form ( i am using Silex):
$test = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
$form = $app['form.factory']->createBuilder('form')
->add('title','text',array(
'attr' => array(
'placeholder' => 'Title of your Album'
)))
->add('description','textarea',array(
'attr' => array(
'placeholder' => 'Describe your Album'
)))
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...'
),
))
The choices are defined as an multi-array, so I get <option> with <optgropup>. How can I enable in SF2, that some options are selected?
Use the data option:
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...',
'data' => $selected_value
),
where $selected_value can be a single value like 'value_1' or an simple array with multiple values array('value_1', 'value_2') to select.

Validating Zend\Form\Element\Collection

I have a form which has 'rows' added dynamically using Zend\Form\Element\Collection. This works fine, but I am struggling to add the validation for these rows.
So far my code looks something like the following. I presume I need to pass something to InputFilter\InputFilter::add() but I can't figure out what:
<?php
class EditForm extends \Zend\Form\Form
{
public function __construct()
{
parent::__construct('edit');
$this->setUpFormElements();
$this->setupInputFilters();
}
protected function setUpFormElements()
{
$fieldset = new \Zend\Form\Fieldset;
$nameElement = new \Zend\Form\Element\Text('name');
$fieldset->add($nameElement);
$descriptionElement = new \Zend\Form\Element\Text('description');
$fieldset->add($description);
$this->add(
array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'rows',
'options' => array(
'label' => 'Edit Rows',
'should_create_template' => true,
'allow_add' => true,
'target_element' => $fieldset,
)
)
);
return $this;
}
public function setupInputFilters()
{
$filter = new \Zend\InputFilter\InputFilter();
$filter->add(
array(
'name' => 'rows',
'required' => true,
'validators' => array(
// Not sure what to do here!
)
)
);
return $this;
}
}
I think you need to add the input filter to the getInputFilterSpecification method of the fieldset you are adding dynamically
class Row extends Fieldset implements InputFilterProviderInterface
{
$this->add(array(
'name' => 'yourFieldset',
'options' => array(
'label' => 'One of your fieldset elements'
),
'attributes' => array(
'required' => 'required'
)
));
$this->add(array(
'name' => 'fields',
'options' => array(
'label' => 'Another fieldset element'
),
'attributes' => array(
'required' => 'required'
)
));
public function getInputFilterSpecification()
{
return array(
'yourFieldset' => array(
'required' => true,
),
'fields' => array(
'required' => true,
'validators' => array(
array(
'name' => 'Float'
)
)
)
);
}
then in your form you need to set the validation group
class EditForm extends Form
{
public function __construct()
{
$this->add(
array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'rows',
'options' => array(
'label' => 'Edit Rows',
'should_create_template' => true,
'allow_add' => true,
'target_element' => $fieldset,
)
)
);
$this->setValidationGroup(array(
'csrf',
'row' => array(
'yourFieldset',
'fields',
)
));
}
}

Resources