How to use Yii 2 Lajax ToggleTranslate - yii2

I searched in documentation how to turn on ToggleTranslate on Yii 2 but with no success. I echoed widget
<?= \lajax\translatemanager\widgets\ToggleTranslate::widget(); ?>
but it does not apper. Then I went to source code and got this:
if (!Yii::$app->session->has(Module::SESSION_KEY_ENABLE_TRANSLATE)) {
return;
}
I commented it and my button appeared. But button is not working. So my question is how to properly (by proper flow, by proper guide) configure it and run it?

Site controller I modified:
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
/** set session key for appearing translate button */
if(array_key_exists('admin', Yii::$app->authManager->getAssignments(Yii::$app->user->id)))
\Yii::$app->session->set('frontendTranslation_EnableTranslate',1);
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}

Related

In Symfony4 update function not working,after values fetch from database

I'm new to Symfony.I made a form with
insert,delete,view,update. In view page i added a edit button which is used
to update the values.when i submit the form after change the values in form
values will not updated in output.Please share your thoughts.
Thank You...
here i attached my controller code
/**
* #Route("/update/{id}", name="update")
*/
//edit function
public function edit($id,Request $request,Ramsurath $ramsurath) :Response
{
$form = $this->createForm(RamsurathType::class, $ramsurath);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('index');
}
return $this->render('ramsurath/update.html.twig',
['ramsurath'=>$ramsurath,'form' => $form->createView()]);
}
See the Symfony Documentation for Processing Forms.
You are missing the call to $form->getData() before flushing:
/**
* #Route("/update/{id}", name="update")
*/
//edit function
public function edit($id,Request $request,Ramsurath $ramsurath) :Response
{
$form = $this->createForm(RamsurathType::class, $ramsurath);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$ramsurath = $form->getData(); // this is the line you are missing
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('index');
}
return $this->render('ramsurath/update.html.twig',
['ramsurath'=>$ramsurath,'form' => $form->createView(),
]);
}

Yii2, on login from frontend redirect to backend?

After login from frontend in yii2 I want, page should redirect to backend index page.
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this-> render('commercial');
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$this->layout = 'noBar';
return $this-> render('home'); //backend/index should be open
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
I have tried in many ways (like Url manager n all) but did not get the required result?
Instead this line:
return $this-> render('home'); //backend/index should be open
Write:
return $this->redirect('https://yoursite.com/backend/site/index');
Or you can make backendUrlManager in config like :
Yii2 Links between Frontend and Backend (advanced template)

Yii2 model error disappears in the view, why?

I have a view, which is including a form. If an attribute of the model is empty in the DB I see the form so I can immediately upload a file, which at the same time updates the attribute in the DB, so next time I see the data what I want according to what I've uploaded.
This is my controller:
public function actionView($id) {
$model = $this->findModel($id);
...
if ($model->load($_POST)) {
$this->actionUpload($id);
}
return $this->render('view', [
'model' => $model,
]);
}
public function actionUpload($id) {
$model = $this->findModel($id);
if ($model->upload()) {
return $this->redirect(Url::previous());
} else {
# ***
return $this->render('view', [
'model' => $model,
]);
}
}
*** If validation fails, at this point I can see the error, but in the view not any more, because it's empty. How can it be? It should be there, shouldn't it? Somewhere I'm doing a mistake but I have no clue how.
My View:
if ($model->attr) {
echo $model->attr;
} else {
echo $this->render('_upload', [
'model' => $model,
]);
}
_upload:
echo $form->field($model, 'uploadedFiles[]')->fileInput([...
echo $form->errorSummary($model);
(submitButton)
Can you please tell me at which point my error can be disappearing?
Merge both controller actions into one action. From first action you are calling the second action which does redirect on success - so it would never return back to first action.

Yii2: why does the layout not get shown?

I have a controller with a working action:
class ConfigurationController extends Controller {
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
public function actionView() {
$myModel = ...
$this->render('view', ['model' => $myModel]);
}
}
All seems to be fine, however the layout file which is app/views/layout/main.php does not get shown. There is no special configuration about the layout. What could be wrong?
The main reason: I did not use the return statement. So the correct action is:
public function actionView() {
$myModel = ...
return $this->render('view', ['model' => $myModel]);
// ^^^^^^
}
More info can be found in the guide.
Note: Usually an empty page would be shown. But I also had a <?php $form = ActiveForm::begin(); ?> without an <?php ActiveForm::end(); ?> in the view file. This caused a partial rendering somehow (caused no exception). So I needed to correct this as well.
I'm just sharing my problem and what I've found out so if anyone else has a similar effect may be reminded that the return statement must not be forgotten.

class not found even when it exits on Yii2

I'm making my first application using Yii and I have the next code:
public function actionCreate()
{
$model = new User();
$singUp = new \frontend\models\SingupForm;
if ($singUp->load(Yii::$app->request->post()) && $singUp->save()) {
$model = ModelName::findOne(['id' => $singUP->id]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', [
'model' => $model,
'singUp' => $singUp
]);
}
}
And I'm having the next error message when I try to go to that method:
Class 'frontend\models\SingupForm' not found
But I have the file saved on the directory as it is showed in the screenshot I attached. Additionally I added all the models folder of the Frontend on the controller I am using:
use Yii;
use \frontend\models;
use common\models\User;
use backend\models\search\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\PermissionHelpers;
So I don't know what I'm doing wrong. Please help
Screenshot
There is a typo in your code.
Please Change frontend\models\SingupForm to frontend\models\SignupForm.