Saturday, February 9, 2013

saveAssociated validation not working

Hi, i have a problem with validation when using saveAssociated. Poll model validation works fine, but PollOptions model validation not...

Controller
    public function admin_create() {
        if ($this->request->is('post')) {
            //$this->Poll->set($this->request->data);
            if ($this->Poll->saveAssociated($this->request->data)) {
                $this->Session->setFlash('Saved.', 'alerts/success');
                $this->redirect(array('action' => 'admin_index'), null, true);
            }
        }
    }

 
Poll model
<?php
App::uses('PollAppModel', 'Polls.Model');
class Poll extends PollAppModel {
    public $name = 'Poll';
    public $useTable = 'polls';
    public $hasMany = array(
        'PollOptions' => array(
            'className' => 'PollsOptions',
            'foreignKey' => 'poll_id',
            'dependent' => true
        )
    );
    public $validate = array(
        'title' => array(
            array('rule' => 'notEmpty',
                'message' => 'Enter title.'
            ),
            array(
                'rule' => array('maxLength', 150),
                'message' => 'Too long.'
            )
        )
    );
}
?>
PollOptions model
<?php
App::uses('PollAppModel', 'Polls.Model');
class PollOptions extends PollAppModel {
    public $name = 'PollOptions';
    public $useTable = 'polls_options';
    public $validate = array(
        'title' => array(
            array('rule' => 'notEmpty',
                'message' => 'Enter title.'
            ),
            array(
                'rule' => array('maxLength', 150),
                'message' => 'Too long.'
            )
        )
    );
}
?>

Form
 <?php
echo $this->Html->css(array('/Polls/css/polls'), 'stylesheet', array('inline' => false));
?>
<script>
    $(function() {
        $('#addOption').click(function(event) {
            event.preventDefault();
            var newOption = $('.options:eq(0)').clone();
            var optionNum = $('#options input').length+1;
            newOption.find('input').val('').attr('id', 'PollOptions'+optionNum+'Title').attr('name', 'data[PollOptions]['+optionNum+'][title]');
            $('#options').append(newOption);
        })
    })
</script>
<div>
    <?php echo $this->Form->create('Poll', array('class' => 'form-horizontal', 'novalidate' => true)); ?>
    <div class="control-group">
        <div class="required">
            <?php echo $this->Form->label('Poll.title', 'Title:', array('class' => 'control-label')); ?>
        </div>
        <div class="controls">
            <?php
            echo $this->Form->input('Poll.title', array('type' => 'text', 'class' => 'input-block-level',
                'maxlength' => 150, 'label' => false));
            ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo $this->Form->label('Poll.active', 'Active:', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php
            echo $this->Form->checkbox('Poll.active', array('label' => false));
            ?>
        </div>
    </div>
    <hr>
    <div id="options" class="control-group">
        <div class="required">
            <?php echo $this->Form->label('PollOptions.title', 'Options:', array('class' => 'control-label')); ?>
        </div>
        <div class="controls options">
            <?php
            echo $this->Form->input('PollOptions.0.title', array('type' => 'text', 'class' => 'input-block-level',
                'maxlength' => 150, 'label' => false));
            ?>
        </div>
        <div class="controls options">
            <?php
            echo $this->Form->input('PollOptions.1.title', array('type' => 'text', 'class' => 'input-block-level',
                'maxlength' => 150, 'label' => false));
            ?>
        </div>
    </div>
    <div align="right">
        <button class="btn small" id="addOption"><i class="icon-plus"></i> Add option</button>
    </div>
    <div class="form-actions">
        <button type="submit" class="btn btn-success">Save</button>
        <button type="reset" class="btn">Cancel</button>
    </div>
    <?php echo $this->Form->end(); ?>
</div>

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: