YII framework _form.php with different contents - view

Is it possible to use same _form.php in two different view files with different content in yii framework? If yes then how to achieve it.

Of course you can do it however both models have to have the same attributes.
in views/pleace1/create.php and update.php
<?php $this->renderPartial('/pleace1/_form', array('model'=>$model)); ?>
in views/pleace2/create.php and update.php
<?php $this->renderPartial('/pleace1/_form', array('model'=>$model)); ?>
in views/pleace1/_form.php change form widget start to:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'stl-' . $this->controller_alias . '-form',
in both controllers where you using form set public controller_alias to model name without prefix (if you use it)

Related

yii2 not rendering to view just refreshing the page

view.php code part:
View Picture
viewgood.php
<?php
echo 'hello';
GoodsController.php
public function viewgood($id = null) {
}
After clicking on button View Picture my page just refreshing instead of going to viewgood.php
What am I doing incorrectly?
I am a begginer in Yii2
Try use
View Picture
in a anchor tag html your need use the complete route, for use shortcut you try use
<?= Html::a('View Picture', ['/goods/viewgood/', 'id' => $good['GoodGallery'][0]['id']], ['class' => 'btn btn-primary']) ?>
view.php
You can use Html:a(), or in your example use Url helper to() function to generate the proper route for your anchor tag.
View Picture
GoodsController.php
public function actionViewgood($id=null){
//fetch the data e.g.
$model = Good::findOne(id);
//Do some extra code checking when no record is found like throw an exception or set a flash error message, etc.
//render the viewgood and pass the data (if needed)
return $this->render('viewgood', [
'model' => $model
]);
}
viewgood.php
<?php
//do what you want with the data passed by the controller. E.g. print the name of the good (if applicable)
echo $model->name;
//your other code ...
?>

Multiple validation on one attribute in Yii

I want to validate an attribute on multiple CValidator classes.
To be more specific, I want an email address to be validated by the email validator, but I also want it to be required.
I can of course define two separate rules, like so:
array('email', 'email'),
array('email', 'required'),
But when I leave the input blank the validation only returns an error saying the field is required, but it doesn't return an error saying it has to be an email address. When I fill in a non-email string, it then returns the email validation error.
I tried to combine the validators in an array, and a comma separated string but that doesn't work. So I guess the only option is to use a custom validation method.
But how can I use the built-in CValidator validators in this method? And how can I build it, that the two rules are validated together at once instead of one at a time?
If I am getting you right you have an issue with error message. I think you can use following approach to show message.
array('email', 'email', 'message' => 'Please provide valid email.'),
array('email', 'required', 'message' => 'Email is required. Please provide valid email.'),
Hope this will help you....
User interface wise this makes little sense to specify that an email is invalid when left empty. You tell them that the email is required when empty or invalid when it isn't empty but not an email. Doing both seems very confusing to me.
I personally just use:
array('email', 'email', 'allowEmpty' => FALSE),
Use the errorsummary() method:
EDIT:
Changed reference to CHTML
http://www.yiiframework.com/doc/api/1.1/CHtml#errorSummary-detail
In your view, add
<?php echo CHtml::errorSummary($model, NULL, NULL, array ('firstError' => false));
...
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
From the documentation
additional HTML attributes to be rendered in the container div tag. A
special option named 'firstError' is recognized, which when set true,
will make the error summary to show only the first error message of
each attribute. If this is not set or is false, all error messages
will be displayed. This option has been available since version 1.1.3.
Note that if you are using Ajax validation then you will get the first error only, next to the fields
Actually model validation returns all errors of validation, but CActiveForm::error method shows only first one for selected attribute. I guess you use now something like this:
<?php echo $form->error($model,'attr'); ?>
but instead should use
<?php if ($model->hasErrors('attr')) : ?>
<?php $errorList = $model->getErrors('attr'); ?>
<?php foreach ($errorList as $error) : ?>
// display error
<?php endforeach ?>
<?php endif ?>
Also you can write your own helper method for displaying all errors for single attribute.
You can use skipOnError property of the validator.
array('email', 'email', 'skipOnError' => false),
array('email', 'required', 'skipOnError' => false),

