单页 cakephp 上具有相同型号名称的多个表单 [英] Multiple form with same model name on single page cakephp

查看:14
本文介绍了单页 cakephp 上具有相同型号名称的多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个页面上有两个表单:登录表单和注册表单.当我提交注册表时,它会同时验证:登录和注册中的表单字段.如果两个表单具有相同的模型(用户模型),我该如何处理

I have two form on a single page: login form and register form. When I submit the register form, it validates both: form fields that are in login and registeration. How can I handle it if both form have the same model (user model)

报名表

<?php echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'add'))); ?>
<?php echo $this->Form->input('username', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('email', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('password', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('confirm_password', array('type' => 'password', 'label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->submit(__('Submit', true), array ('class' => 'reg_button', 'div' => false)); 
  echo $this->Form->end();?>

和登录表单在下面

  <?php echo $this->Form->create('User', array('controller' => 'users', 'action' => 'login'))?>
  <?php echo $this->Form->input('User.username',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>
  <?php echo $this->Form->input('User.password',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>
  <?php echo $this->Form->submit(__('Log in', true), array ('class' => 'reg_button', 'div' => false)); ?>
  <?php echo $this->Form->end();?>

当我提交注册表时,它会验证两个表单,我只想验证注册表.

When I submit registration form it validates both forms, I want to validate only the registration form.

我该如何处理?

推荐答案

我为 不同的问题(与此非常相似).不过,另一个问题与元素和视图有关.我会在这里发布整个解决方案,看看它是否对某人有帮助(尽管我宁愿其他人采用不同的方法).

I've come up with a "solution" (I find the approach dirty, but it works) for a different question (very similar to this). That other question worked with elements and views, though. I'll post the entire solution here to see if it helps someone (though I rather someone else comes with a different approach).

所以,首先:更改两个表单的创建名称.

So, first: change the creation names for the two forms.

//for the registration
<?php echo $this->Form->create('Registration', 
      array('url' => array('controller' => 'users', 'action' => 'add'))); ?>
//for the login
<?php echo $this->Form->create('Login', 
      array('controller' => 'users', 'action' => 'login'))?>

表单应该工作,看起来和发布到相同的操作,所以没有伤害.

The forms should work, look and post to the same actions, so no harm done.

第二步:我没有你的操作代码,所以我将解释一般需要做什么

Second step: I don't have your action code, so I'm going to explain what needs to be done in general

public function login() {
    if ($this->request->is('post')) {
        //we need to change the request->data indexes to make everything work
        if (isset($this->request->data['Login'] /*that's the name we gave to the form*/)) {
            $this->request->data['User'] = $this->request->data['Login'];
            unset($this->request->data['Login']); //clean everything up so all work as it is working now
            $this->set('formName', 'Login'); //we need to pass a reference to the view for validation display
     } //if there's no 'Login' index, we can assume the request came the normal way

    //your code that should work normally
    }
}

注册也是一样(只需要把登录"改成注册"即可).

Same thing for the registration (only need to change 'Login' to 'Registration').

现在,动作应该正常运行,因为它不知道我们更改了视图上的表单名称(我们确保更改了动作中的索引).但是,如果有验证错误,视图会在

Now, the actions should behave normally, since it has no idea we changed the form names on the view (we made sure of that changing the indexes in the action). But, if there are validation errors, the view will check for them in

$this->validationErrors['Model_with_errors']

并且Model_with_errors"(在本例中为User")不会显示在相应的表单中,因为我们更改了名称.所以我们还需要调整视图.哦!例如,我假设这两种形式都在名为 index.ctp 的视图中,但如果它们位于单独的文件中(如果您使用的是元素或类似元素),我建议添加以下行所有文件的代码

And that 'Model_with_errors' (in this case 'User') won't be displayed in the respective forms because we've changed the names. So we need to also tweak the view. Oh! I'm assuming these both forms are in a view called index.ctp, for example, but if they are on separate files (if you're using an element or similar) I recommend add the lines of code for all the files

//preferably in the first line of the view/element (index.ctp in this example)
if (!empty($this->validationErrors['User']) && isset($formName)) {
    $this->validationErrors[$formName] = $this->validationErrors['User'];
}

这样,我们将用户的模型验证复制到假命名的表单中,并且只有那个表单.请注意,如果您在该视图中有相同模型的第三个表单,并且您使用典型的 $this->form->create('User'),则验证错误将显示除非您更改第三个的表单名称.

With that, we copy the model validation of the User to the fake-named form, and only that one. Note that if you have a third form in that view for the same model, and you use the typical $this->form->create('User'), then the validation errors will show for that one too unless you change the form name for that third one.

这样做应该有效,并且只验证具有正确名称的表单.

Doing that should work and only validate the form with the correct name.

我发现这是一种凌乱的方法,因为它涉及控制器视图的更改.我认为一切都应该由控制器完成,并且视图甚至不应该因为验证问题而闪烁......问题在于 Controller.phprender 函数> 需要更换... 可以在AppController中完成,但是每次升级Cakephp,都要小心复制Controller.php新的render函数AppController 中替换它的那个.不过,这种方法的优点是功能"可用于每个表单,而不必担心更改视图.

I find this a messy approach because it involves controller-view changes. I think everything should be done by the controller, and the view shouldn't even blink about validation issues... The problem with that is that the render function of Controller.php needs to be replaced... It can be done in the AppController, but for every updgrade of Cakephp, you'll have to be careful of copying the new render function of Controller.php to the one replacing it in AppController. The advantage of that approach, though, is that the "feature" would be available for every form without having to worry about changing the views.

好吧,无论如何它都不是那么可维护,所以如果只是为了这种情况,最好不要管它......如果有人对如何在控制器方面处理这个感兴趣,但是,评论,我会发布它.

Well, it's just not that maintainable anyway, so better to leave it alone if it's just for this one case... If anyone is interested on how to handle this just in the controller side, though, comment and I'll post it.

这篇关于单页 cakephp 上具有相同型号名称的多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