Yii2 - create radioList not enclosed by label - forms

How do you create an activeRadioList in yii2 where the checkboxes are not wrapped in labels? Ie. The label and input are adjacent to each other.
The following creates a list of radio buttons where each input is wrapped in labels:
<?= $form->field($model,'myattribute')->radioList(['n'=>'No','y'=>'Yes']) ?>
You can create one radio button that is not wrapped in a label by setting the second argument to false:
<?= $form->field($model,'myattribute')->radio(null,false) ?>
But how do you do this for a list? (FYI. I need this to work with the materializedcss framework in case your wondering).

you just need to set label property false
<?= $form->field($model,'myattribute')->radioList(['n'=>'No','y'=>'Yes'])->label(false); ?>
Updated answer
So in that case you need to use custom logic as follows.
<?=
$form->field($model, 'myattribute')
->radioList(
['n'=>'No','y'=>'Yes'], [
'item' => function($index, $label, $name, $checked, $value) {
$return = '<input type="radio" name="' . $name . '" value="' . $value . '">';
$return .= '<i></i>';
$return .= '<span>' . ucwords($label) . '</span>';
return $return;
}
]
)
->label(false);
?>

Related

Yii2 how to make submit button from html a

<?= '<li>'
. Html::beginForm(['/site/logout'], 'post')
. Html::submitButton(
'Exit(admin)',
['class' => 'btn btn-warning']
)
. Html::endForm()
. '</li>'?>
This code is working, but I want to change submit button to html::a. When I try to do it, it's not working.
It is not recommend to use a link as submit button, anyhow if you want, here is code
<?php echo Html::a('Exit(admin)',$url=false,['class'=>'btn btn-warning','onclick'=>'submitForm()']); ?>
in javascript
function submitForm(){
document.getElementById('FormId').submit;
}

Button submit's value not transmitted in POST

