2
votes

In my form.ctp I have some inputs which are named with dot notation, so they are joined on the POST to the same array inside $this->request->data.

The problem is that I can't manage to create a Modelless form for it, because it won't map dot notations to array, and searchs the fields like consumer.name literally in request->data. Is there anything I can do to validate this data with a simple $form->execute()?

My Cake version is 3.1.1

The code:

//Form Class
protected function _buildSchema(Schema $schema)
{
    return $schema->addField('consumer.name', 'string')
        ->addField('consumer.card_number', ['type' => 'string'])
        ->addField('consumer.expire_month', ['type' => 'string'])
        ->addField('consumer.expire_year', ['type' => 'string'])
        ->addField('type', ['type' => 'string'])
        ->addField('consumer.cvv', ['type' => 'string']);
}

protected function _buildValidator(Validator $validator)
{
    return $validator->add('consumer.name', 'length', [
            'rule' => ['minLength', 3],
            'message' => 'Consumer name too short',
        ])
        ->requirePresence('type', true)
        ->requirePresence('consumer.name', true)
        ->requirePresence('consumer.card_number', true)
        ->requirePresence('consumer.expire_month', true)
        ->requirePresence('consumer.expire_year', true)
        ->requirePresence('consumer.cvv', true)
        ->add('consumer.card_number', 'validFormat', [
            'rule' => array('custom', '/[0-9]{16}/'),
            'message' => 'A valid card number is required',
        ])
        ->add('consumer.expire_month', 'validFormat', [
            'rule' => array('custom', '/[01]?[0-9]/'),
            'message' => 'A valid expire month is required',
        ])
        ->add('consumer.expire_year', 'validFormat', [
            'rule' => array('custom', '/[0-9]{2}/'),
            'message' => 'A valid expire year is required',
        ])
        ->add('consumer.cvv', 'validFormat', [
            'rule' => array('custom', '/[0-9]{3}/'),
            'message' => 'A valid CVV is required',
        ]);
}

My view has several inputs like this:

<?php echo $this->Form->input('consumer.name', [
                                'label' => false,
                                'class' => 'form-control inputbox_user',
                                'pattern' => "^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$",
                                'required' => true,
                                'title' => 'Introduce el nombre del titular de la tarjeta.',
                                ]);

                                 ?>

And my controller logic:

$form = new PaymentForm;
    if ($this->request->is(['patch', 'post', 'put'])){
        if( $form->execute($this->request->data) ){
            //ok
        }
    }
}
1

1 Answers

0
votes

The dot notation will be already turned into a "HTML array" in the view. Debug your request and you'll see ['consumer']['name']. This works as intended by the framework and is correct. AFAIK there is no way around that - and not needed.

Simply change your notation to underscore: consumer_name for example. To use the dots is for another reason a bad idea as well: You will make things difficult for yourself if you need to access them via JS or CSS.