Life, SAP, Consulting, Programming, Coding, ASP.NET, Sharepoint, MVC, Javascript, PHP, WebDesign, CSS, HTML

Posts tagged ‘cakephp’

CakePHP set validate from Controller

I am still new to CakePHP. I need an app that allow people to login with both Social Authentication and built-in authentication. When mapping new user from social site, there are some fields can be empty. For example, username. It’s sometimes empty. But it will be required when user register your site. So, how to output and let user know that your it is required?

My solution is to create a function inside Controller, which will be called from register form output function.

Here is the code:

private function _buildValidate(){
$this->User->validate[‘username’] = array(‘required’ => true);
$this->User->validate[‘name’] = array(‘required’ => true);
$this->User->validate[’email’] = array(‘rule’ => ’email’, ‘required’ => true);
$this->User->validate[‘password’] = array(‘rule’ => array(‘minLength’, ‘6’),
‘required’ => true,
‘message’ => ‘Minimum 6 characters long’);
$this->User->validate[‘birthday’] = array(‘rule’ => ‘date’, ‘required’ => true);
$this->User->validate[‘gender’] = array(‘required’ => true);
$this->User->validate[‘city_id’] = array(‘required’ => true);
}

Woawo!!! Cake is on the way to be baked:)

Advertisements

Cập nhật một trường trong CAKEPHP

Bạn đã biết đến sự tiện dụng trong CakePHP, tất cả gần như được hỗ trợ. Chỉ chưa đến 10 dòng code, đã có thể thêm/cập nhật một record mà không cần tốn một dòng truy vấn SQL nào cả. Tuy nhiên, nếu cập nhật chỉ một trường trong một record thì thế nào?

Giả sử rằng trường cần cập nhật là ‘confirmed’ trong model có tên là ‘User’ và id là 5. Chúng ra sẽ khai báo thêm 1 mảng như sau:

  1. $data = array(
  2. ‘User’ => array(
  3. ‘id’          =>    5,
  4. ‘confirmed’   =>    1
  5. )
  6. );


$data = array(
‘User’ => array(
‘id’ => 5,
‘confirmed’ => 1
)
);

Sau khi xử lý, Hàm Save sẽ như sau:

  1. $this->User->save( $data, false, array(‘confirmed’) );

Hoặc có thể sử dụng hàm sau:

$this->User->saveField(‘field_name’,’value’);
$this->User->save( $data, false, array(‘confirmed’) );

Cannot modify header information

Cannot modify header information – CAKEPHP

I recently try CAKEPHP platform. Soon, I encountered with many problem. But after that, I have fixed them all, and learned new experiences.

Cannot modify header information

This error occurred when I tried to apply login validation on my server. XXXController requires logged-in user. Each controller went well. Except that when users not logged in, XXX will redirect to Users/login. This is point I got my erro. I tried some search. There are many people had encountered the same error. Almost guru say that It was dued to WHITESPACE out side of <?php ?> tag. I had checked many time my controllers. No WHITESPACE. I cleared my controller. Rewrote them. No use. Finally, I noted that I used HTML comment <!–comment –> out side of the <?php ?> tag. I had removed these comment. Aha. They are not truly WHITESPACE, but they gave madness. Hope this post will help some new dummy like me.