如何在PHP中使用Phalcon创建一步一步的表单

时间:2014-08-14 23:38:58

标签: php forms phalcon

我目前正在使用Phalcon框架进行项目,该框架具有复杂形式和大量输入的页面,可以很好地分解我将表单分成一个循序渐进的过程

如何在进入下一步之前验证每一步的表格,然后将整个表格保存到最后一步?

我似乎无法找到关于此类流程的任何记录,因为如果我使用表单构建器,它就会完整地验证表单。

2 个答案:

答案 0 :(得分:1)

简单,只需在表单类中创建一个自定义方法来验证任何步骤,并将来自某个步骤的已发布数据保存到消息类中,并通过" stepX"将其存储到会话中,当发布的数据不是有效只是从post设置默认值。有效时将其保存到会话中,如上所述。

例如我的意思" controller"

<?php
class MyController extends BaseController {
     public function processStep1Action(){
           $form = new MyForm();
           if($this->request->isPost()){//im using my custom request class 
              if(!$form->isValid($this->request->getPost()){
                  //error messages goes here
                  $form->setDefaultsFromRequest($this->request); // it will set the filled data
              }
              else {
                  $messageClass = new MyMessageContainer();
                  $messageClass->setData($this->request);//inside parse requested data into message class, or parse it as $messageClass->name = $this->request->getPost('name');

                  $this->session->save('step1',$messageClass); //maybe it would be want to serialize it
                  //then redirect to the step 2 or x

              }
           }
     }
}

因此,在下一步中,您可以访问会话中的数据$ this-&gt; session-&gt; get(&#39; step1&#39;);因此,您可以在最后一步加载所有发布的数据并将其存储到数据库中。

我希望这有帮助! :)

这是我的表格,也许对你有帮助。

<?php 

namespace Manager\Library\Forms\User;

use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Email,
    Phalcon\Forms\Element\Select,
    Phalcon\Forms\Element\Password,
    Phalcon\Forms\Element\Check,
    Phalcon\Validation\Validator\Confirmation,
    Phalcon\Validation\Validator\StringLength,
    Phalcon\Forms\Element\Submit,
    Phalcon\Validation\Validator\PresenceOf,
    Model\Group;

class AddUser extends Form {
    public function initialize()
    {
        $email = new Email('email');
        $email->addValidators(array(
            new \Phalcon\Validation\Validator\Email(array(
            'message' => 'Nezadali jste email nebo má nesprávny tvar(email@domena.tld).'
            ))
        ));

                $this->add($email);

        $this->initGroupElement();

                $password = new Password('password');
        $password
            ->addValidator(new StringLength(array('min' => 6,'messageMinimum'   => 'Nezadali jste heslo nebo je příliš krátke, minimální počet znaků je 6.')))
            ->addValidator(new Confirmation(array('with'    => 'password-again',"message"   => "Zadané hesla se neshodují.")));
        $this->add($password);

        $repeatPassword = new Password('password-again');
        $this->add($repeatPassword);

                $this->initializeProfileElements();
                $active = new Check('active',array('value'  => 1));
                $this->add($active);
        $this->add( new Submit('save') );

                \Phalcon\Tag::setDefault('password', '');
                \Phalcon\Tag::setDefault('password-again', '');
    }

        public function initializeEdit(){
            $email = new Email('email');
        $email->addValidators(array(
            new \Phalcon\Validation\Validator\Email(array(
            'message' => 'Nezadali jste email nebo má nesprávny tvar(email@domena.tld).'
            ))
        ));

                $this->add($email);

        $this->initGroupElement();

                $password = new Password('password');

        $this->add($password);

        $repeatPassword = new Password('password-again');
        $this->add($repeatPassword);

                $this->initializeProfileElements();
                $active = new Check('active',array('value' => 1));
                $this->add($active);
        $this->add( new Submit('save') );

                \Phalcon\Tag::setDefault('password', '');
                \Phalcon\Tag::setDefault('password-again', '');
        }

        protected function initGroupElement(){
            $auth = \Core\Auth::getIdentity();
            $groups = new Group();
//            $groups->addColumns(array('id','name'));
            //set global condition about Super Admin
            $groups->addFilter('id', 1,'<>');

            if($auth){
                //set restrictions for main groups
                if((int)$auth->group_id === 1){ //super admingroup
                    //no filter
                }
                else if((int)$auth->group_id === 2){ //admin group
                    $groups->addFilter('id', 1,'>');
                }
                else if((int)$auth->group_id === 6){//Provozovatel group
                    $groups->addFilter('id',array(3,6,7));
                    $groups->addFilter('public', 1,'=',true);
                }
                else { // other groups
                    $groups->addFilter('public', 1);
                }
            }

            $groups = $groups->findFiltered();
            $groupElement = new Select('group');
            foreach($groups as $group){
                $groupElement->addOption(array($group->id => $group->name));
            }

            $this->add($groupElement);

        }

        protected function initializeProfileElements(){
            $forename = new \Phalcon\Forms\Element\Text('forename');
            $this->add($forename);
            $surname = new \Phalcon\Forms\Element\Text('surname');
            $this->add($surname);
            $street = new \Phalcon\Forms\Element\Text('street');
            $this->add($street);
            $postal = new \Phalcon\Forms\Element\Text('postal');
            $this->add($postal);
            $city = new \Phalcon\Forms\Element\Text('city');
            $this->add($city);
            $ic = new \Phalcon\Forms\Element\Text('ic');
            $this->add($ic);
            $dic = new \Phalcon\Forms\Element\Text('dic');
            $this->add($dic);
        }

        public function setDefault($fieldName,$value){
            \Phalcon\Tag::setDefault($fieldName, $value);
        }

        public function setDefaults($object){
            if($object instanceof \Model\User){
                $this->setDefaultsFromObject($object);
            }
            else if($object instanceof \Phalcon\Http\Request){
                $this->setDefaultsFromRequest($object);
            }
        }

        protected function setDefaultsFromObject(\Model\User $user){
            $profile = $user->getRelated('\Model\Profile');   
            \Phalcon\Tag::setDefaults(array(
                'email'     => $user->email,
                'group'     => $user->group_id,
                'active'    => $user->active,
                'forename'  => $profile->forename,
                'surname'   => $profile->surname,
                'street'    => $profile->street,
                'city'      => $profile->city,
                'postal'    => $profile->postal,
                'ic'        => $profile->IC,
                'dic'       => $profile->DIC
            ));
        }

        protected function setDefaultsFromRequest(\Phalcon\Http\Request $request){
            \Phalcon\Tag::setDefaults(array(
                    'email'     => $request->getPost('email'),
                    'group'     => $request->getPost('group'),
                    'active'    => $request->getPost('active')
             ));

            \Phalcon\Tag::setDefaults(array(
                'forename'  => $request->getPost('forename'),
                'surname'   => $request->getPost('surname'),
                'street'    => $request->getPost('street'),
                'city'      => $request->getPost('city'),
                'postal'    => $request->getPost('postal'),
                'ic'        => $request->getPost('ic'),
                'dic'       => $request->getPost('dic')
            ));
        }
}

答案 1 :(得分:0)

除了卡米尔的答案之外,另一个需要考虑的选择是在前端使用Javascript来处理您的多步骤表单。这将增加一些复杂性,因为您需要使用javascript来处理表单步骤并进行初步验证,但它只需要一个提交,您可以在单个方法中验证内容。