Answer the question
In order to leave comments, you need to log in
Why doesn't setAttributes work?
Hello! Somehow it's not very clear to me. Below are the controller and form listings. The bottom line is that when data from the form is passed to the controller via AJAX, safe assignment does not work, validation also does not work (all 3 commented out options do not work). That is, no errors occur, nothing (try, catch for some reason does not catch anything). What is the problem please?
PS wrote a question, and went to install xDebug :-)
$model = new Note;
/**
* AJAX валидация
*/
if (isset($_POST['ajax']) && $_POST['ajax'] === "note-form")
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
/**
* Обработка данных (только по AJAX)
*/
if (isset($_POST['Note']) && Yii::app()->request->isAjaxRequest)
{
// 1 вариант:
//$model->attributes = $_POST['Note'];
// 2 вариант:
//$model->setAttributes($_POST['Note'], false);
// 3 вариант:
// foreach ($_POST['Note'] as $name => $value) {
// $model->$name = $value;
// }
// Валидация раз:
// $model->validate();
// Валидация два:
// CActiveForm::validate($model);
if ($model->save()) {
echo "Запись успешно добавлена!";
} else {
echo "Запись не может быть добавлена!";
}
}
/**
* Вывод формы
*/
else
{
$model = is_null($id) ? $model : Note::model()->findByPk($id);
$this->layout = 'admin';
$this->render('form',array('model'=>$model), false);
}
<?php
/* @var $this NoteController */
/* @var $model Note */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
"id" => "note-form",
"enableAjaxValidation" => true,
)); ?>
<div class="row">
<?php echo $form->label($model,'id_type'); ?>
<?php echo $form->textField($model,'id_type'); ?>
<?php echo $form->error($model,'id_type'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'title'); ?>
<?php echo $form->textField($model,'title'); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'content'); ?>
<?php echo $form->textArea($model,'content'); ?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'addition_date'); ?>
<?php echo $form->textField($model,'addition_date'); ?>
<?php echo $form->error($model,'addition_date'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'is_deleted'); ?>
<?php echo $form->checkBox($model, 'is_deleted'); ?>
<?php echo $form->error($model,'is_deleted'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::ajaxSubmitButton("Применить", Yii::app()->request->requestUri, array(
"type" => "POST",
"data" => "js:jQuery(this).parents('form').serialize()",
"update" => "#out",
"success" => "function(data,textStatus){ alert(data + ' - ' + textStatus); }",
)); ?>
<?php echo CHtml::resetButton('Отмена'); ?>
</div>
<div id="out"></div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question