Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modelless form with file input, validation error doesn't appear in view #14779

Closed
1 of 3 tasks
burzum opened this issue Jul 8, 2020 · 4 comments
Closed
1 of 3 tasks
Labels
Milestone

Comments

@burzum
Copy link
Contributor

burzum commented Jul 8, 2020

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.8.x

What you did

Created modelless Form with file input, added validation for the file and error output in the view.

Also the date inputs are not threated the same as when used with DB. For whatever reason the validation doesn't seem to like the arrays that are returned from the two date inputs. This works fine when working with a form that has a model.

I've already checked https://book.cakephp.org/3/en/core-libraries/form.html and https://book.cakephp.org/3/en/core-libraries/validation.html

View:

	<?php echo $this->Form->create($form, ['type' => 'file']) ?>
	<fieldset>
		<legend><?php echo __('Add Event Codes') ?></legend>
		<?php
			echo $this->Form->file('csv_file', [
				'accept' => 'text/csv'
			]);
			echo $this->Form->error('csv_file');
			echo $this->Form->control('event_organizer_id', ['options' => $eventOrganizers]);
			echo $this->Form->control('event_id');
			echo $this->Form->control('code_type');
			echo $this->Form->control('start_date');
			echo $this->Form->control('end_date');
		?>
	</fieldset>
	<?php echo $this->Form->button(__('Submit')) ?>
	<?php echo $this->Form->end() ?>

Controller:

		$form = new EventCodesBulkAddingForm();

		if ($this->request->is('post')) {
			$data = $this->getRequest()->getData();
			if (!$form->execute($data)) {
				//...
			}
		}

What happened

Error doesn't show up but the field was invalidated:

object(App\Form\EventCodesBulkAddingForm) {

