烈火人改变弹头

时间:2018-07-29 13:57:51

标签: php pyrocms

我尝试在pyrocms中创建简单的模块,现在我创建公共形式,在其中输入名称和描述,但是slug是skipField并从名称生成。此时,我需要更改“子代码生成器”以添加后缀,例如'-'.uniqid()

我的代码视图如下:

public function save(AdvertFormBuilder $form)
{


    $form->skipField('slug');
    $form->setOption('redirect', 'dodaj-ogloszenie/potwierdzenie');
    $form->setOption('redirect_error', 'dodaj-ogloszenie');
    $form->build();
    $form->validate();


    if($form->hasFormErrors()){
        return redirect($form->getOption('redirect_error'));
    }else{
        $form->saveForm();
        return redirect($form->getOption('redirect'));
    }
    ...

现在,我需要像$_POST['name'].'-'.uniqid()一样更改此条。我尝试通过:

    $form->getFormField('slug')->setValue($form->getFormField('name')->value.'-'.uniqid());
    dd($form->getFormField('slug'));exit;

但不起作用:(


Update1

流文件:

/**
 * The stream definition.
 *
 * @var array
 */
protected $stream = [
    'slug' => 'adverts',
     'title_column' => 'name',
     'translatable' => true,
     'trashable' => false,
     'searchable' => false,
     'sortable' => false,
];

/**
 * The stream assignments.
 *
 * @var array
 */
protected $assignments = [
    'name' => [
        'translatable' => true,
        'required' => true,
    ],
    'slug' => [
        'unique' => true,
        'required' => true,
    ],
    'category' => [
        'required' => true,
    ],

];

和表单构建文件:

/**
 * The form fields.
 *
 * @var array|string
 */
protected $fields = [
    'name'        => [
        'translatable' => true,
        'required'     => true,
        'unique'       => true,
    ],
    'slug'        => [
        'required' => true,
        'unique'   => true,
        'config'   => [
            'slugify' => 'name',
        ],
    ],
    'category' => [
        'required' => true,
    ],
];

1 个答案:

答案 0 :(得分:0)

模块迁移:

use Anomaly\Streams\Platform\Database\Migration\Migration;
use Defr\AdvertsModule\Category\CategoryModel;

class DefrModuleAdvertsCreateAdvertsFields extends Migration
{
    /**
     * The addon fields.
     *
     * @var array
     */
    protected $fields = [
        'name'     => 'anomaly.field_type.text',
        'slug'     => [
            'type'   => 'anomaly.field_type.slug',
            'config' => [
                'slugify' => 'name',
                'type'    => '-',
            ],
        ],
        'category' => [
            'type'   => 'anomaly.field_type.relationship',
            'config' => [
                'mode'    => 'lookup',
                'related' => CategoryModel::class,
            ],
        ],
    ];
}

广告流迁移:

class DefrModuleAdvertsCreateAdvertsStream extends Migration
{
    /**
     * The stream definition.
     *
     * @var array
     */
    protected $stream = [
        'slug'         => 'adverts',
        'title_column' => 'name',
        'searchable'   => true,
        'trashable'    => true,
    ];

    /**
     * The stream assignments.
     *
     * @var array
     */
    protected $assignments = [
        'name' => [
            'required' => true,
        ],
        'slug' => [
            'required' => true,
            'unique'   => true,
        ],
        'category',
    ];
}

类别流迁移:

class DefrModuleAdvertsCreateCategoriesStream extends Migration
{
    /**
     * The stream definition.
     *
     * @var array
     */
    protected $stream = [
        'slug'         => 'categories',
        'title_column' => 'name',
    ];

    /**
     * The stream assignments.
     *
     * @var array
     */
    protected $assignments = [
        'name' => [
            'required' => true,
        ],
        'slug' => [
            'required' => true,
            'unique'   => true,
        ],
    ];
}

键入name时,应该自动输入该标签。