Answer the question
In order to leave comments, you need to log in
Yii2 model form how to manually create a field?
Hello everyone!)
In general, there is a news form, a simple form where there is a Title, Description, and metatags, and it became necessary to make a url field, but for it to be automatically created:
It should not be in the layout, it should simply be equal to the Title field.
Here's what's there:
public $title;
public $url;
public $text;
public $keywords;
public $description;
public function rules()
{
return [
[['title', 'url', 'text', 'keywords', 'description'], 'filter', 'filter' => 'trim'],
[['title', 'url', 'text'], 'required'],
[['url'], 'filter', 'filter' => 'strtolower'],
[['url'], 'filter', 'filter' => [$this, 'normalizeUrl']]
];
}
public function normalizeUrl($value)
{
return strtr($value, array(' ' => '-', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'e', 'ж' => 'j', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'shch', 'ы' => 'y', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', 'ъ' => '', 'ь' => ''));
}
public $url = $this->title;
Answer the question
In order to leave comments, you need to log in
Good afternoon.
Do you want to make something like Slug?
Then use the built-in SluggableBehavior class .
When using the class, everything is done automatically and there will be no need to sculpt a "rusty fox")))
public function normalizeUrl($value)
{
return strtr($value, array(' ' => '-', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'e', 'ж' => 'j', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'shch', 'ы' => 'y', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', 'ъ' => '', 'ь' => ''));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question