Validating content before save in yii2 - php

I am using yii2 for a weigh bridge project
Upon create, the user is redirected to view but my controller doesn't validate the information in such a way that even if data is not entered in the form fields a user is always redirected to view.
How can I implement the validation property
Controller code:
public function actionCreate()
{
$model = new TruckWeight1();
if ($model->load(Yii::$app->request->post()) ) {
$model->time_recorded =date('H:i:s');;
$model->recorded_by =
$model->recorded_date = date('Y-m-d');
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

try this
public function actionCreate()
{
$model = new TruckWeight1();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->time_recorded =date('H:i:s');;
$model->recorded_by =
$model->recorded_date = date('Y-m-d');
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
for more on validation validation

Related

A pop up validation form in yii2

I am trying to get who took a book from a database so i want to put admission number so that i can get the details of the stodent; return date etc.
public function actionCreate()
{
$model = new BooksReturn();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
so you need to create SqlDataProvider , that will take admission number and check it with database with Yii::$app->user->identity->(db with student id or something) and then you can return it as a result? or I can't get what you want

how to validate a specific field in yii2

I need to do some stuff before saving my data in DB.
the problem is that the data changes during the validation and save process.
here is some code:
public function actionCreate()
{
$model = new Customers();
if ($model->load(Yii::$app->request->post()) && $model->validate('special_field')) {
// do some stuff
// data changes here
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
but $model->validate('special_field') function does not work
I've tried this and it worked properly
if ($model->load(Yii::$app->request->post())) {
// your stuff
if ($model->validate('special_field')) {
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
$errors = $model->errors;
var_dump($errors);
die();
}
}

Sending SMS in yii2

I'm using two models in one controller. one of them is database model (model) another model is for sending sms (smsModel).
I have problem in smsModel.
I got this error in my result:
Class 'fcadmin\models\SoapClient' not found
How can I fix it?
My controller:
public function actionCreate($id) {
$model = new Requestresult();
$smsModel = new SmsSender();
$request_model = Request::findOne(['id' => $id]);
$model->CodeKargah = $request_model->CodeKargah;
$model->month = $request_model->month;
$model->trackingCode = $request_model->trackingCode;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$smsModel->sendSms('09193452126', 'sdf');
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
smsModel:
public function sendSms($to, $text) {
$options = [
'login' => 'myusername',
'password' => 'mypassword'
];
$client = new SoapClient('http://sms.hostiran.net/webservice/?WSDL', $options);
$messageId = $client->send($to, $text);
sleep(3);
return ($client->deliveryStatus($messageId));
}
You need to read up about namespaces. If you're in a namespace and don't tell PHP that you want to use the global namespace, it will look for classes of name x in the current namespace.
In your case you need to be using new \SoapClient.

Yii2 : Bad Request (#400) Missing required parameters: id

My controller :
public function actionCreate()
{
$model = new CreateBookings();
if ($model->load(Yii::$app->request->post()))
{
$imageName = $model->primary_name;
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);
$model->id_image = 'uploads/'.$imageName.'.'.$model->file->extension;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else
{
return $this->render('create', [
'model' => $model,
]);
}
}
Getting this error on submitting my form, dont know what's wrong with it..
Tried with $model->save(false); ..but not working as well
Try with getPrimaryKey() method:
public function actionCreate()
{
$model = new CreateBookings();
if ($model->load(Yii::$app->request->post()))
{
$imageName = $model->primary_name;
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);
$model->id_image = 'uploads/'.$imageName.'.'.$model->file->extension;
if($model->save())
{
$lastInsertID = $model->getPrimaryKey();
return $this->redirect(['view', 'id' => $lastInsertID]);
}
else
{
// print_r($model->getErrors()); => check whether any validation errors are there
}
} else
{
return $this->render('create', [
'model' => $model,
]);
}
}

Yii2 submit data with link

I'm writing simple shopping app and I have some issues with the add to cart link
I'm passing al the same parameter that were on Yii's default Crud form via get and i want to save them in database. Sad thing is when I tried to simply get 'post' to 'get' like this: its not working
if ($model->load(Yii::$app->request->get()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
I think I am missing something very simple but I'm rather new to this so any help will be appreciated.
Oh well i fixed that :D
public function actionCreate()
{
$model = new Koszyk();
$model->setAttributes($_GET);
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
else {
$m =$_GET;
return $this->render('create', [
'model' => $model,'param'=>$m
]);
}
}

Categories