	'_schema' => [
		'_fields' => [
			'event_organizer_id' => [
				'type' => 'string',
				'length' => null,
				'precision' => null,
				'default' => null
			],
			'event_id' => [
				'type' => 'string',
				'length' => null,
				'precision' => null,
				'default' => null
			],
			'csv_file' => [
				'type' => 'string',
				'length' => null,
				'precision' => null,
				'default' => null
			],
			'code_type' => [
				'type' => 'string',
				'length' => null,
				'precision' => null,
				'default' => null
			],
			'start_date' => [
				'type' => 'date',
				'length' => null,
				'precision' => null,
				'default' => null
			],
			'end_date' => [
				'type' => 'date',
				'length' => null,
				'precision' => null,
				'default' => null
			]
		]
	],
	'_errors' => [
		'csv_file' => [
			'mimeType' => 'The provided value is invalid'
		],
		'start_date' => [
			'notBlank' => 'The provided value is invalid'
		],
		'end_date' => [
			'notBlank' => 'The provided value is invalid',
			'greaterThanOrEqualToField' => 'The end date must be greater or equal than the start date'
		]
	],
	'_validator' => [
		'_presenceMessages' => [],
		'_allowEmptyMessages' => [],
		'_allowEmptyFlags' => [],
		'_useI18n' => true,
		'_providers' => [
			(int) 0 => 'form',
			(int) 1 => 'default'
		],
		'_fields' => [
			'csv_file' => [
				'isPresenceRequired' => true,
				'isEmptyAllowed' => false,
				'rules' => [
					(int) 0 => 'mimeType',
					(int) 1 => 'uploadedFile'
				]
			],
			'event_organizer_id' => [
				'isPresenceRequired' => 'create',
				'isEmptyAllowed' => false,
				'rules' => [
					(int) 0 => 'notBlank'
				]
			],
			'event_id' => [
				'isPresenceRequired' => 'create',
				'isEmptyAllowed' => false,
				'rules' => [
					(int) 0 => 'notBlank'
				]
			],
			'code_type' => [
				'isPresenceRequired' => 'create',
				'isEmptyAllowed' => false,
				'rules' => [
					(int) 0 => 'notBlank'
				]
			],
			'start_date' => [
				'isPresenceRequired' => 'create',
				'isEmptyAllowed' => false,
				'rules' => [
					(int) 0 => 'notBlank',
					(int) 1 => 'date'
				]
			],
			'end_date' => [
				'isPresenceRequired' => 'create',
				'isEmptyAllowed' => false,
				'rules' => [
					(int) 0 => 'notBlank',
					(int) 1 => 'date',
					(int) 2 => 'greaterThanOrEqualToField'
				]
			]
		]
	],
	'_schemaClass' => 'Cake\Form\Schema',
	'_data' => [],
	'_eventManager' => object(Cake\Event\EventManager) {

		'_listeners' => [
			'Form.buildValidator' => '1 listener(s)'
		],
		'_isGlobal' => false,
		'_trackEvents' => false,
		'_generalManager' => '(object) EventManager',
		'_dispatchedEvents' => null
	
	},
	'_eventClass' => 'Cake\Event\Event',
	'_validatorClass' => 'Cake\Validation\Validator',
	'_validators' => [
		'default' => object(Cake\Validation\Validator) {

			'_presenceMessages' => [],
			'_allowEmptyMessages' => [],
			'_allowEmptyFlags' => [],
			'_useI18n' => true,
			'_providers' => [
				(int) 0 => 'form',
				(int) 1 => 'default'
			],
			'_fields' => [
				'csv_file' => [
					'isPresenceRequired' => true,
					'isEmptyAllowed' => false,
					'rules' => [
						(int) 0 => 'mimeType',
						(int) 1 => 'uploadedFile'
					]
				],
				'event_organizer_id' => [
					'isPresenceRequired' => 'create',
					'isEmptyAllowed' => false,
					'rules' => [
						(int) 0 => 'notBlank'
					]
				],
				'event_id' => [
					'isPresenceRequired' => 'create',
					'isEmptyAllowed' => false,
					'rules' => [
						(int) 0 => 'notBlank'
					]
				],
				'code_type' => [
					'isPresenceRequired' => 'create',
					'isEmptyAllowed' => false,
					'rules' => [
						(int) 0 => 'notBlank'
					]
				],
				'start_date' => [
					'isPresenceRequired' => 'create',
					'isEmptyAllowed' => false,
					'rules' => [
						(int) 0 => 'notBlank',
						(int) 1 => 'date'
					]
				],
				'end_date' => [
					'isPresenceRequired' => 'create',
					'isEmptyAllowed' => false,
					'rules' => [
						(int) 0 => 'notBlank',
						(int) 1 => 'date',
						(int) 2 => 'greaterThanOrEqualToField'
					]
				]
			]
		
		}
	]

}

What you expected to happen

Error message shown.

@dereuromark dereuromark added this to the 3.9.1 milestone Jul 8, 2020
@ADmad
Copy link
Member

ADmad commented Jul 8, 2020

You are using $this->Form->file() which will only generate the <input> tag. You need to use $this->Form->control() to get the error div too.

@ADmad ADmad closed this as completed Jul 8, 2020
@ADmad ADmad added the invalid label Jul 8, 2020
@ADmad ADmad reopened this Jul 8, 2020
@ADmad
Copy link
Member

ADmad commented Jul 8, 2020

Apologies, I missed the $this->Form->error() call below.

@burzum
Copy link
Contributor Author

burzum commented Jul 8, 2020

@ADmad the funny thing is this works when using the control as you suggest. But shouldn't the error() method display it as well?

                        // Shows error
			echo $this->Form->control('csv_file', [
				'type' => 'file',
				'accept' => 'text/csv'
			]);
                        // Nothing shown
			echo $this->Form->error('csv_file');

@dereuromark dereuromark added defect and removed invalid labels Jul 8, 2020
@burzum
Copy link
Contributor Author

burzum commented Jul 8, 2020

@ADmad OK, we can close the ticket. I've just realized somebody 😡 added a style that hides the error messages. So now I need to apologize for this ticket. 😄

Edit: Well, the issue with the dates remains.

Edit2: Somebody is a bug in Bootstrap that they never fixed... 😠 twbs/bootstrap#29439

@burzum burzum closed this as completed Jul 8, 2020
@ADmad ADmad added support and removed defect labels Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants