Пример #1
0
 /**
  * Creates a new Packages model.
  * If creation is successful, the response will be a 'success'=true.
  * @return mixed
  *   
  *  $(document).on('submit','#packages-form',function(e) {
  *    e.preventDefault();
  *    var $form = $(this);
  *    $.ajax({
  *        url: '<?php echo y('.urlManager')->createUrl("/module/controller/create-ajax");?>',
  *        dataType: 'json', 
  *        type: 'post',
  *        data: $form.serialize(),
  *        success: function (data){
  *
  *          console.log(data);
  *
  *          $.each($form.serializeArray(), function(index, name) {
  *            $('[name='+name.name+']')
  *              .parent()
  *              .find('#validate-'+name.name)
  *              .remove();
  *          });
  *
  *          if(data.success) {
  *            $form[0].reset();
  *            bootbox.alert(data.message);
  *
  *          } else {
  *
  *            $.each(data.data, function(name, errors) {
  *              $('[name='+name+']')
  *              .parent()
  *              .append($('<p id="validate-'+name+'" class="help-block text-danger">'+errors.join(',<br>')+'</p>'));
  *            });
  *          }
  *        }
  *    });
  *  });
  *
  */
 public function actionCreateAjax()
 {
     y('.response')->format = 'json';
     $model = new Packages();
     $model->attributes = $_REQUEST;
     if ($model->save()) {
         return ['success' => 1, 'data' => $model, 'message' => y('app', 'Record created!')];
     } else {
         return ['success' => 0, 'data' => $model->getErrors()];
     }
 }