Answer the question
In order to leave comments, you need to log in
How to make the ActiveRecord::load() method work in Yii2?
Hello! I have a question: I'm using Yii2 and I'm trying to do a trivial save of data from a form to a database using ActiveRecord like this:
$order = new Order();
if($order->load(Yii::$app->request->post()) && $order->validate())
{
$order->save(false);
$this->redirect(['orders']);
}
class Order extends ActiveRecord
{
const STATUS_NEW = 'na_utv';
const STATUS_DOIT = 'na_isp';
const STATUS_DONE = 'ready';
const STATUS_DECLINE = 'declined';
public static function tableName()
{
return 'orders';
}
public function rules()
{
return [
[['status', 'consume_type', /*'comment',*/ 'count', 'cost'], 'required', 'message' => 'Это поле должно быть заполнено'],
['technic_id', 'match', 'pattern' => '/null.*/', 'not' => true, 'message' => 'В поле "обслуживаемая техника" нужно выбрать принтер, а не подразделение'],
['user_fio', 'required', 'message' => "Введите ФИО заказчика"]
];
}
public function attributeLabels()
{
return [
'technic_id' => 'Обслуживаемая техника',
'status' => 'Статус заказа',
'consume_type' => 'Тип расходного материала',
'count' => 'Количество',
'user_fio' => "ФИО заказчика",
'comment' => 'Комментарий к заказу',
'cost' => 'Стоимость расходного материала'
];
}
}
Answer the question
In order to leave comments, you need to log in
if you load data into the model using the load method, then only those attributes that are considered safe will be loaded, and specifically in your case, all the attributes that are specified in the rules. You can solve this problem by assigning the safe validator to the Comment attribute.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question