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.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 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:     /**
 30:      * The request object.
 31:      *
 32:      * @var \Cake\Http\ServerRequest
 33:      */
 34:     protected $_request;
 35: 
 36:     /**
 37:      * The form object.
 38:      *
 39:      * @var \Cake\Form\Form
 40:      */
 41:     protected $_form;
 42: 
 43:     /**
 44:      * Constructor.
 45:      *
 46:      * @param \Cake\Http\ServerRequest $request The request object.
 47:      * @param array $context Context info.
 48:      */
 49:     public function __construct(ServerRequest $request, array $context)
 50:     {
 51:         $this->_request = $request;
 52:         $context += [
 53:             'entity' => null,
 54:         ];
 55:         $this->_form = $context['entity'];
 56:     }
 57: 
 58:     /**
 59:      * {@inheritDoc}
 60:      */
 61:     public function primaryKey()
 62:     {
 63:         return [];
 64:     }
 65: 
 66:     /**
 67:      * {@inheritDoc}
 68:      */
 69:     public function isPrimaryKey($field)
 70:     {
 71:         return false;
 72:     }
 73: 
 74:     /**
 75:      * {@inheritDoc}
 76:      */
 77:     public function isCreate()
 78:     {
 79:         return true;
 80:     }
 81: 
 82:     /**
 83:      * {@inheritDoc}
 84:      */
 85:     public function val($field, $options = [])
 86:     {
 87:         $options += [
 88:             'default' => null,
 89:             'schemaDefault' => true
 90:         ];
 91: 
 92:         $val = $this->_request->getData($field);
 93:         if ($val !== null) {
 94:             return $val;
 95:         }
 96: 
 97:         $val = $this->_form->getData($field);
 98:         if ($val !== null) {
 99:             return $val;
100:         }
101: 
102:         if ($options['default'] !== null || !$options['schemaDefault']) {
103:             return $options['default'];
104:         }
105: 
106:         return $this->_schemaDefault($field);
107:     }
108: 
109:     /**
110:      * Get default value from form schema for given field.
111:      *
112:      * @param string $field Field name.
113:      * @return mixed
114:      */
115:     protected function _schemaDefault($field)
116:     {
117:         $field = $this->_form->schema()->field($field);
118:         if ($field) {
119:             return $field['default'];
120:         }
121: 
122:         return null;
123:     }
124: 
125:     /**
126:      * {@inheritDoc}
127:      */
128:     public function isRequired($field)
129:     {
130:         $validator = $this->_form->getValidator();
131:         if (!$validator->hasField($field)) {
132:             return false;
133:         }
134:         if ($this->type($field) !== 'boolean') {
135:             return $validator->isEmptyAllowed($field, $this->isCreate()) === false;
136:         }
137: 
138:         return false;
139:     }
140: 
141:     /**
142:      * {@inheritDoc}
143:      */
144:     public function getRequiredMessage($field)
145:     {
146:         $parts = explode('.', $field);
147: 
148:         $validator = $this->_form->getValidator();
149:         $fieldName = array_pop($parts);
150:         if (!$validator->hasField($fieldName)) {
151:             return null;
152:         }
153: 
154:         $ruleset = $validator->field($fieldName);
155: 
156:         $requiredMessage = $validator->getRequiredMessage($fieldName);
157:         $emptyMessage = $validator->getNotEmptyMessage($fieldName);
158: 
159:         if ($ruleset->isPresenceRequired() && $requiredMessage) {
160:             return $requiredMessage;
161:         }
162:         if (!$ruleset->isEmptyAllowed() && $emptyMessage) {
163:             return $emptyMessage;
164:         }
165: 
166:         return null;
167:     }
168: 
169:     /**
170:      * {@inheritDoc}
171:      */
172:     public function getMaxLength($field)
173:     {
174:         $validator = $this->_form->getValidator();
175:         if (!$validator->hasField($field)) {
176:             return null;
177:         }
178:         foreach ($validator->field($field)->rules() as $rule) {
179:             if ($rule->get('rule') === 'maxLength') {
180:                 return $rule->get('pass')[0];
181:             }
182:         }
183: 
184:         return null;
185:     }
186: 
187:     /**
188:      * {@inheritDoc}
189:      */
190:     public function fieldNames()
191:     {
192:         return $this->_form->schema()->fields();
193:     }
194: 
195:     /**
196:      * {@inheritDoc}
197:      */
198:     public function type($field)
199:     {
200:         return $this->_form->schema()->fieldType($field);
201:     }
202: 
203:     /**
204:      * {@inheritDoc}
205:      */
206:     public function attributes($field)
207:     {
208:         $column = (array)$this->_form->schema()->field($field);
209:         $whiteList = ['length' => null, 'precision' => null];
210: 
211:         return array_intersect_key($column, $whiteList);
212:     }
213: 
214:     /**
215:      * {@inheritDoc}
216:      */
217:     public function hasError($field)
218:     {
219:         $errors = $this->error($field);
220: 
221:         return count($errors) > 0;
222:     }
223: 
224:     /**
225:      * {@inheritDoc}
226:      */
227:     public function error($field)
228:     {
229:         return array_values((array)Hash::get($this->_form->getErrors(), $field, []));
230:     }
231: }
232: 
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