P K
P K

Reputation: 3

Cakephp multiple input fields

I'm a newbie at cakephp. I got a form with 5 inputs. My form should be able to save either one user input or all 5 inputs. I'm able to save when user fills all 5 inputs, however, when user fills only 1 or 2 and saves it. Blank spaces with date created (current date) gets saved in the database. How can i make it to save only the user inputs from the form without any empty fields in the database. My Add function below.

public function add() {

    if ($this->request->is('post')) {
        $this->Item->create();

    for ($i=0;$i<5;$i++){
    if(empty($this->request->data['Item'][$i]['name'])){

    }else{


        $name = $this->request->data['Item'][$i]['name'];
        $explode_name = explode(":",$name);
        $this->request->data['Item'][$i]['name'] = $explode_name[0];
        $this->request->data['Item'][$i]['hrid'] = $explode_name[1];
    }   
}


    if ($this->Item->saveAll($this->request->data['Item'])) {
        $this->Session->setFlash(__('The Item has been saved'));
        $this->redirect(array('action' => 'index'));
    } else {
        $this->Session->setFlash(__('The item could not be saved. Please, try again.'));
    }

    }   
    $itemTypes = $this->Item->ItemType->find('list',array('order' =>array('ItemType.name' => 'asc')));
    $this->set(compact('itemTypes'));
}

Upvotes: 0

Views: 417

Answers (2)

Sumon Sarker
Sumon Sarker

Reputation: 2795

In CakePHP 2.x you can do it as like below-

public function add(){
  if ($this->request->is('post')) {
  $this->Item->create();
  $items = $this->request->data['Item']; /*Get all items Array*/
  $items = array_filter(array_map('array_filter', $items)); /*Remove all empty array, only keep Array with user inputs*/
  if ($this->Item->saveAll($items)) {
    /*Success*/
  } else {
    /*Error*/
  }
}

}

In CakePHP 3.x you can do it as like below-

public function add() {
  if ($this->request->is('post')) {
    $items = $this->request->data['Item']; /*Get all items Array*/
    $items = array_filter(array_map('array_filter', $items)); /*Remove all empty array, only keep Array with user inputs*/
    $entities = $this->Item->newEntities($items); /*Prepare all Data*/
    if($this->Item->saveMany($entities)){ /*Save all data*/
       /*Success*/
    }else{
       /*Error*/
    }
  }
}

Upvotes: 0

Rohit Ailani
Rohit Ailani

Reputation: 910

There is a small thing that you are missing, and it is that whether there is name empty or not but it has a value set for that particular index. You should unset that in case the value is empty as below

public function add() {

    if ($this->request->is('post')) {
        $this->Item->create();

        for ($i=0;$i<5;$i++){
            if(empty($this->request->data['Item'][$i]['name'])){
                // here we are removing the empty name index so that it does not saves the result
                unset($this->request->data['Item'][$i]);
            }else{


                $name = $this->request->data['Item'][$i]['name'];
                $explode_name = explode(":",$name);
                $this->request->data['Item'][$i]['name'] = $explode_name[0];
                $this->request->data['Item'][$i]['hrid'] = $explode_name[1];
            }   
        }

        // also here we should check here that atleast there is one entry
        if(!empty($this->request->data['Item'])){
            if ($this->Item->saveAll($this->request->data['Item'])) {
                $this->Session->setFlash(__('The Item has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The item could not be saved. Please, try again.'));
            }
        } else {
                $this->Session->setFlash(__('There is no such item. Please fill value for at least one item.'));
        }
    }   
    $itemTypes = $this->Item->ItemType->find('list',array('order' =>array('ItemType.name' => 'asc')));
    $this->set(compact('itemTypes'));
}

Please try the above code.

Upvotes: 0

Related Questions