Author - StudySection Post Views - 105 views
forms cakephp

Model-less Forms in CakePHP

The class used is Cake\Form\Form.

Most of the time you will have forms backed by ORM entities and ORM tables or other persistent stores, but there are times when you’ll need to validate user input and then perform an action if the data is valid.

Let’s discuss it with the Contact Form example.

Creating the Form

Generally, when using the Form class you’ll want to use a subclass to define your form. This makes testing easier and lets you re-use your form. Forms are put into src/Form and usually have Formed as a class suffix. For example, a simple contact form would look like this:
// in src/Form/ContactForm.php
namespace App\Form;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
class ContactForm extends Form
{
protected function _buildSchema(Schema $schema)
{
return $schema->addField('name', 'string')
->addField('email', ['type' => 'string'])
->addField('body', ['type' => 'text']);
}
protected function validationDefault(Validator $validator)
{
$validator->add('name', 'length', [
'rule' => ['minLength', 10],
'message' => 'A name is required'
])->add('email', 'format', [
'rule' => 'email',
'message' => 'A valid email address is required',
]);

return $validator;
}

protected function _execute(array $data)
{
// Send an email.
return true;
}
}

In the above example, we see the 3 methods that form provides:

_buildSchema – is used to define the schema data that is used by FormHelper to create an HTML form. You can define field type, length, and precision.
validationDefault – Gets a Cake\Validation\Validator instance that you can attach validators to.
_execute – lets you define the behavior you want to happen when execute() is called and the data is valid.

Processing Request Data

Once you’ve defined your form, you can use it in your controller to process and validate request data:
// In a controller
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class ContactController extends AppController
{
public function index()
{
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success('We will get back to you soon.');
} else {
$this->Flash->error('There was a problem submitting your form.');
}
}
$this->set('contact', $contact);
}
}

In the above example, we use the execute() method to run our form’s _execute() method only when the data is valid, and set flash messages accordingly. We could have also used the validate() method to only validate the request data:

$isValid = $form->validate($this->request->getData());

Setting Form Values

You can set default values for modeless forms using the setData() method. Values set with this method will overwrite existing data in the form object:
// In a controller
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class ContactController extends AppController
{
public function index()
{
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success('We will get back to you soon.');
} else {
$this->Flash->error('There was a problem submitting your form.');
}
}
if ($this->request->is('get')) {
$contact->setData([
'name' => 'John Doe',
'email' => 'john.doe@example.com'
]);
}
$this->set('contact', $contact);
}
}

Values should only be defined if the request method is GET, otherwise, you will overwrite your previous POST Data which might have validation errors that need corrections.

Getting the Form Errors
$errors = $form->getErrors();

Invalidating individual Form Fields from Controller

It is possible to invalidate individual fields from the controller without the use of the Validator class. The most common use case for this is when the validation is done on a remote server. In such a case, you must manually invalidate the fields accordingly to the feedback from the remote server:
// in src/Form/ContactForm.php
public function setErrors($errors)
{
$this->_errors = $errors;
}

According to how the validator class would have returned the errors, $errors must be in this format:
["fieldName" => ["validatorName" => "The error message to display"]]

Now you will be able to invalidate form fields by setting the fieldName, then set the error messages:
// In a controller
$contact = new ContactForm();
$contact->setErrors(["email" => ["_required" => "Your email is required"]]);

Proceed to Create HTML with FormHelper to see the results.
Once you’ve created a Form class, you’ll likely want to create an HTML form for it. FormHelper understands Form objects just like ORM entities:

echo $this->Form->create($contact);
echo $this->Form->control('name');
echo $this->Form->control('email');
echo $this->Form->control('body');
echo $this->Form->button('Submit');
echo $this->Form->end();

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri