Friday, November 14, 2014

Re: cake3 validation in controller

How do link the schema with Table Object?
You can provide an example?

I have an action "contact" in PagesController which processes the contact form:

//model
namespace App\Model\Table;
use Cake\Database\Schema;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class PagesTable extends Table {
    public function initialize(array $config) {
        $t = new Schema\Table('pages');
        $t->addColumn('email', 'string')
          ->addColumn('name', 'string')
          ->addColumn('subject', 'string')
          ->addColumn('content', 'string');
    }
}

regards --cesar

El viernes, 14 de noviembre de 2014 09:32:15 UTC-2, Florian Krämer escribió:
Create a table / model object with a schema but no table and use it as usual. The controller should be tiny and models fat.



On Thursday, November 13, 2014 1:58:42 PM UTC+1, cesar calvo wrote:
It is possible show in a form the error messages with no model associated? Obviously using the Form helper.
I see validating-data, but I need some guidance about the feasibility automatically display errors like when work with a model

//view
<?=
    $this->Form->create(),
    $this->Form->input('name', ['label' => __('Name')]),
    $this->Form->input('email', ['label' => __('Email')]),
    $this->Form->input('subject', ['label' => __('Subject')]),
    $this->Form->input('message', ['label' => __('Message'), 'type' => 'textarea', 'rows' => '3']),
    $this->Form->button(__('Submit')),
    $this->Form->end()
?>

//controller
public function contact() {
        if ($this->request->is('post')) {
            $validator = new Validator();
            $validator
                ->validatePresence('email')
                ->notEmpty('email', __('This field is required.'))
                ->add('email', 'valid', ['rule' => 'email', 'message' => __('This field requires a valid email address.')])
                ->validatePresence('name')
                ->notEmpty('name', __('This field is required.'))
                ->validatePresence('subject')
                ->notEmpty('subject', __('This field is required.'))
                ->validatePresence('message')
                ->notEmpty('message', __('This field is required.'))
            ;
            $errors = $validator->errors($this->request->data);
            if (empty($errors)) {
                $this->Flash->success(__('Your data has been sent succesfully.'));
            } else {
                $this->Flash->error(__('Unable to sent your data.'));
            }
        }
}

best regards

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: