Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
7e805864
Commit
7e805864
authored
Oct 31, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added brief description of forms
parent
1dcb63ca
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
93 additions
and
0 deletions
+93
-0
form.md
docs/guide/form.md
+93
-0
No files found.
docs/guide/form.md
View file @
7e805864
Working with forms
==================
The primary way of using forms in Yii is
[
[\yii\widgets\ActiveForm
]
]. It should be preferred when you have a model
behind a form. Additionally there are some useful methods in
[
[\yii\helpers\Html
]
] that are typically used for adding
buttons and help text.
First step creating a form is to create a model. It can be either Active Record or regular Model. Let's use regular
login model as an example:
```
php
use
yii\base\Model
;
class
LoginForm
extends
Model
{
public
$username
;
public
$password
;
/**
* @return array the validation rules.
*/
public
function
rules
()
{
return
[
// username and password are both required
[
'username, password'
,
'required'
],
// password is validated by validatePassword()
[
'password'
,
'validatePassword'
],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*/
public
function
validatePassword
()
{
$user
=
User
::
findByUsername
(
$this
->
username
);
if
(
!
$user
||
!
$user
->
validatePassword
(
$this
->
password
))
{
$this
->
addError
(
'password'
,
'Incorrect username or password.'
);
}
}
/**
* Logs in a user using the provided username and password.
* @return boolean whether the user is logged in successfully
*/
public
function
login
()
{
if
(
$this
->
validate
())
{
$user
=
User
::
findByUsername
(
$this
->
username
);
return
true
;
}
else
{
return
false
;
}
}
}
```
In controller we're passing model to view where Active Form is used:
```
php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
<?php
$form
=
ActiveForm
::
begin
([
'id'
=>
'login-form'
,
'options'
=>
[
'class'
=>
'form-horizontal'
],
])
?>
<?=
$form
->
field
(
$model
,
'username'
)
?>
<?=
$form
->
field
(
$model
,
'password'
)
->
passwordInput
()
?>
<div
class=
"form-group"
>
<div
class=
"col-lg-offset-1 col-lg-11"
>
<?=
Html
::
submitButton
(
'Login'
,
[
'class'
=>
'btn btn-primary'
])
?>
</div>
</div>
<?php
ActiveForm
::
end
()
?>
```
In the code above
`ActiveForm::begin()`
not only creates form instance but marks the beginning of the form. All the content
that is located between
`ActiveForm::begin()`
and
`ActiveForm::end()`
will be wrapped with appropriate
`<form>`
tag.
Same as with any other widget you can specify some options passing an array to
`begin`
method. In our case we're adding
extra CSS class and specifying ID that will be used in the tag.
In order to insert a form field along with its label all necessary validation JavaScript we're calling
`field`
method
and it gives back
`\yii\widgets\ActiveField`
. It it's echoed directly it creates a regular input. In case you want to
customize it you can add a chain of additional methods:
```
php
<?=
$form
->
field
(
$model
,
'password'
)
->
passwordInput
()
?>
// or
<?=
$form
->
field
(
$model
,
'username'
)
->
textInput
()
->
hint
(
'Please enter your name'
)
->
label
(
'Name'
)
?>
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment