2011-08-01 6 views
1

フォームを保存できないレンダリングビューの編集ビューでユーザー名が検証されるという奇妙な問題が発生しました。私はこのビューにユーザ名の入力がないので、なぜこれが検証されているのですか?フォームにフィールドが存在しなくてもユーザー名が検証されています

次のように検証は次のとおりです。

var $validate = array(
       'username' => array(
      'empty' => array(
           'rule' => 'notEmpty', 
           'required' => true, 
           'allowEmpty' => false, 
           'message' => 'Username is required', 
      ), 
      'isUnique' => array(
           'rule' => 'isUnique', 
           'message' => 'This username has already been taken' 
      ), 
      'pattern' => array(
           'rule' => array('custom','/[a-zA-Z0-9\_\-]{6,}$/i'), 
           'message' => 'Must be 6 characters or longer with no spaces', 
      ), 
      'length' => array(
           'rule' => array('maxLength', 15), 
           'message' => 'Please keep username under 15 characters', 
      ), 
      'notEmpty' => array(
           'rule' => 'notEmpty', 
           'message' => 'Username cannot be empty', 
      ) 
); 

以下の編集機能:

function edit($id = null) { 
      $this->set('title_for_layout', 'Edit Profile'); 
      unset($this->User->validate['email']['email']); 
      unset($this->User->validate['username']); 
      if($this->Auth->user('id')==$id) { 
       $this->set('user', $this->User->read(null, $id)); 
       } else { 
        $this->Session->setFlash(__('You are not authorised to edit other member profiles', true)); 
        $this->redirect(array('action' => 'index')); 
     } 
     if (!empty($this->data)) { 
      if ($this->User->save($this->data)) { 
       $this->Session->setFlash(__('Your profile has been saved', true)); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('Your profile could not be saved. Please, try again.', true)); 
      } 
     } 
     if (empty($this->data)) { 
      $this->data = $this->User->read(null, $id); 
     } 
     $groups = $this->User->Group->find('list'); 
     $this->set(compact('groups')); 
    } 

私は、私だけは、フォームを保存することができますが、これは正しい方法ですることはできません解除usernameを持っていますこれを行うために。誰かがこれにいくつかの光を当てることができますか?

多くの感謝!

答えて

1

required => trueは 'username'キーを必須フィールドにしています。つまり、ユーザー名キーを使用せずに設定を更新しようとすると無効になります。

私は自分で作ったわけではありませんが、'on' => 'create'emptyルールに追加しようとします。そのため、レコードの作成時にのみ適用されます。

+0

頭に釘を打つ - 私はちょうど来て、何かを書きたいとあなたは私の発見を確認した! '' on '=>' create''は全て欠けていた..ありがとう! – Plastika

+0

required => trueを使用しないことを強くお勧めします。要求は異なって達成することができます – mark

+0

詳細については、 "必要とされる"と "allowEmpty"についての神話を参照してください。http://www.dereuromark.de/2010/09/21/saving-model-data-and-security/ – mark

関連する問題