I am using Yii2 and I want to create comment functionality on post using Pjax widget.
The comment windows displays all the existing comments for the post adding an edit button to the ones that belongs to the connected user in order to permit changes.
Under this list of comments,there is a form including a textarea and a submit button to allow comment creation.
Each time the user click on an already existing comment 's edit button, a form is displayed to allow update.
How all this actually runs and the trouble I meet:
I can update an existing comment an infinite number of times, and it is what I want.
If, just after refreshing the view post page, I enter a new comment, it
is correctly submitted. But I cannot enter an other one and I want to be able to post more than one. The second time, it appears that the POST contains the content of the comment but nothing
regarding the submit button. Thus I cannot recognize which button has
been clicked.
The same thing happens after I have changed an
existing comment i.e. I cannot create a new comment for the same
reason – nothing about the submit button in the POST.
My question
How comes the submit button is transmitted in the POST only if it is the first one used and it is the first time it is used after a refresh of the page?
Here are the relevant code parts
1-Controller
/**
* display a post
*#param integer $id
*
* return a view
*/
public function actionView ($id){
$post = $this->findModel($id);
$comment=$this->manageComment($post);
return $this->render('view', [
'post_model' => $post,
'comment_model' => $comment,
]);
}
/*
* deals with the post according to the submit value
* if submitted and return the comment under treatment
* otherwise – not submitted – return a new comment
*
*#param $post the post that holds the comments
*
* return a Comment model
*/
protected function manageComment($post)
{
$comment=new Comment;
if(isset($_POST['Comment']) )
{
switch ($_POST['Submit']) {
case 'create':
$comment->attributes=$_POST['Comment'];
if($post->addComment($comment,false))
{
//la création a réussi
if($comment->status==Comment::STATUS_PENDING)
Yii::$app->session->setFlash('success',
Yii::t('app','Thank you for your comment.
Your comment will be visible once it is approved.'));
//on renouvelle le commentaire pour éviter d'avoir
//un bouton update (mise à jour)
return new Comment;
}
break;
case 'update':
$comment=Comment:: find()
->where(['id' => $_POST['Comment']['id']])
->one();
if($comment->attributes=$_POST['Comment']){
if($post->addComment($comment,true))
{
//update successful
if($comment->status==Comment::STATUS_PENDING)
{Yii::$app->session->setFlash('success',
Yii::t('app','Thank you for your comment.
Your comment will be visible once it is approved.'));}
$comment= new Comment;
return $comment;
} else {
//la mise à jour a échoué
$comment= new Comment;
return $comment;
}
}else{echo'load failed'; exit();}
break;
case 'edit':
// echo $_POST['Comment']['id']; exit();
$comment->id = $_POST['Comment']['id'];
return $comment;
break;
default:
}
}
//creation successful
return $comment;
}
2-In Post view
<!--comment_model is passed by PostController-->
<?= $this->render('#app/views/comment/_create-form',
['model' => $comment_model, 'post' => $post_model]); ?>
3 - the _create-form view
<div class="comment-form">
<?php Pjax::begin([ ]);?>
<!-- this bloc must be part of the Pjax in order for the new
comment to appear immediately -->
<div class="comment">
<h4 ><?= Yii::t('app','Comments by users')?></h4>
<div class="comments">
<?php $comments= Comment::find()
->where (['post_id' => $post->id ])
->andWhere(['status' => true])
->all();
foreach($comments as $com){
$identity= User::findIdentity($com->user_id);
$firstname=$identity->firstname;
familyname=$identity->familyname;
$date= Utils::formatTimestamp($com->created_at);
$txt1 = Yii::t('app','Posted by ');
$txt2 = Yii::t('app',' on ');
$text_header =$txt1. $firstname.' '.$familyname.$txt2.$date;
//here we check that $model->id is defined an equal to $com->id
//to include edition form
if (isset($model->id) && ($model->id == $com->id) )//
{
// echo Yii::$app->user->identity->id. ' '.$com->user_id; exit();
echo $this->render(
'_one-comment',(['com' => $com,
'text_header' => $text_header,
'submit_btn' => false,
'with_edit_form' => true]));
} else
{
$submit_btn=false;
if (Yii::$app->user->identity->id == $com->user_id) $submit_btn=true; else $submit_btn=false;
echo $this->render('_one-comment',(['com' => $com, 'text_header' => $text_header, 'submit_btn' => $submit_btn,'with_edit_form' => false]));
}
}?>
</div>
<!--this div should be between Pjax::begin and Pjax::end otheswise it is not refreshed-->
<div id="system-messages" >
<?php foreach (Yii::$app->session->getAllFlashes() as $type => $message): ?>
<?php if (in_array($type, ['failure','success', 'danger', 'warning', 'info'])): ?>
<?= Alert::widget([
'options' => ['class' => ' alert-dismissible alert-'.$type],
'body' => $message
]) ?>
<?php endif ?>
<?php endforeach ?>
</div>
<?php
$form = ActiveForm::begin([
'options' => ['data' => ['pjax' => true]],//'id' => 'content-form'
// more ActiveForm options
]); ?>
<?= $form->field($model, 'content')
->label('Postez un nouveau commentaire')
->textarea(['rows' => 6]) ?>
<?= Html::submitButton('Create comment',
['content' => 'edit','name' =>'Submit','value' => 'create']) ?>
<?php
ActiveForm::end();?>
</div>
<?php Pjax::end()
?>
</div>
4 - the _one-comment view
<div class="one-comment">
<div class="comment-header">
<?php echo $text_header;?>
</div>
<div class="comment-text">
<?php echo $com->content;?>
</div>
<div class="comment-submit">
<?php
if ($with_edit_form)
{
// echo "with edit ".$com->id; exit();
echo $this->render('_update-one-comment',(['com' =>$com]));
} else
{
if($submit_btn){
$form = ActiveForm::begin([
'options' => [ 'data' => ['pjax' => true]],//'name' => 'one','id' => 'com-form-'.$com->id,
// more ActiveForm options
]); ?>
<?= $form->field($com, 'id')->hiddenInput()->label(false);?>
<div class="form-group">
<?= Html::submitButton('', [
'class' => 'glyphicon glyphicon-pencil sub-btn',
'content' => 'edit',
'name' =>'Submit','value' => 'edit']) ?>
</div>
<?php
ActiveForm::end();
}
}?>
</div>
</div>
5 the _update-one-comment form
//echo 'dans update-one-comment';print_r($com->toArray()); exit();
$form = ActiveForm::begin([
'options' => [ 'data' => ['pjax' => true]],//'name' => 'edit-one','id' => 'edit-com-form-'.$com->id,
// more ActiveForm options
]); ?>
<?= $form->field($com, 'id')->hiddenInput()->label(false);?>
<?= $form->field($com, 'created_at')->hiddenInput()->label(false);?>
<?= $form->field($com, 'updated_at')->hiddenInput()->label(false);?>
<?= $form->field($com, 'content')
->label('Mise à jour commentaire')
->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton($com->isNewRecord ?
Yii::t('app', 'Create comment') :
Yii::t('app', 'Update comment'), ['class' => $com->isNewRecord ?
'btn btn-success' :
'btn btn-primary','name' =>'Submit','value' => 'update']) ?>
</div>
<?php
ActiveForm::end();
?>
6- the Post model 's addComment()
public function addComment($comment, $up=false)
{
if(Yii::$app->params['commentNeedApproval'])
{$comment->status=Comment::STATUS_PENDING;}
else {$comment->status=Comment::STATUS_APPROVED;}
$comment->post_id=$this->id;
$comment->user_id=Yii::$app->user->identity->id;
if($up){
$test=$comment->update();
if($test) {//($comment->save(false)){
Yii::$app->session->setFlash('success',Yii::t(
'app','The comment has been succesfully updated.'));
return true;
}
Yii::$app->session->setFlash('failure',Yii::t(
'app','The comment could not be updatded.'));
} else
{ //création
if($comment->save()){
Yii::$app->session->setFlash('success',
Yii::t('app','The comment has been succesfully recorded.'));
return true;
}
Yii::$app->session->setFlash('failure',
Yii::t('app',$up.false.'The comment could not be recorded.'));
}
return false;
}
Why are you counting on the submit button name / value to figure out what you need to do?
$_POST['Comment']['id'] is a much better indicator of what needs to done. If it is there update otherwise create.
Also make sure the model does not allow editing of comments posted by others.
The model should not set flash messages, that is a job for the controller.
"I can update an existing comment an infinite number of times." of course you can, from what you told us there is nothing that would say you should not be able to. And you are not checking to prevent that.
As the submit button's value is not always transmitted in the post (it works with Chromium but not with Firefox), I solved my problem adding a hidden field in the forms and used it instead of the submit button's value.

Moodle block visibility per role on quiz attempt page

I have been following this tutorial successfully and created a small custom block.
I need this block to display on maths quiz/attempt.php pages but I have two problems.
The block displays for the admin user but does not display for students
How do I display the block only on selected quizes i:e maths and not english quizes
CODE:
class block_customfeedback extends block_base {
public function init() {
$this->title = get_string('customfeedback', 'block_customfeedback');
}
public function get_content() {
if ($this->content !== null) {
return $this->content;
}
$form .= "<form action='http://www.remoteserver.com/response.php' method='post'>";
$form .= "<label>Question ID</label> <input name='QuestionID' id='questionid' type='text' />";
$form .= "<label>Quiz Name</label> <input name='QuizName' id='quizname' type='text' />";
$form .= "<label>Your Feedback</label> <textarea name='Feedback' id='feedback' type='text' ></textarea>";
$form .= "<input type='submit' value='Submit' />";
$form .= "</form>";
$this->content = new stdClass;
$this->content->text = $form;
// $this->content->footer = 'Footer here...';
return $this->content;
}
public function applicable_formats() {
return array(
'all' => true
);
}
} // close class
You can make your block visible on quiz attempt page.
Steps:
Go to your quiz settings page.
Click on appearance
Click on show more
Change the value of "Show blocks during quiz attempts" to Yes.
Now login with student and check.

Keep Search Parameters Through Pagination

Somewhat new to Laravel(4.2) and I'm having issues with pagination on my search function. So far I've been able to successfully carry out a search, though in the rare cases it actually goes over onto a second page it resets to simply "?page=2"
Below is the code for the form.
{{ Form::open(array('method' => 'post', 'name' => 'all', 'novalidate' => 'novalidate')) }}
<input type="text" name="srch_lname" class="input-large" value="{{ Input::old('srch_lname', Session::get('srch_lname')) }}" />
<input type="text" name="srch_fname" class="input-large" value="{{ Input::old('srch_fname', Session::get('srch_fname')) }}" />
.
.
.
<?php echo $employees->links(); ?>
And the controller handling the search.
public function getIndex() {
$srch_lname = Session::get('srch_lname');
$srch_fname = Session::get('srch_fname');
$employees = vEmployees::co()->restrictions()
->where('lastname', 'LIKE', $srch_lname . '%')
->where('firstname', 'LIKE', $srch_fname . '%')
->orderBy('lastname')
->orderBy('firstname')
->paginate(10);
return View::make('index')
->with('employees', $employees)
->with('title', 'Users')
->with('pagetitle', 'Employees')
->with('pagedescription', '')
}
public function postIndex() {
if (Input::has('btnSearch')) {
return Redirect::to('/employees')->with('search', 1)
->with('srch_lname', Input::get('srch_lname'))
->with('srch_fname', Input::get('srch_fname'))
I've been trying some other solutions found throughout SO though it either ends up causing issues or bringing me back to the same problem.
Any sort of push in the right direction would be awesome!
Append the search data to the pagination links()
<?php echo $employees->appends(array("srch_lname" => ...))->links(); ?>
http://laravel.com/docs/4.2/pagination#appending-to-pagination-links

fuelphp html email issue

I ran into a problem what i cant solve.
i took a look at the fuelphp documentation, made it that way, but i get an error with passed walue
code
$email_data = array();
//echo Config::get('base_url'). "user/activate/". $user['hash'];
$email = Email::forge();
$email->from('my#email.me', Config::get('site_name'));
$email->to(Input::post('email'), Input::post('first_name') . " " . Input::post('last_name'));
$email->subject('Regisztráció');
$email_data['name'] = "Kedves " . Input::post('first_name'). " " .Input::post('last_name') ."<br><br>" ;
$email_data['title'] = "Üdvözöllek a ".Config::get('site_name')." oldalán" ."<br>";
$email_data['link'] = 'Fiókod mherősítéséhez kérlek kattints ide';
$email->html_body(\View::forge('email/activation', $email_data));
$email->send();
$response->body(json_encode(array(
'status' => 'ok',
)));
the email template file
<?php
print_r($email_data);
?>
and the email sends me out this
Notice!
ErrorException [ Notice ]: Undefined variable: email_data
APPPATH/views/email/activation.php # line 3:
2:3:print_r($email_data);4:?>
could please someone give me a hint what im doing wrong?
$email->html_body(\View::forge('email/activation', $email_data));
Should be
$email->html_body(\View::forge('email/activation', array('email_data' => $email_data)));
Your controller is correct. This is you template that is not correct.
When passing data to the view (template) you pass it as an array.
So you view should be:
...
You Name is : <?php echo $name ?>
You title is : <?php echo $title ?>
...
This is the way it should be done. Not transforming the array into an element of an other array in order to perform a print_r...