cakephp saveall,表单中包含动态数量的元素

时间:2015-05-05 08:09:56

标签: php cakephp

// add.ctp

<?php echo $this->Form->create('City');?>
    <fieldset>

        <?php 
        echo $this->Form->input('City.city_name');
        echo $this->Form->input('City.city_latitude');
        echo $this->Form->input('City.city_longitude');
        ?>
        <div class="add_more_city"><button class="btn green">Add More</button></div>
        <?php echo $this->Form->submit('Save', array('class' => 'form-submit',  'title' => 'Click here to add the city')); ?>
    </fieldset>
<?php echo $this->Form->end(); ?>
    <script>
    $(document).ready(function(){
        $('.add_more_city').click(function(e){
            e.preventDefault();
            $(this).before('City Name<input name="data[City][city_name]" maxlength="100" type="text" id="CityCityName" required="required"/>City Latitude<input name="data[City][city_latitude]" step="any" type="number" id="CityCityLatitude" required="required"/></div><div class="input number required"><label for="CityCityLongitude">City Longitude</label><input name="data[City][city_longitude]" step="any" type="number" id="CityCityLongitude" required="required"/></div>');
        });
    });
    </script>

//城市控制器

public function add() {
        if(!$this->Session->check('Auth.User')){
            $this->redirect(array('controller'=>'users','action' => 'login'));      
        }else{
            if(AuthComponent::user('role')=='Admin'){
                if ($this->request->is('post')) {    
                    $this->City->create();
                        if ($this->City->saveAll($this->request->data)) {

                            $this->Session->setFlash(__('City has been added '));
                            return $this->redirect(array('controller' => 'cities','action' => 'add'));
                        } else {
                            $this->Session->setFlash(__('The city has not added. Please, try again.'));
                        } 
                }
            }else{
                $this->Session->setFlash(__('You are not authorized to access it'));
                $this->redirect(array('controller'=>'users','action' => 'login'));
            }
        }
    }

//模型

<?php
    App::uses('AuthComponent', 'Controller/Component');
    class city extends AppModel {
         public $validate = array(
           'city_name' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'A city name is required',
                'allowEmpty' => false
            ),
            'between' => array(
                'rule' => array('between', 3, 100),
                'required' => true,
                'message' => 'City must be between 3 to 100 characters'
            ),
            'valid' => array(
                'rule'    => "/[a-zA-Z0-9]+(?:[ '-][a-zA-Z0-9]+)*/",
                'message' => 'City can only be letters, numbers, comma and hyphen'
            )
        ),

        'city_latitude' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A Latitude is required'
            ),
            'valid' => array(
                'rule'=> '/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)/',
                'message'=> 'Valid latitude is required'
            )

        ),
        'city_longitude' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A Longitude is required'
            ),
            'valid' => array(
                'rule'=> '/\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/',
                'message'=> 'Valid Longitude is required'
            )
        )

    );


        /**
     * Before isUniqueUsername
     * @param array $options
     * @return boolean
     */


    }

?>

我想使用saveAll将多个城市数据保存在一起。在添加更多按钮(java函数)的帮助下触发添加文本输入以添加多个城市的表单。 Cakephp saveAll无效。你能帮我做那个吗?

2 个答案:

答案 0 :(得分:1)

我得到了解决方案。问题出在这个领域。我正在动态更新表单的name参数。

   <?php echo $this->Form->create('City');?>
        <fieldset>
        <?php 
        echo $this->Form->input('city_name', array('name' => 'City[0][city_name]'));
echo $this->Form->input('city_latitude', array('name' => 'City[0][city_latitude]'));
echo $this->Form->input('city_longitude', array('name' => 'City[0][city_longitude]'));
        ?>
        <div class="extra"></div>
        <div class="add_more_city" id="add_more_1"><button class="btn green">Add More</button></div>
        <?php echo $this->Form->submit('Save', array('class' => 'form-submit',  'title' => 'Click here to add the city')); ?>
    </fieldset>
<?php echo $this->Form->end(); ?>





<script>
    $(document).ready(function(){
        $('.add_more_city').click(function(e){
            e.preventDefault();
            var get_id=$('.add_more_city').attr('id');
            var split_id=get_id.split('_');
            var id = parseInt(split_id[2]);
            var id_update =id+1;
            $('.add_more_city').attr('id','add_more_'+id_update);
            $('.extra').before(' <div class="input text"><label for="CityCityName">City Name</label><input name="City['+id+'][city_name]" maxlength="100" type="text" id="CityCityName"/></div><div class="input number"><label for="CityCityLatitude">City Latitude</label><input name="City['+id+'][city_latitude]" step="any" type="number" id="CityCityLatitude"/></div><div class="input number"><label for="CityCityLongitude">City Longitude</label><input name="City['+id+'][city_longitude]" step="any" type="number" id="CityCityLongitude"/></div></div>');
        });
    });
    </script>

答案 1 :(得分:0)

更改字段名称 -

<h:commandLink id="openFrais"
               action="/page.xhtml"
               immediate="true"
               title="#{msgs.open}">

    <div class="sprite sprite-folder"/>

    <f:setPropertyActionListener value="#{collaborateur.collaId}"
                                 target="#{collabHome.collabId}" />
</h:commandLink>