Yii2 - how to change structure of existing widgets? - php

Using this code:
<?= $form->field($model, 'username') ?>
would return a specific HTML containing a set of divs, text etc.
Let's say I don't want to print out the label "Username" Above the input field, and maybe I want to add some HTML code for this input (maybe a div next to the input field..) what is the best way to do this?
Do I need to edit the class/file that is actually responsible for the output of this function?

The field above the 'username' is label you can change it by chaining lable() function
<?php $form->field($model, 'username')->textInput()->label('Add your lable here') ?>
Please click this link for more detail

Related

Yii2, Form and field names

Simple question but no solution yet. As we know
<?php $form = ActiveForm::begin(['method'=>'get']); ?>
<?= $form->field($formFilter, 'keyword')
->textInput(['placeholder' => \Yii::t('', 'keyword')]); ?>
...
will create simple form and input fields. Of course we will load $_POST data in action like
if ($this->isPost() && $formFilter->load($this->post())) {
if ($formFilter->validate()) {
...
If we will look in $_POST we will see something like FormFilter[keyword] as name of field. So question is, how can I change it? I need (i think) somehow change in in form\model not in view, because we need proper loading in action.
Where it will be used? Any GET form will show ugly url with class names, for example using simple action and models we will get FormFilter[keyword] but I want change it to keyword, so url will be more understandable than 'long field names'.
Anyone know how to deal with this?
Sorry, later I found solution, I think it will help not just me...
Simple one is to redefine formName() method in our form/model. Using formName() we can even change it what ever we need or disable at all if will set such one
public function formName()
{
return '';
}
So, if forName() returns empty string we will get url :
http://site/items?keyword=&locationID=&employmentType=&educationLevel=&salaryMin=
Default one will be:
http://site/items?FormVacanciesFilter[keyword]=&FormVacanciesFilter[locationID]=6&FormVacanciesFilter[employmentType]=&FormVacanciesFilter[educationLevel]=&FormVacanciesFilter[salaryMin]=
You can change it per field in the view, e.g. I have a form based on yii\base\DynamicModel where I need to control the field names, and, for example:
echo $form->field($model, 'test')->hiddenInput(['name' => 'test'])->label(false);
will output:
<div class="form-group field-dynamicmodel-test">
<input type="hidden" id="dynamicmodel-test" class="form-control" name="test" value="{value of $model->test}">
<p class="help-block help-block-error"></p>
</div>

Yii2 Form Fields

How can I edit the _search view (which is implemented by Yii2 Crud Generator), so that I can make two form fields on the same line?
(Note that I have resized the form fields to 500px so that they can fit one line).
Also, I would like the 'Search' and 'Reset' buttons to be on the same line too.
You can use options and assign eg: a proper twitter-bootstrap grid width
this way :
<?php echo $form->field($model, 'your_filed', ['options' => ['class' => 'col-md-4', ]]) ?>

Reference a hidden defined form field in PHP

I am trying to create a template commenting system using Dreamweaver for a site. I have the form setup to submit the webpage and corresponding text to a mysql db. the webpage value is a hidden form field.
The form submits to the db okay but I want to create a repeating view for the comments. How do I reference the hidden form field so I can use it where "WHERE webpage=""" is called?
UPDATE: By repeating view I mean:
<?php do { ?>
<p><?php echo $row_InsertRecords['text']; ?></p>
<?php } while ($row_InsertRecords = mysql_fetch_assoc($InsertRecords)); ?>
My problem is I need to make partial edits to the PHP for that template so I can retrieve comments specific to the child page but Dreamweaver won't let me. It either propogates ALL of the PHP or none of it.
For a dynamic commenting system that uses the templates in Dreamweaver, you can use the following code in your dwt file:
$fname=basename($_SERVER['PHP_SELF']);
$query_ViewRecords = "SELECT * FROM commentsDB WHERE id='".$fname."'";
id can match a hidden value in your comment form defined as such:
<input type="hidden" name="IDField" value=<?php echo "\"$fname\""; ?>/>
The code above was part of code generated by Dreamweaver's DB functions that were later edited by me to add the WHERE clause. This way you can generate HTML that matched each child page when it is created by the template. Make sure codeOutsideHTMLIsLocked parameter is set to true for these changes to propagate to the child pages.

Cakephp hidden input field

So i have this field i want to keep hidden in my form.
For this purpose i have tried the following:
<?php echo $this->Form->input('group_id', array('hiddenField' => true, 'value'=> 2)); ?>
I also tried:
<?php echo $this->Form->input('group_id', array('options' => array('hiddenField'=> 'true'), 'value'=>2 )); ?>
How ever i still see the input field..
What am i doing wrong?
You misread the documentation, I assume.
hiddenField is to enable/disable specific hidden fields for specific form fields.
You are either looking for
$this->Form->hidden('group_id')
or
$this->Form->input('group_id', ['type' => 'hidden']);
I usually only use the latter.
See http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
BUT - that said - you shouldnt actually use either one of those. And omit any fields that serve no real purpose for the view and its form.
Instead you should inject those fields into the data array prior to saving.
See http://www.dereuromark.de/2010/06/23/working-with-forms/
If you are looking to add a hidden field that uses a related second data array that will not be passed via post or put by default, you can use this to pass it:
echo $this->Form->hidden('Group.name');
This is useful for echoing out edit page titles when the post or put encounters an error. A dynamic title can lose Group.name data array when your form is set up such as this:
<h1>Edit Group - <?php echo h($this->request->data['Group']['name']); ?></h1>
For data that is to be saved to db however, follow Mark's suggestion above.
Try following code in cakephp 3 to set hidden field
<?php
echo $this->Form->hidden('name');
?>

How can I keep HTML styling in a php form storing data in MySQL?

so I have a box where a user enters some data "a description" and it stores in MySQL.
However, html tags such as <img src="">, <a href="">, etc. do not store there in the 'description' field. How can I allow certain HTML codes to be passed through to MySQL?
This is what the description input box looks like:
<?=form_textarea(
'description',
$Channels->description,
'class="field" style="width:306px; height: 70px; margin-left:5px;"'
)?>
This is where the form gets passed:
$this->form_validation->set_rules(
'description',
lang('user_edit_channel_description'),
'trim|required|strip_tags|xss_clean'
);
And then posted to MySQL here:
$rows['description'] = $this->input->post('description');
You will need to use the form of the strip_tags function with the second parameter, which allows you to specify which HTML tags are allowed. As explained in this post, you need to create a callback function to call because the two-parameter version of strip_tags cannot be directly used in set_rules(.
You'll want to get rid of strip_tags from the validation rules, since that removes all HTML tags. Or, ever better, call the function separately using a second parameter to define what tags you want to allow.

Categories