Answer the question
In order to leave comments, you need to log in
How to send first_name to database?
<?php
/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Form\Controller;
use Form\Form\FormApplication;
use Form\Model\Form;
use Zend\Form\Element\Button;
use Zend\Form\Element\Checkbox;
use Zend\Form\Element\Date;
use Zend\Form\Element\File;
use Zend\Form\Element\Hidden;
use Zend\Form\Element\Number;
use Zend\Form\Element\Select;
use Zend\Form\Element\Submit;
use Zend\Form\Element\Text;
use Zend\Form\Element\Textarea;
use Zend\Form\View\Helper\FormLabel;
use Zend\InputFilter\InputFilter;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Validator\StringLength;
use Zend\View\Model\ViewModel;
use Zend\Validator\EmailAddress;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$json = '
[
{
"type":"text",
"label":"First name",
"placeholder":"First name",
"className":"form-control",
"name":"first_name",
"subtype":"text",
"maxlength": "30"
},
{
"type": "button",
"subtype": "submit",
"label": "Button",
"className": "btn btn-success",
"name": "button-1511804098888",
"style": "success",
"value": "Apply"
}
]';
$actionUrl = $this->url()->fromRoute('formApplication');
$form = new \Zend\Form\Form();
$form->setAttribute('method', 'post');
$form->setName('test_from');
$form->setAttribute('action', $actionUrl);
$inputFilter = new InputFilter();
$elements = json_decode($json);
foreach ($elements as $element) {
switch ($element->type) {
case 'text':
$textElement = new Text($element->name);
$textElement->setAttribute('placeholder', $element->placeholder);
$textElement->setAttribute('class', $element->className);
$form->add($textElement);
$validators = [];
$filters = [];
if (isset($element->maxlength)) {
$maxLengthValidator = new StringLength();
$EmailValidator = new EmailAddress();
if (is_string($element->maxlength)) {
$maxLengthValidator->setMax($element->maxlength);
$validators[] = $maxLengthValidator;
$validators[] = $EmailValidator;
}
}
$isRequired = false;
if (isset($element->required)) {
$isRequired = true;
}
$inputFilter->add([
'name' => $element->name,
'required' => $isRequired,
'filters' => $filters,
'validators' => $validators,
]);
break;
case 'button':
$subType = $element->subtype;
switch ($subType) {
case 'submit':
$submitElement = new Submit($element->name);
$submitElement->setAttribute('class', $element->className);
$submitElement->setAttribute('value', $element->value);
$form->add($submitElement);
break;
}
break;
}
}
$form->setInputFilter($inputFilter);
if ($this->getRequest()->isPost()) {
$postData = $this->getRequest()->getPost();
$form->setData($postData);
if ($form->isValid()) {
$validData = $form->getData();
var_dump($validData);
die ('valid');
} else {
var_dump($form->getMessages());
die('not valid');
}
}
return new ViewModel([
'form' => $form
]);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question