cakePHP: how set error validation to input field manually in controller

后端 未结 6 405
南旧
南旧 2021-01-12 00:15

I want set error validation to input field manually in controller example:

  if ($remainTime < 30) {
      ..... set error validation in here (error: lim         


        
6条回答
  •  孤城傲影
    2021-01-12 00:49

    2019 update for CakePHP3 in YourController.php

    // creane new
    $entity = $this->YouTable->newEntity();
    
    // or get existing from database
    // $entity = $this->YouTable->get($id);
    
    // invalidate fields
    $entity->setErrors('your_field', 'error message');
    $this->set('$entity', $entity);
    

    and in action.ctp

    // create form based on your entity
    echo $this->Form->create($entity);
    // and include your control
    echo $this->Form->control('your_field');
    

    if your form based on table, and for modelless forms first you need to create src/Form/YourForm.php with schema definition and then call setErrors() on YourForm instance from controller.

提交回复
热议问题