Home>
I want to achieve

I want to accurately validate each variable length parameter entered in the form for each element.

What you know
  • There are no defects in the input values ​​of the form
  • Corresponds to the key of the element where the error should be displayed as a result of validationvalidationErrorsThe value is stored in the element in
Current status

The sources of the relevant parts of the view, model and controller are as follows.

  • View
## test.ctp
<? php echo ($this->Form->create ('ProductData', array ('type' =>'post')));?>
<!-Omitted->
<? php echo $this->Form->text ('ProductData.PAYMENT.'. $Key.'. MONEY', array ('default' =>$money,'required' =>false));?>
<? php echo $this->Form->error ('ProductData.PAYMENT.'. $Key.'. MONEY', array ('class' =>'has-error','escape' =>false));?>
<!-Omitted->
<? php echo ($this->Form->end ());?>
  • model
<? php
class ProductData extends AppModel {
    public $useTable ='product_data';
    public $primaryKey ='PRODUCT_ID';
    public $validate = array (
        'PAYMENT' =>array (
            'MONEY' =>array ('rule' =>'checkPaymentMoney','message' =>null),
        ),
    );
    public function checkPaymentMoney ($check) {
        $isError = false;
        foreach ($check ['PAYMENT'] as $key =>$item) {
            if (! preg_match ('/ ^ [0-9] * $/', $item ['MONEY'])) {
                debug ($item);( 1)
                $this->validationErrors ['PAYMENT'] [$key] ['MONEY'] ='Enter in half-width numbers';
                $isError = true;
            }
        }
        if ($isError) {return false;}
        return true;
    }
}
  • Controller
if (! $This->ProductData->validates ()) {
    // Omitted
    debug ($this->ProductData->validationErrors);( 2)
    // Omitted
}

Also,debug ()When using etc., 2 is as expected

array (
    'PAYMENT' =>array (
        (int) 1 =>array (
            'MONEY' =>'Please enter in half-width numbers'
        )
    )
)


Data such as is entered, but it becomes a message in the view'Enter in half-width numbers'Is not displayed.

How can I get the above message to appear in the view?
We would appreciate your help.

  • Answer # 1

    Was self resolved.

    An error statement was displayed by changing the corresponding input form from Form->text to Form->input.
    The modified source is as follows.

    ## test.ctp
    <? php echo ($this->Form->create ('ProductData', array ('type' =>'post')));?>
    <!-Omitted->
    <? php echo $this->Form->input ('ProductData.PAYMENT.'. $Key.'. MONEY', array ('default' =>$money,'type' =>'text','required' =>false));?>
    <!-Omitted->
    <? php echo ($this->Form->end ());?>