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

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

问题描述

我在一个页面上有两种形式:登录表单和注册表单。当我提交注册表,它验证两个:在登录和注册的表单字段。如果两个表单都有相同的型号(用户模型),我该如何处理



注册表单

 <?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();?

当我提交注册表单时,它会验证这两种表单,我只想验证注册表单。 p>

如何处理?



解决方案

我想出了一个解决方案我发现方法脏,但它工作)为不同的问题(非常类似于此)。那个另一个问题与元素和观点一起工作。我会在这里发布整个解决方案,看看它是否有助于某人(虽然我更喜欢别人有不同的方法)。



因此,首先:更改创建名称

  //注册
<?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'))?>

表单应该工作,查看并发布相同的操作, >

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

  public function login(){
if($ this-> request-> is('post')){
//更改request->数据索引以使一切都工作
if(isset($ this-> request-> data ['Login'] / *是我们给表单* /的名称)
$ this-> request-> data ['User'] = $ this-> request-> data ['Login'];
unset($ this-> request-> data ['Login']); //清理所有的工作,因为它现在正在工作
$ this-> set('formName','Login'); //我们需要传递一个引用到视图的验证显示
} //如果没有登录索引,我们可以假定请求来的正常方式

//你的代码应该正常工作
}
}

同样的事情注册需要将登录更改为注册)。



现在,操作应该正常,因为它不知道我们更改了视图上的表单名称确保改变动作中的索引)。 ,如果有验证错误,视图将在

中检查它们。

  $ this-> ; validationErrors ['Model_with_errors'] 

并且Model_with_errors不能以相应的形式显示,因为我们更改了名称。所以我们还需要调整视图。哦!我假设这两种形式都在一个视图中被称为 index.ctp ,例如,但如果他们在单独的文件(如果你使用一个元素或类似的)我建议为所有文件添加代码行。

  //最好在视图/元素的第一行ctp)
if(!empty($ this-> validationErrors ['User'])&&& isset($ formName)){
$ this-> validationErrors [$ formName ] = $ this-> validationErrors ['User'];
}



这样,我们将用户的模型验证复制到假名形式,只有那一个。请注意,如果您在同一模型的视图中有第三个表单,并且使用典型的 $ this-> form-> create('User')那么验证错误将会显示出来,除非你改变第三个的表单名称。



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



我发现这是一个混乱的方法,因为它涉及控制器视图更改。我认为一切都应该由控制器完成,并且视图应该不会闪烁关于验证问题...问题是, render 函数 Controller.php 需要替换...可以在 AppController 中完成,但是对于Cakephp的每个更新,必须小心将 Controller.php 的新的render函数复制到替换 AppController 中的函数。这种方法的优点是,特征将可用于每种形式,而不必担心改变视图。



好吧,这不是可维护的,所以更好地把它孤独,如果它只是为这一个case ...如果任何人有兴趣,如何处理这只是在控制器端,虽然,评论,我会后。


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)

Register form

<?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();?>

and Login form is below

  <?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.

How can I handle that?

解决方案

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']

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'];
}

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.

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天全站免登陆