本文整理汇总了PHP中Zend\Form\Form::getMessages方法的典型用法代码示例。如果您正苦于以下问题:PHP Form::getMessages方法的具体用法?PHP Form::getMessages怎么用?PHP Form::getMessages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Form\Form
的用法示例。
在下文中一共展示了Form::getMessages方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: hasFormErrors
protected function hasFormErrors(Form $form)
{
foreach ($form->getMessages() as $message) {
if (count($message) > 0) {
return true;
}
}
return false;
}
示例2: testSettingValidateAllFlagAfterPartialValidationForcesFullValidation
public function testSettingValidateAllFlagAfterPartialValidationForcesFullValidation()
{
$this->populateForm();
$this->form->setValidationGroup('foo');
$validSet = array('foo' => 'abcde');
$this->form->setData($validSet);
$this->form->setValidationGroup(Form::VALIDATE_ALL);
$this->assertFalse($this->form->isValid());
$messages = $this->form->getMessages();
$this->assertArrayHasKey('foobar', $messages, var_export($messages, 1));
}
示例3: formErros
/**
* Pega as mensagens do formulario e as dispoe
* em forma de lista ul,li
* @param \Zend\Form\Form $form
* @return string HTML
*/
public function formErros($form)
{
$msg = "";
$arrayMessage = $form->getMessages();
foreach ($arrayMessage as $elemName => $messages) {
foreach ($messages as $message) {
$label = "";
$elemName = $form->get($elemName);
if ($elemName != null) {
$label = $elemName->getLabel();
}
$msg .= "<ul><li>" . $label . " " . $message . "</li></ul>";
}
}
return $msg;
}
示例4: processForm
/**
* Process form
*
* @return Response|null
*/
public function processForm()
{
$request = $this->getRequest();
$this->form->setData($request->getPost());
$fm = $this->flashMessenger();
if (!$this->form->isValid()) {
$fm->addErrorMessage('The form was not valid. ' . var_export($this->form->getMessages(), true), 'error');
return;
}
try {
$object = $this->saveObject();
} catch (\Exception $exception) {
}
if (isset($exception)) {
$fm->addErrorMessage('The object was not saved. ' . $exception->getMessage());
return;
} else {
$fm->addSuccessMessage('The object has been successfully saved!');
}
return $this->redirectTo($object);
}
示例5: formAction
public function formAction()
{
// Build a name element.
$name = new Element('name');
$name->setLabel('Your name');
$name->setAttributes(array('type' => 'text'));
// Build a submit button element.
$send = new Element('send');
$send->setLabel('Send');
$send->setAttributes(array('type' => 'submit', 'value' => 'Send'));
// Build a checkbox element.
$check = new Checkbox('check');
$check->setLabel('Checkbox example');
// Build a multi-checkbox element.
$multicheck = new MultiCheckbox('multicheck');
$multicheck->setLabel('Multi checkbox example');
$multicheck->setOptions(array('value_options' => array('One' => 'one', 'Two' => 'two')));
// Assemble the form.
$form = new Form('contact');
$form->add($name);
$form->add($check);
$form->add($multicheck);
$form->add($send);
// Get the request if any.
$request = $this->getRequest();
$data = $request->getPost();
$form->setData($data);
// Validate the form
if ($form->isValid()) {
$validatedData = $form->getData();
$success = 'Form submit was successful';
} else {
$success = 'Form submit failed';
$messages = $form->getMessages();
}
// Set the method attribute for the form
$form->setAttribute('method', 'post');
return new ViewModel(array('form' => $form, 'success' => $success, 'messages' => $messages, 'data' => $data));
}
示例6: saveAction
public function saveAction(Params $params, Request $request, Response $response, Form $form, Finder $finderService, Update $updateService, Form $form, View $view)
{
if ($request->getMethod() !== Request::METHOD_PUT) {
return $view;
}
$id = $params('id');
$entity = $finderService->find(['T4webTranslate' => ['Words' => ['Id' => (int) $id]]]);
if (!$entity) {
$response->setStatusCode(Response::STATUS_CODE_404);
$view->setErrors(['message' => 'bad params']);
return $view;
}
$data = Json::decode($request->getContent(), Json::TYPE_ARRAY);
$form->setData($data);
if (!$form->isValid()) {
$response->setStatusCode(Response::STATUS_CODE_404);
$view->setErrors($form->getMessages());
return $view;
}
$entity->populate($data);
$result = $updateService->update($id, $entity->extract());
$view->setVariables($result->extract());
return $view;
}
示例7: hasMessage
/**
* Retorna classe (css) para elementos que tem mensagem (erro)
*
* @param string $element
* @param string $ifYes
* @return string
*/
public function hasMessage($element, $ifYes = 'alert-danger')
{
return (bool) parent::getMessages($element) ? $ifYes : '';
}
示例8: testOnlyElementsWithErrorsInMessages
public function testOnlyElementsWithErrorsInMessages()
{
$fieldset = new TestAsset\FieldsetWithInputFilter('set');
$fieldset->add(new Element('foo'));
$fieldset->add(new Element('bar'));
$form = new Form();
$form->add($fieldset);
$form->setInputFilter(new InputFilter());
$form->setData(array());
$form->isValid();
$messages = $form->getMessages();
$this->assertArrayHasKey('foo', $messages['set']);
$this->assertArrayNotHasKey('bar', $messages['set']);
}
示例9: testElementsOfSubFormReceiveCorrectDefaultTranslator
/**
* @group ZF-11831
*/
public function testElementsOfSubFormReceiveCorrectDefaultTranslator()
{
$isEmptyKey = \Zend\Validator\NotEmpty::IS_EMPTY;
// Global default translator
$trDefault = new Translator(array('adapter' => 'arrayAdapter', 'content' => array($isEmptyKey => 'Default'), 'locale' => 'en'));
Registry::set('Zend_Translate', $trDefault);
// Translator to use for elements
$trElement = new Translator(array('adapter' => 'arrayAdapter', 'content' => array($isEmptyKey => 'Element'), 'locale' => 'en'));
\Zend\Validator\AbstractValidator::setDefaultTranslator($trElement);
// Change the form's translator
$form = new Form();
$form->addElement(new \Zend\Form\Element\Text('foo', array('required' => true, 'validators' => array('NotEmpty'))));
// Create a subform with it's own validator
$sf1 = new SubForm();
$sf1->addElement(new \Zend\Form\Element\Text('foosub', array('required' => true, 'validators' => array('NotEmpty'))));
$form->addSubForm($sf1, 'Test1');
$form->isValid(array());
$messages = $form->getMessages();
$this->assertEquals('Element', @$messages['foo'][$isEmptyKey], 'Form element received wrong validator');
$this->assertEquals('Element', @$messages['Test1']['foosub'][$isEmptyKey], 'SubForm element received wrong validator');
}
示例10: testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays
public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
{
$form = new Form();
$form->setElementsBelongTo('foo[bar]');
$form->addElement('text', 'firstName')->getElement('firstName')->setRequired(false);
$form->addElement('text', 'lastName')->getElement('lastName')->setRequired(true);
$subForm = new \Zend\Form\SubForm();
$subForm->setElementsBelongTo('baz');
$subForm->addElement('text', 'email')->getElement('email')->setRequired(true)->addValidator('NotEmpty');
$subSubForm = new \Zend\Form\SubForm();
$subSubForm->setElementsBelongTo('bat[quux]');
$subSubForm->addElement('checkbox', 'home')->getElement('home')->setRequired(true)->addValidator('InArray', false, array(array('1')));
$subForm->addSubForm($subSubForm, 'subSub');
$form->addSubForm($subForm, 'sub')->addElement('submit', 'save', array('value' => 'submit'));
$data = array('foo' => array('bar' => array('lastName' => 'Cow')));
$form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
$data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
$form->isValidPartial($data);
$messages = $form->getMessages();
$this->assertFalse(empty($messages));
$this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
$this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
}
示例11: hydrate
/**
* Hydrate an array of data onto an entity using a form
*
* @param AbstractEntity $entity
* @param array $values
* @param Form $form
* @return AbstractEntity
*/
private function hydrate(AbstractEntity $entity, array $values, Form $form)
{
$form->setObject($entity);
if ($form->getBaseFieldset()) {
$form->getBaseFieldset()->setObject($entity);
}
$form->setData($values);
if (!$form->isValid()) {
return $this->setErrorMessages($form->getMessages());
}
$result = $form->getData();
if (!$result instanceof AbstractEntity) {
throw new RuntimeException('Unable to retrieve entity from Form');
}
return $result;
}
示例12: handleErrors
/**
* Check if the form is valid and return a response object if invalid
* @param \Zend\Form\Form $form
* @return bool|\Zend\Stdlib\ResponseInterface
*/
private function handleErrors(\Zend\Form\Form $form)
{
$request = $this->getRequest();
$form->setData($request->getPost()->toArray());
if (!$form->isValid()) {
return $this->jsonResponse(array('success' => false, 'errors' => $form->getMessages()));
}
return false;
}
示例13: parseFormMessage
/**
* @param \Zend\Form\Form $form
* @return bool|string
*/
private function parseFormMessage($form)
{
if (is_array($form->getMessages())) {
$errorMessage = '';
foreach ($form->getMessages() as $title => $values) {
$formElement = $form->getElements();
$errorMessage .= $formElement[$title]->getLabel() . PHP_EOL;
if (is_array($values)) {
foreach ($values as $value) {
$errorMessage .= '<li>' . $value . '</li>';
}
} else {
$errorMessage .= $values;
}
}
return $errorMessage;
} elseif (is_string($form->getMessages())) {
return $form->getMessages();
} else {
return false;
}
}
示例14: testValidateData
public function testValidateData()
{
$myFieldset = new Fieldset();
$myFieldset->add(array('name' => 'email', 'type' => 'Email'));
$myForm = new Form();
$myForm->add(array('name' => 'collection', 'type' => 'Collection', 'options' => array('target_element' => $myFieldset)));
$data = array('collection' => array(array('email' => 'test1@test1.com'), array('email' => 'test2@test2.com'), array('email' => 'test3@test3.com')));
$myForm->setData($data);
$this->assertTrue($myForm->isValid());
$this->assertEmpty($myForm->getMessages());
}
示例15: updateIteration
/**
* @param $tag
* @param Form $form
* @param Params $params
* @return bool
*/
public function updateIteration($tag, Form $form, Params $params)
{
$valid = true;
$this->getEventManager()->trigger(self::EVENT_FORM_DATA . $tag, $this, array('form' => $form, 'data' => $params));
if (!$this->call($this->getFormValidators(), $tag, array($params, $form), function (Params $params, Form $form) {
return $form->isValid();
})) {
$valid = false;
$this->errorMessages[$tag] = $this->normalizeMessages($form->getMessages());
}
return $valid;
}