Cakephp 3将输入字段的隐藏值设置为其他输入字段的值

时间:2017-09-21 21:10:06

标签: cakephp input

好的,我在cakephp 3中有一点形式

<?= $this->Form->create($program, ['type'=>'file']) ?>
    <fieldset>
    <?php
        echo $this->Form->input('title',        ['label' => 'Program Title']);
        echo $this->Form->input('subtitle',     ['label' => 'Subtitle or Short Summary (15 words or less)']);
        echo $this->Form->input('start_date',   ['type'=>'date']);
        echo $this->Form->input('end_date',     ['type'=>'date']);
        echo $this->Form->input('slug', ['type' => 'hidden']);   

我希望在提交时将隐藏的输入表单字段slug设置为与输入字段标题相同的值。

3 个答案:

答案 0 :(得分:1)

结束从表单中删除字段并在控制器中处理它,就像这样 $program = $this->Programs->patchEntity($program, $this->request->data); $program->slug=$program->title;

我甚至用 - 替换空格,使文本更低原因并删除不需要的字符

$program->slug = strtolower(str_replace(' ', '-', $program->slug)); 
$remove = array("`","!","@","#","$","%","^","&","*","(",")","_","+","=","{","}","[","]","|",":",";",",",'"',"<",">",".","/");
$replace   = array("");
$program->slug = str_replace($remove, $replace, $program->slug);

仍在改进代码,但就像魅力一样

答案 1 :(得分:0)

如果您要将字符串转换为slug,我建议您Inflector

<PreferenceCategory android:title="HPWhatsApp 4.0" android:key="cat_wa">
    <com.whatsapp.preference.WaPreference android:icon="@drawable/ic_preguntas" android:title="HPWhatsApp WEB" android:key="settings_faq" />
    <com.whatsapp.preference.WaPreference android:icon="@drawable/ic_actualizaciones" android:title="@string/updatess" android:key="updates_key" />
    <com.whatsapp.preference.WaPreference android:icon="@drawable/ic_Thanks" android:title="Donar" android:summary="Donar al desarrollador" >
        <intent android:action="android.intent.action.VIEW" android:data="https://paypal.me/Hectorc4rp" />
        <!--
          Close the com.whatsapp.preference.WaPreference here !
        -->
    </com.whatsapp.preference.WaPreference>
    <PreferenceScreen android:icon="@drawable/ic_9" android:title="Contactar al desarrollador" android:summary="Habla con Héctor Paez, creador de HPWhatsApp" >
        <intent android:action="android.intent.action.VIEW" android:data="https://api.whatsapp.com/send?phone=543814805749" />
    </PreferenceScreen>
</PreferenceCategory>

答案 2 :(得分:0)

我认为应该使用var event = { 'summary': 'Google I/O 2015', 'location': '800 Howard St., San Francisco, CA 94103', 'description': 'A chance to hear more about Google\'s developer products.', 'start': { 'dateTime': '2015-05-28T09:00:00-07:00', 'timeZone': 'America/Los_Angeles' }, 'end': { 'dateTime': '2015-05-28T17:00:00-07:00', 'timeZone': 'America/Los_Angeles' }, ... }; 实体中的mutator方法和Program表类中的beforeSave事件在模型中设置slug。

Slug应该是唯一的,所以如果重复slug,那么附加的数字应该附加到slug。

在我自己的CMS中,我使用类似的东西(代码适合您的模型):

Programs

表格方法

namespace App\Model\Entity;

use Cake\ORM\Entity;
use Cake\Utility\Text;
use Cake\ORM\TableRegistry;

class Program extends Entity
{
    protected function _setSlug($value = '') {

        $copy = $slug = Text::slug(mb_strtolower($value));
        $table = TableRegistry::get('Programs');
        $i = 1;

        do {
            $result = $table->find('all' , [
                    'conditions' => [
                        'slug' => $copy
                    ]
                ])
            ->first();

            if(!is_null($result)) {
                $copy = $slug.'-'.$i;
                $i++;
            }
        } while(!is_null($result));

        return $copy;
    }
}