本文整理汇总了PHP中Zend\Form\Form类的典型用法代码示例。如果您正苦于以下问题:PHP Form类的具体用法?PHP Form怎么用?PHP Form使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Form类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: loginAction
/**
* Login form
*/
public function loginAction()
{
if ($this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute($this->options->getLoginRedirectRoute());
}
$request = $this->getRequest();
$post = $request->getPost();
$form = $this->loginForm;
$fm = $this->flashMessenger()->setNamespace('zfcuser-login-form')->getMessages();
if (isset($fm[0])) {
$this->loginForm->setMessages(array('identity' => array($fm[0])));
}
if ($this->options->getUseRedirectParameterIfPresent()) {
$redirect = $request->getQuery()->get('redirect', !empty($post['redirect']) ? $post['redirect'] : false);
} else {
$redirect = false;
}
if (!$request->isPost()) {
return array('loginForm' => $form, 'redirect' => $redirect, 'enableRegistration' => $this->options->getEnableRegistration());
}
$form->setData($post);
if (!$form->isValid()) {
$this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN) . ($redirect ? '?redirect=' . rawurlencode($redirect) : ''));
}
// clear adapters
$this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
return $this->forward()->dispatch(static::CONTROLLER_NAME, array('action' => 'authenticate'));
}
示例2: testCorrectInputDataMerging
public function testCorrectInputDataMerging()
{
$this->disablePhpUploadCapabilities();
$form = new Form();
$form->add(['name' => 'collection', 'type' => 'collection', 'options' => ['target_element' => new TestAsset\TestFieldset('target'), 'count' => 2]]);
copy(__DIR__ . '/TestAsset/nullfile', __DIR__ . '/TestAsset/nullfile_copy');
$request = $this->request;
$request->setMethod('POST');
$request->setPost(new Parameters(['collection' => [0 => ['text' => 'testvalue1'], 1 => ['text' => '']]]));
$request->setFiles(new Parameters(['collection' => [0 => ['file' => ['name' => 'test.jpg', 'type' => 'image/jpeg', 'size' => 20480, 'tmp_name' => __DIR__ . '/TestAsset/nullfile_copy', 'error' => UPLOAD_ERR_OK]]]]));
$this->controller->dispatch($this->request, $this->response);
$plugin = $this->plugin;
$plugin($form, '/test/getPage', true);
$this->assertFalse($form->isValid());
$data = $form->getData();
// @codingStandardsIgnoreStart
$this->assertEquals(['collection' => [0 => ['text' => 'testvalue1', 'file' => ['name' => 'test.jpg', 'type' => 'image/jpeg', 'size' => 20480, 'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . 'TestAsset' . DIRECTORY_SEPARATOR . 'testfile.jpg', 'error' => 0]], 1 => ['text' => null, 'file' => null]]], $data);
// @codingStandardsIgnoreEnd
$this->assertFileExists($data['collection'][0]['file']['tmp_name']);
unlink($data['collection'][0]['file']['tmp_name']);
$messages = $form->getMessages();
$this->assertTrue(isset($messages['collection'][1]['text'][NotEmpty::IS_EMPTY]));
$requiredFound = false;
foreach ($messages['collection'][1]['file'] as $message) {
if (strpos($message, 'Value is required') === 0) {
$requiredFound = true;
break;
}
}
$this->assertTrue($requiredFound, '"Required" message was not found in validation failure messages');
}
示例3: testEmptyFormNameShouldNotRenderEmptyFormId
public function testEmptyFormNameShouldNotRenderEmptyFormId()
{
$form = new Form();
$form->setMethod('post')->setAction('/foo/bar')->setView($this->getView());
$html = $form->render();
$this->assertNotContains('id=""', $html, $html);
}
示例4: getFilter
/**
* @param null $name
* @return \Zend\Form\ElementInterface|Form
*/
public function getFilter($name = null)
{
if (!empty($name)) {
return $this->filter->get($name);
}
return $this->filter;
}
示例5: addCSRFElement
public function addCSRFElement($name, $value)
{
$csrf = new \Zend\Form\Element\Hidden($name);
$csrf->setValue($value);
$this->form->add($csrf);
return $this;
}
示例6: injectValidatorPluginManager
public static function injectValidatorPluginManager(Form $form, ServiceLocatorInterface $sl)
{
$plugins = $sl->get('ValidatorManager');
$chain = new FilterChain();
$chain->setPluginManager($plugins);
$form->getFormFactory()->getInputFilterFactory()->setDefaultFilterChain($chain);
}
示例7: add
/**
* @param \Zend\Form\Form $form
* @param $data
* @return Entity\Comment|null
* @throws \Exception
*/
public function add(\Zend\Form\Form $form, $data)
{
$serviceLocator = $this->getServiceLocator();
$entityManager = $serviceLocator->get('Doctrine\\ORM\\EntityManager');
$entityType = $entityManager->getRepository('\\Comment\\Entity\\EntityType')->findOneByAlias($data['alias']);
if (!$entityType) {
throw new EntityNotFoundException();
}
$form->setData($data);
if ($form->isValid()) {
if ($entityType->getIsEnabled()) {
$data = $form->getData();
$comment = new Entity\Comment();
$comment->setEntityType($entityType);
$user = $serviceLocator->get('Zend\\Authentication\\AuthenticationService')->getIdentity()->getUser();
$comment->setUser($user);
$comment->setComment($data['comment']);
$entityManager->getConnection()->beginTransaction();
try {
$hydrator = new DoctrineHydrator($entityManager);
$hydrator->hydrate($data, $comment);
$entityType->getComments()->add($comment);
$entityManager->persist($comment);
$entityManager->persist($entityType);
$entityManager->flush();
$entityManager->getConnection()->commit();
return $comment;
} catch (\Exception $e) {
$entityManager->getConnection()->rollback();
throw $e;
}
}
}
return null;
}
示例8: create
/**
* Create a new post
*
* @param array $data
* @param \Zend\Form\Form $form
* @return bool
*/
public function create($data, &$form)
{
$post = new PostEntity();
$em = $this->getEntityManager();
$form->bind($post);
$form->setData($data);
if (!$form->isValid()) {
return false;
}
// we rename the file with a unique name
$newName = FileUtilService::rename($data['post']['thumbnail'], 'images/posts', "post");
foreach (PostEntity::$thumbnailVariations as $variation) {
$result = FileUtilService::resize($newName, 'images/posts', $variation["width"], $variation["height"]);
if (!$result) {
$this->message = $result;
return false;
}
}
$post->setThumbnail($newName);
$post->setPostDate("now");
$post->setUrl($this->getPostUrl($post));
try {
$em->persist($post);
$em->flush();
$this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_POST_CREATED"]);
return true;
} catch (\Exception $e) {
$this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_POST_NOT_CREATED"]);
return false;
}
}
示例9: getModel
protected function getModel()
{
// Dependências
$form = new Form();
$inputFilter = new InputFilter();
$formSearch = new Form();
$inputFilterSearch = new InputFilter();
$persistence = $this->getMock('Balance\\Model\\Persistence\\PersistenceInterface');
// Parâmetro
$form->add(new Text('id'));
$inputFilter->add(new Input('id'));
// Parâmetro
$form->add(new Text('foo'));
$inputFilter->add(new Input('foo'));
// Pesquisa: Palavras Chave
$formSearch->add(new Text('keywords'));
$inputFilterSearch->add(new Input('keywords'));
// Configurações
$form->setInputFilter($inputFilter);
$formSearch->setInputFilter($inputFilterSearch);
// Inicialização
$model = new Model($persistence);
// Formulários
$model->setForm($form)->setFormSearch($formSearch);
// Apresentação
return $model;
}
示例10: create
/**
* Create a new image
*
* @param array $data
* @param \Zend\Form\Form $form
* @return bool
*/
public function create($data, &$form)
{
$entity = new ImageEntity();
$em = $this->getEntityManager();
$form->bind($entity);
$form->setData($data);
if (!$form->isValid()) {
return false;
}
// we rename the file with a unique name
$newName = FileUtilService::rename($data['image']['image'], 'images/gallery', "gallery");
foreach (ImageEntity::$thumbnailVariations as $variation) {
$result = FileUtilService::resize($newName, 'images/gallery', $variation["width"], $variation["height"]);
if (!$result) {
$this->message = $result;
return false;
}
}
$entity->setImage($newName);
try {
$em->persist($entity);
$em->flush();
$this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_IMAGE_CREATED"]);
return true;
} catch (\Exception $e) {
$this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_IMAGE_NOT_CREATED"]);
return false;
}
}
示例11: __invoke
public function __invoke(Form $form, $action)
{
$form->setAttribute('method', 'post');
$form->setAttribute('action', $action);
$form->prepare();
$view = $this->getView();
$html = '';
$html .= $view->form()->openTag($form);
$html .= '<table class="default-table">';
$formElements = $form->getElements();
foreach ($formElements as $formElement) {
if ($formElement instanceof Checkbox) {
$html .= $view->formRowCheckbox($form, $formElement);
} else {
if ($formElement instanceof Submit) {
$html .= $view->formRowSubmit($form, $formElement);
} else {
$html .= $view->formRowDefault($form, $formElement);
}
}
}
$html .= '</table>';
$html .= $view->form()->closeTag();
return $html;
}
示例12: _recursivelyPrepareForm
protected function _recursivelyPrepareForm(Form\Form $form)
{
$belongsTo = $form instanceof Form\Form ? $form->getElementsBelongTo() : null;
$elementContent = '';
$separator = $this->getSeparator();
$translator = $form->getTranslator();
$view = $form->getView();
foreach ($form as $item) {
$item->setView($view)->setTranslator($translator);
if ($item instanceof Form\Element) {
$item->setBelongsTo($belongsTo);
} elseif ($item instanceof Form\Form) {
if (!empty($belongsTo)) {
if ($item->isArray()) {
$name = $this->mergeBelongsTo($belongsTo, $item->getElementsBelongTo());
$item->setElementsBelongTo($name, true);
} else {
$item->setElementsBelongTo($belongsTo, true);
}
}
$this->_recursivelyPrepareForm($item);
} elseif ($item instanceof Form\DisplayGroup) {
if (!empty($belongsTo)) {
foreach ($item as $element) {
$element->setBelongsTo($belongsTo);
}
}
}
}
}
示例13: create
/**
* @param ServiceLocatorInterface $serviceLocator
* @return FormInterface
*/
public function create()
{
$formElementManager = $this->serviceManager->get('FormElementManager');
$form = new Form();
$form->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Previous'))->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Next'))->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Valid'))->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Cancel'));
return $form;
}
示例14: addExtSubForm
public function addExtSubForm(ZendForm $form, $name)
{
$extsForm = $this->getExtSubForm();
$form->setIsArray(true);
$form->removeDecorator('FormDecorator');
$extsForm->addSubForm($form, $name);
}
示例15: __invoke
public function __invoke(Form $form)
{
$form->setAttribute('class', self::DEFAULT_FORM_CLASS);
foreach ($form->getElements() as $element) {
/*
* controls how far the form indents into
* the page using Twitter:Bootstrap CSS
*
*/
$defLabelAttributes = array('class' => self::DEFAULT_LABEL_CLASS);
$element->setLabelAttributes($defLabelAttributes);
$element->setAttribute('class', self::DEFAULT_INPUT_CLASS);
/*
* set the id attribute of all inputs to be equal to their names
*
* makes life simple when trying to make the view
* dynamic
*/
$element->setAttribute('id', $element->getName());
}
/*
* the submit button is a little different, it uses
* a button class to proper rendering
*
*/
$form->get('submit')->setAttribute('class', self::DEFAULT_SUBMIT_BUTTON_CLASS);
return $form;
}