Related
I have got a fieldset implementing InputFilterProviderInterface, so it has the function getInputFilterSpecification. The __construct() function is adding a file element ('logo') like so:
$this->add(array(
'name' => 'logo',
'type' => 'file',
'attributes' => array(
'required' => false,
'class' => 'form-control',
'id' => 'logo',
),
'options' => array(
'label' => 'Company Logo: ',
),
));
In my getInputFilterSpecification function how do I add the RenameUpload input filter?
I have tried several variations of the following without success:
public function getInputFilterSpecification()
{
return array(
array(
'name' => 'logo',
'filterchain' => array(
'name' => 'filerenameupload',
'options' => array(
'target' => '/public/images/' . time(),
),
),
'required' => false,
),
//... other filters
}
}
How do I add the FileRenameUpload filter (Zend\Filter\File\RenameUpload)?
[edit]
I have changed the array to:
array(
'name' => 'logo',
'filters' => array(
array(
'name' => 'filerenameupload',
'options' => array(
'target' => '/public/images/' . time(),
),
),
),
'required' => false,
),
Which "appears" to be working, however I am now getting this message -
File 'C:\xampp\tmp\phpDA68.tmp' could not be renamed. An error
occurred while processing the file.
What errors could have occurred? How do I fix it?
This issue is being caused by specifying an invalid target. Prefixing it with getcwd() makes the path relative to the application root.
array(
'name' => 'logo',
'filters' => array(
array(
'name' => 'filerenameupload',
'options' => array(
'target' => getcwd() . '/public/images/' . time(),
),
),
),
'required' => false,
),
You must have created dir before use renameFile.
Example:
mkdir( $rootPath . '/public/images/' . time() );
I have this class:
<?php
class RegisterFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'email1',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'email2',
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'table' => 'user',
'field' => 'email',
'messages' => array(
'recordFound' => "Email already exist ... ! <br>",
),
),
),
),
));
}
}
?>
I get this error: No database adapter present. Any ideas why this happens?
It would be good if you'd read the documentation about Zend\Validator\Db\Record*. The given error message means exactly what it said. You don't provide a DB-Adapter inside the Validator.
From the DOCs:
$validator = new Zend\Validator\Db\RecordExists(
array(
'table' => 'users',
'field' => 'emailaddress',
'adapter' => $dbAdapter
)
);
If you want to find out how to get the DB-Adapter into your Form, i have written a Blog Article about said topic.
i have my application based in ZendSkeletonApplication , now i want create to relationship between my models so:
user portal
id id
firstName name
lastName url
........
portal_Id
I want to fill my select-option in the user form with database values
<?php
namespace Register\Form;
use Zend\Captcha\AdapterInterface as CaptchaAdapter;
use Zend\Form\Form;
use Zend\Form\Element;
class UserForm extends Form
{
protected $portalTable;
public function __construct($name = null)
{
parent::__construct('user');
$this->setAttribute('method', 'post');
$this->setAttribute('class', 'form-horizontal');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(array(
'type' => 'Select',
'name' => 'portal_id',
'options' => array(
'label' => 'Portal',
'empty_option' => 'Seleccione un portal',
'value_options' => array(
'1' => 'portal 1',
'2' => 'portal 2',
'3' => 'portal 3',
//i want option from database with
),
)
));
$this->add(array(
'name' => 'firstName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'First Name',
),
));
$this->add(array(
'name' => 'lastName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
$this->add(array(
'name' => 'login',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Login',
),
));
$this->add(array(
'name' => 'password',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'Password',
),
));
$this->add(array(
'name' => 'password_repeat',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'password (repeat)',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Email',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf',
'options' => array(
'csrf_options' => array(
'timeout' => 600
)
)
));
$this->add( array(
'type' => 'Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => array('class' => 'Dumb',
),
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
in this part i want fill select from database
$this->add(array(
'type' => 'Select',
'name' => 'portal_id',
'options' => array(
'label' => 'Portal',
'empty_option' => 'Seleccione un portal',
'value_options' => array(
'1' => 'portal 1',
'2' => 'portal 2',
'3' => 'portal 3',
//i want option from database with
),
)
));
sorry for my English
I have written an in-depth blog "Zend\Form\Element\Select and Database-Values" about this topic. Basically though, this is what you have to do:
Basically all you have to do is Query the Database inside your Form for the Data. For this you need the DB-Adapter to be available inside your Form, which is done by Dependency-Injection. Since the DB-Adapter is required for your Form to function correctly, i'd suggest Setter-Injection.
Inside your getServiceConfig() do this:
return array('factories' => array(
'namespace-form-formname' => function($sm) {
$dbA = $sm->get('Zend\Db\Adapter\Adapter');
$form = new \Namespace\Form\Formname($dbA);
return $form;
}
));
This will inject the Zend\Db\Adapter\Adapter into your form, which should already be valid though other configuration. Then you need to modify your form code a little bit:
public function __construct(\Zend\Db\Adapter\Adapter $dbA) {
parent::__construct('form-name');
// Do the DB-Query here. You got the DB-Adapter
// http://zf2.readthedocs.org/en/latest/modules/zend.db.adapter.html
$selectArray = array(
'key' => 'value',
'key' => 'value',
'key' => 'value',
); // obviously, this is just a fake-$selectArray demonstrating
// what the output of your Queries should be
// Add your Form Elements here
// use $selectArray as value_options of your desired select element
}
And that's basically it. Sadly i can't give you an concrete example, as i've never worked with Zend\Db, but i assume this will get you started.
PS: In your controller, call the form like this:
$form = $this->getServiceLocator()->get('namespace-form-formname');
Try:
// add code on controller
$arrPortalId = array();
$results = array('1' => 'portal 1', '2' => 'portal 2', '3' => 'portal 3',); // this part change your database value
foreach ($results as $key => $val) {
$arrPortalId[$key] = $va;
}
$dataParams['portalId'] = $arrPortalId;
$form = new UserForm($dataParams);
<?php
namespace Register\Form;
use Zend\Captcha\AdapterInterface as CaptchaAdapter;
use Zend\Form\Form;
use Zend\Form\Element;
class UserForm extends Form
{
protected $portalTable;
public function __construct($params = array())
{ $name = isset($params['name'])?$params['name']:'';
parent::__construct('user');
$this->setAttribute('method', 'post');
$this->setAttribute('class', 'form-horizontal');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$portalId = (isset($params['portalId']) && count($params['portalId']) > 0)?$params['portalId']:array();
$this->add(array(
'type' => 'Select',
'name' => 'portal_id',
'options' => array(
'label' => 'Portal',
'empty_option' => 'Seleccione un portal',
'value_options' => $portalId,
)
));
$this->add(array(
'name' => 'firstName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'First Name',
),
));
$this->add(array(
'name' => 'lastName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
$this->add(array(
'name' => 'login',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Login',
),
));
$this->add(array(
'name' => 'password',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'Password',
),
));
$this->add(array(
'name' => 'password_repeat',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'password (repeat)',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Email',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf',
'options' => array(
'csrf_options' => array(
'timeout' => 600
)
)
));
$this->add( array(
'type' => 'Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => array('class' => 'Dumb',
),
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
I was looking for show a navigation menu in the layout.phtml file:
<?php echo $this->navigation('navigation')->menu(); ?>
So, I added these lines to /module/Application/config/module.config.php in order to declare the menu's structure according to my app routes:
'navigation' => array(
'default' => array(
'place' => array(
'label' => 'Places',
'route' => '/place',
'pages' => array(
'country' => array(
'label' => 'Countries',
'route' => '/place/country',
),
'state' => array(
'label' => 'States',
'route' => '/place/state',
),
'city' => array(
'label' => 'Cities',
'route' => '/place/city',
),
),
),
),
),
and then I wrote a loader for the Zend/Navigation instance:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
but I get repeatedly this exception:
Fatal error: Zend\Mvc\Router\Exception\RuntimeException: Route with
name "" not found in
C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\View\Helper\Navigation\AbstractHelper.php
on line 471
I attempted to solve the problem adding this line to the menu declaration:
'pages' => array(
'route' => '/place',
'country' => array(
'label' => 'Countries',
'route' => '/place/country',
),
Nevertheless, In that case I get another different exception:
Fatal error: Uncaught exception
'Zend\Navigation\Exception\InvalidArgumentException' with message
'Invalid argument: $page must be an instance of
Zend\Navigation\Page\AbstractPage or Traversable, or an array' in
C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php
on line 733
UPDATED: Following Jurian Sluiman's suggestion,
This must be my router configuration (module\Place\config\module.config.php), if I've got a module called 'Place', with 3 controllers (city, country and state):
'router' => array(
'routes' => array(
'city' => array(
'type' => 'segment',
'options' => array(
'route' => '/city[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\City',
'action' => 'index',
),
),
),
'country' => array(
'type' => 'segment',
'options' => array(
'route' => '/country[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\Country',
'action' => 'index',
),
),
),
'state' => array(
'type' => 'segment',
'options' => array(
'route' => '/state[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\State',
'action' => 'index',
),
),
),
),
),
And this must be my navigation config (actually I located this one in an autoloadable file in /config/autoload/menu.global.php):
return array(
'navigation' => array(
'default' => array(
'place' => array(
'label' => 'Places',
'route' => 'country',
'pages' => array(
'country' => array(
'label' => 'Countries',
'route' => 'country',
),
'state' => array(
'label' => 'States',
'route' => 'state',
),
'city' => array(
'label' => 'Cities',
'route' => 'city',
),
),
),
),
),
);
...And now It works!.
Usually routes start not with a /. The / is a delimiter for route names, so when the /place route is assembled, you have a parent "" and then a child "place". The first one is empty, giving you this error.
Perhaps you confused route names with the route itself. You have to provide the name of the route, so the route itself (the URL) will be returned. So imagine you have this route config:
'router' => array(
'routes' => array(
'place' => array(
'type' => 'literal',
'options' => array(
'route' => '/place',
'defaults' => array(
// Some defaults here
),
),
),
),
),
In this case, your route name is place and not /place. If you have a different route config then the one here above, put the route config in your question so I can update my answer.
Currently, I have this select code in ZF2. Works great, have no issues, BUT! I want to put an id or class attribute into one of the options. Is that possible currently in ZF2?
$this->add(
array(
'name' => 'number_of_rooms',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Number of Rooms',
'options' => array(
array(
'value' => '',
'label' => '- number of rooms -',
'disabled' => true,
),
array(
'value' => 'one bedrooms,
'label' => '1 Bedrooms',
),
array(
'value' => 'two bedrooms',
'label' => '2 Bedrooms',
),
array(
'value' => 'three bedrooms',
'label' => '3 Bedrooms',
),
),
),
'attributes' => array(
'id' => 'number_of_rooms',
),
)
);
You would need to do two things. First would be to write your own SelectElement extending Zend\Form\Element\Select and then you'd need to also write your own ViewHelper rendering the Select, easiest extending Zend\Form\View\Helper\FormSelect
With the inbuilt Select the only valid options are:
//FormSelect.php #43-48
protected $validOptionAttributes = array(
'disabled' => true,
'selected' => true,
'label' => true,
'value' => true,
);
You can check this from the classes linked above.