【问题标题】:How to create a step by step form with Phalcon in PHP如何在 PHP 中使用 Phalcon 逐步创建表单
【发布时间】:2014-08-14 23:38:57
【问题描述】:

我目前正在使用 Phalcon 框架进行一个项目,该框架的页面具有复杂的表单和大量输入,为了很好地分解它,我将表单分为一步一步的过程。

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

我似乎找不到任何有关此类过程的记录,因为如果我使用表单生成器,它喜欢完整地验证表单。

【问题讨论】:

    标签: php forms phalcon


    【解决方案1】:

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

    例如我的意思是“控制器”

    <?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->session->get('step1');这样您就可以在最后一步加载所有发布的数据并将其存储到数据库中。

    我希望这会有所帮助! :)

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

    <?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')
                ));
            }
    }
    

    【讨论】:

    • 感谢您的帖子@Kamil,但 $form-isValid($this->request->getPost()) 不会在第 1 步抛出错误,因为第 2 步所需的字段不是还没有输入吗?
    • 正如我所说,您必须定义自定义方法来呈现任何您不会为此使用初始化函数的步骤,例如步骤 1: $form = new MyForm(); $form->initializeStep1();里面只是对 step1 的所有元素的验证,所以如果你为 step1 调用 ->isValid 将是正确的:)
    • 我想我明白你的意思了我已经有了将步骤保存到会话的想法,但在验证方面不确定。感谢任何一种方式都朝着正确的方向迈出了一步:)
    • 确保它会起作用:),不客气! :)
    【解决方案2】:

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

    【讨论】:

    • 我最终按照@Kamil 的回答做了一些事情。如果使用 Phalcon 的表单生成器创建的表单有超过 1 个步骤,它会被分成多个步骤函数。这似乎是最可行的解决方案,并且正在做我们需要做的事情。
    猜你喜欢
    • 2011-04-19
    • 2017-05-06
    • 2022-08-02
    • 2014-08-03
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多