Drupal 7 - Ajax and Bootstrap - Rerender Main Menu on Login

How can I rerender the main menu in Drupal after an Ajax Login?
I already tried the following (generating the return value in my PHP Ajax Controller):
$tree = menu_tree_all_data('main-menu');
$returnValue = drupal_render(menu_tree_output($tree));
However this will not include the Bootstrap specific class names. It basically just returns a minimalistic menu structure without anything added by the theme.
In the Bootstrap theme, the menu is rendered like this:
<?php if (!empty($primary_nav)): ?>
<?php print render($primary_nav); ?>
<?php endif; ?>
<?php if (!empty($secondary_nav)): ?>
<?php print render($secondary_nav); ?>
<?php endif; ?>
How can I enforce this type of rendering from my module?
Since I am working on an ajax one pager site, I need to update the menu based on a user's role after login. A page reload would destroy the desired look and feel, and is therefore no option.
Currently I am using the following code to process the login:
function wr_pages_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_login_block' || $form_id == 'user_login') {
$form['actions']['submit']['#ajax'] = array(
'callback' => 'wr_login_form_callback',
'wrapper' => 'user-login',
'method' => 'replace',
'effect' => 'fade',
'event' => 'click'
);
}
}
function wr_login_form_callback($form, $form_state) {
if (!form_get_errors()) {
return "<script>adjustViewsAndMenu();</script>";
}
else {
return $form;
}
}
adjustViewsAndMenu() makes changes to the menu, and adds extra parts to the one pager, depending on a user's role, however this function does not use any Drupal rendering, which is most likely not the best way to deal with this problem. Still haven't found out how to rerender a menu with AJAX while using the theme's rendering.

Best practice loading custom language file Codeigniter

What is best practice.
Loading my custom language file in Codeigniter in the view or in the controller and then pass it from the controller to the view?
Controller example:
$data = array( 'registrationSuccess' => $this->lang->line('success'),
'sayHello' => $this->lang->line('hello'));
$this->load->view('view_registration', $data);
You should load the language file in your controller, but you should use it directly in your view.

Getting form data in Yii to a CActiveRecord model works for one model but not for another

I'm getting a submitted form in this way:
$resume->attributes = $_POST['ResumeModel'];
$profile->attributes = $_POST['UserProfile'];
Both CActiveRecord models are correctly populated before this from the corresponding tables, they have the correct data and all.
Both models' data is present on $_POST as modified by the form.
But it seems that the assignment to the attributes property works only for $profile and not for $resume.
If I check their values after that assignment, $profile doesn't get the edits from the form.
Is there something in the definition of the model that can cause that? As far as I can see, both models are similarly implemented
I don't understand why this happens, does anyone have a clue?
Thanks!
The problem is that some fields on the $resume model didn't have any validation rules and weren't declared as safe either, so they couldn't be safely mass assigned.
Reference:
http://www.yiiframework.com/doc/guide/form.model#securing-attribute-assignments
Have you checked the $_POST variables closely? For the mass "attributes" assignment to work the array should be of the form:
$_POST = (
'ResumeModel' => (
'data1' => 'something',
'data2' => 'something else',
),
'UserProfile' => (
'data3' => 'yo ho ho',
'data4' => 'bottle of rum',
)
)
If it looks like this it's wrong:
$_POST = (
'ResumeModel' => (
'data1' => 'something',
'data2' => 'something else',
'data3' => 'yo ho ho',
'data4' => 'bottle of rum',
)
)
To ensure that the form is building the correct $_POST array for each model, make sure that you are passing both the $resume and $profile model into your form View like this:
<?php
$resume=new ResumeModel;
$profile=new UserProfile;
$this->render('yourFormView', array('resume'=>$resume,'profile'=>$profile));
?>
Then, in "yourFormView", make sure that you are creating the form fields appropriately with each model, like so:
<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php echo $form->textField($resume,'data1'); ?>
<?php echo $form->textField($resume,'data2'); ?>
<?php echo $form->textField($profile,'data3'); ?>
<?php echo $form->textField($profile,'data4'); ?>
<?php $this->endWidget(); ?>

Resources