0
votes

Using Cake v2.x I have a a form for my add method that offers four html select inputs. The selects are optional, if the user makes one selection and saves the format of my array to be saved is as follows:

array(
    'AttributeProductVariation' => array(
        'product_id' => '26',
        'sku' => 'xxxxxx',
        'stock_level' => '10',
        'id' => ''
    ),
    'AttributeVariation' => array(
        (int) 1 => array(
            'variation_id' => '1'
        ),
        (int) 2 => array(
            'variation_id' => ''
        ),
        (int) 4 => array(
            'variation_id' => ''
        ),
        (int) 7 => array(
            'variation_id' => ''
        )
    )
)

The problem is that values are saved in the AttributeVariation table with variation_id = 0 for the 3 selects which were left empty. I need these option to be ignored and not saved in this case.

My formhelper is as follows:

$this->Form->input('AttributeVariation.'.($key).'.variation_id', [  
     'label' =>  $attributes[$key],                       
     'required' => false,
     'options' => $variations[$key],
     'empty' => 'Select an Attribute if required',
]);

I save as follow:

$this->AttributeProductVariation->saveAll($this->request->data)

I could of course look for empty selects and remove these from the array before saving but these seems like a hack? I'm sure the Framework can deal with this out of the box?

1
you can check for variation_id is not emptyPoonam

1 Answers

0
votes

You could use data validation:

http://book.cakephp.org/2.0/en/models/data-validation.html

public $validate = array(
    'variation_id' => array(
        'notempty' => array( 
            'rule => array('notempty') 
        )
    )
)