CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • ArrayContext
  • ContextFactory
  • EntityContext
  • FormContext
  • NullContext

Interfaces

  • ContextInterface
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\View\Form;
 16: 
 17: use Cake\Http\ServerRequest;
 18: use Cake\Utility\Hash;
 19: 
 20: /**
 21:  * Provides a context provider for Cake\Form\Form instances.
 22:  *
 23:  * This context provider simply fulfils the interface requirements
 24:  * that FormHelper has and allows access to the request data.
 25:  */
 26: class FormContext implements ContextInterface
 27: {
 28:     /**
 29:      * The request object.
 30:      *
 31:      * @var \Cake\Http\ServerRequest
 32:      */
 33:     protected $_request;
 34: 
 35:     /**
 36:      * The form object.
 37:      *
 38:      * @var \Cake\Form\Form
 39:      */
 40:     protected $_form;
 41: 
 42:     /**
 43:      * Constructor.
 44:      *
 45:      * @param \Cake\Http\ServerRequest $request The request object.
 46:      * @param array $context Context info.
 47:      */
 48:     public function __construct(ServerRequest $request, array $context)
 49:     {
 50:         $this->_request = $request;
 51:         $context += [
 52:             'entity' => null,
 53:         ];
 54:         $this->_form = $context['entity'];
 55:     }
 56: 
 57:     /**
 58:      * {@inheritDoc}
 59:      */
 60:     public function primaryKey()
 61:     {
 62:         return [];
 63:     }
 64: 
 65:     /**
 66:      * {@inheritDoc}
 67:      */
 68:     public function isPrimaryKey($field)
 69:     {
 70:         return false;
 71:     }
 72: 
 73:     /**
 74:      * {@inheritDoc}
 75:      */
 76:     public function isCreate()
 77:     {
 78:         return true;
 79:     }
 80: 
 81:     /**
 82:      * {@inheritDoc}
 83:      */
 84:     public function val($field, $options = [])
 85:     {
 86:         $options += [
 87:             'default' => null,
 88:             'schemaDefault' => true
 89:         ];
 90: 
 91:         $val = $this->_request->getData($field);
 92:         if ($val !== null) {
 93:             return $val;
 94:         }
 95: 
 96:         $val = $this->_form->getData($field);
 97:         if ($val !== null) {
 98:             return $val;
 99:         }
100: 
101:         if ($options['default'] !== null || !$options['schemaDefault']) {
102:             return $options['default'];
103:         }
104: 
105:         return $this->_schemaDefault($field);
106:     }
107: 
108:     /**
109:      * Get default value from form schema for given field.
110:      *
111:      * @param string $field Field name.
112:      * @return mixed
113:      */
114:     protected function _schemaDefault($field)
115:     {
116:         $field = $this->_form->schema()->field($field);
117:         if ($field) {
118:             return $field['default'];
119:         }
120: 
121:         return null;
122:     }
123: 
124:     /**
125:      * {@inheritDoc}
126:      */
127:     public function isRequired($field)
128:     {
129:         $validator = $this->_form->getValidator();
130:         if (!$validator->hasField($field)) {
131:             return false;
132:         }
133:         if ($this->type($field) !== 'boolean') {
134:             return $validator->isEmptyAllowed($field, $this->isCreate()) === false;
135:         }
136: 
137:         return false;
138:     }
139: 
140:     /**
141:      * {@inheritDoc}
142:      */
143:     public function getRequiredMessage($field)
144:     {
145:         $parts = explode('.', $field);
146: 
147:         $validator = $this->_form->getValidator();
148:         $fieldName = array_pop($parts);
149:         if (!$validator->hasField($fieldName)) {
150:             return null;
151:         }
152: 
153:         $ruleset = $validator->field($fieldName);
154: 
155:         $requiredMessage = $validator->getRequiredMessage($fieldName);
156:         $emptyMessage = $validator->getNotEmptyMessage($fieldName);
157: 
158:         if ($ruleset->isPresenceRequired() && $requiredMessage) {
159:             return $requiredMessage;
160:         }
161:         if (!$ruleset->isEmptyAllowed() && $emptyMessage) {
162:             return $emptyMessage;
163:         }
164: 
165:         return null;
166:     }
167: 
168:     /**
169:      * {@inheritDoc}
170:      */
171:     public function getMaxLength($field)
172:     {
173:         $validator = $this->_form->getValidator();
174:         if (!$validator->hasField($field)) {
175:             return null;
176:         }
177:         foreach ($validator->field($field)->rules() as $rule) {
178:             if ($rule->get('rule') === 'maxLength') {
179:                 return $rule->get('pass')[0];
180:             }
181:         }
182: 
183:         return null;
184:     }
185: 
186:     /**
187:      * {@inheritDoc}
188:      */
189:     public function fieldNames()
190:     {
191:         return $this->_form->schema()->fields();
192:     }
193: 
194:     /**
195:      * {@inheritDoc}
196:      */
197:     public function type($field)
198:     {
199:         return $this->_form->schema()->fieldType($field);
200:     }
201: 
202:     /**
203:      * {@inheritDoc}
204:      */
205:     public function attributes($field)
206:     {
207:         $column = (array)$this->_form->schema()->field($field);
208:         $whiteList = ['length' => null, 'precision' => null];
209: 
210:         return array_intersect_key($column, $whiteList);
211:     }
212: 
213:     /**
214:      * {@inheritDoc}
215:      */
216:     public function hasError($field)
217:     {
218:         $errors = $this->error($field);
219: 
220:         return count($errors) > 0;
221:     }
222: 
223:     /**
224:      * {@inheritDoc}
225:      */
226:     public function error($field)
227:     {
228:         return (array)Hash::get($this->_form->getErrors(), $field, []);
229:     }
230: }
231: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs