Wednesday, July 30, 2014

Re: Display Form Validation Errors CakePHP 3

Glad it worked :)

On Wednesday, July 30, 2014 3:35:21 PM UTC+2, Jipson Thomas wrote:
Hi Jose Lorenzo,
Thank you very much. Now it is working perfectly.
Regards,
Jipson

On Wednesday, 30 July 2014 14:31:30 UTC+1, José Lorenzo wrote:
Ok, you problem is that you are calling create() without the entity, You should get that fixed by doing:

<?php echo $this->Form->create($package) ?>

On Wednesday, July 30, 2014 3:29:33 PM UTC+2, Jipson Thomas wrote:
Hi Jose Lorenzo,
Thank you for the reply. Please find the below code

CTP
======
<div class="users form">
<?php echo $this->Flash->render('auth') ?>
<?php echo $this->Form->create() ?>
    <fieldset>
        <legend><?php echo __('Please enter the package details') ?></legend>
       <?php echo $this->Form->input('name', ['label' => 'Package Name']); ?>
       <?php echo $this->Form->input('annual_price', ['label' => 'Price / Year','type' => 'decimal']); ?>
       <?php echo $this->Form->input('monthly_price', ['label' => 'Price / Month','type' => 'decimal']); ?>
       <?php echo $this->Form->input('duration', ['label' => 'Minimum Months of Contract','type' => 'integer']);?>
       <?php echo $this->Form->input('no_partners', ['label' => 'Maximum No of Partners','type' => 'integer']);?>
       <?php echo $this->Form->input('no_emails', ['label' => 'Maximum No of Emails/Month','type' => 'integer']);?>
       <?php echo $this->Form->input('storage', ['label' => 'Maximum Allowed Storage Space (GB)','type' => 'integer']);?>
       <?php echo $this->Form->input('resource_library', ['label' => 'Resource Library','type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('portal_cms', ['label' => 'Portal CMS','type' => 'checkbox','value'=>'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('MDF', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('deal_registration', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('partner_recruit', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('training', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('Socialmedia', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('multilingual', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('partner_incentive', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
       <?php echo $this->Form->input('partner_app', ['type' => 'checkbox','value' => 'Y','hiddenField' => 'N']);?>
      
    </fieldset>
<?php echo $this->Form->button(__('Save')); ?>
<?php echo $this->Form->end() ?>
</div>

Controller Action
==============
public function add(){
        $package = $this->Package->newEntity($this->request->data);
        if ($this->request->is('post')) {
            if ($this->Package->save($package)) {
                $this->Flash->success(__('The package has been saved.'));
                return $this->redirect(['action' => 'index']);
            }
            else{
              //  $errors = $this->Package->errors();
               
                $error_string=  implode('\n',$package->errors());
                //print_r($package);
            }
            $this->Flash->error(__('Unable to add the package.'));
        }
        $this->set('package', $package);
    }

Model - Table Class
=================
class PackagesTable extends Table {
    public function initialize(array $config) {
        $this->addBehavior('Timestamp', [
            'events' => [
                'Model.beforeSave' => [
                    'created_on' => 'new',
                    'modified_on' => 'always',
                ]
               
            ]
        ]);
    }
    public function validationDefault(Validator $validator) {
        $validator
            ->notEmpty('name','A valid package name is required')
            ->notEmpty('annual_price','A valid annual price is required')
            ->notEmpty('monthly_price','A valid monthly price is required')
            ->notEmpty('duration','Minimum duration of contract is required')
            ->notEmpty('no_partners','Maximum no of partners is required')
            ->notEmpty('no_emails','Maximum no of emails per month is required');

        return $validator;
    }
}

Regards,
Jipson

On Wednesday, 30 July 2014 14:20:54 UTC+1, Jipson Thomas wrote:
Hi ,
On my package creation form I am using cakephp validation. The validation is working perfectly. Unfortunately the validation error messages are not displaying on my form. Would you please help me on this?

The code I am using is as follows. Please advise me what I am missing here.

My Table Class

==============
public function validationDefault(Validator $validator) {
        $validator
            ->notEmpty('name','A valid package name is required')
            ->notEmpty('annual_price','A valid annual price is required')
            ->notEmpty('monthly_price','A valid monthly price is required')
            ->notEmpty('duration','Minimum duration of contract is required')
            ->notEmpty('no_partners','Maximum no of partners is required')
            ->notEmpty('no_emails','Maximum no of emails per month is required');

        return $validator;
    }

On my controller

================
I am using the following codes to get the validation result.
$package = $this->Package->newEntity($this->request->data);
if ($this->SubscriptionPackage->save($package)) {
                $this->Flash->success(__('The package has been saved.'));
                return $this->redirect(['action' => 'index']);
            }
            else{
//print_r($package->errors()); It will display an array with field names and message.
}

On my ctp file I am using the following code.
====================================
 <?php echo $this->Form->input('name', ['label' => 'Package Name']); ?>
       <?php echo $this->Form->input('annual_price', ['label' => 'Price / Year','type' => 'decimal']); ?>
       <?php echo $this->Form->input('monthly_price', ['label' => 'Price / Month','type' => 'decimal']); ?>
       <?php echo $this->Form->input('duration', ['label' => 'Minimum Months of Contract','type' => 'integer']);?>
       <?php echo $this->Form->input('no_partners', ['label' => 'Maximum No of Partners','type' => 'integer']);?>
       <?php echo $this->Form->input('no_emails', ['label' => 'Maximum No of Emails/Month','type' => 'integer']);?>
       <?php echo $this->Form->input('storage', ['label' => 'Maximum Allowed Storage Space (GB)','type' =>?>

--
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: