本文整理汇总了PHP中Drupal\Core\Form\FormStateInterface::setError方法的典型用法代码示例。如果您正苦于以下问题:PHP FormStateInterface::setError方法的具体用法?PHP FormStateInterface::setError怎么用?PHP FormStateInterface::setError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Form\FormStateInterface
的用法示例。
在下文中一共展示了FormStateInterface::setError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* {@inheritdoc}
*/
public function validate(array &$form, FormStateInterface $form_state)
{
$uploaded_files = $form_state->getValue(['upload'], []);
$trigger = $form_state->getTriggeringElement();
// Only validate if we are uploading a file.
if (empty($uploaded_files) && $trigger['#value'] == 'Upload') {
$form_state->setError($form['widget']['upload'], t('At least one file should be uploaded.'));
}
}
示例2: elementValidate
/**
* Validates a Geofield generic component based form element.
*
* @param array $element
* The element being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*/
function elementValidate(&$element, FormStateInterface $form_state, &$complete_form)
{
$allFilled = TRUE;
$anyFilled = FALSE;
$error_label = isset($element['#error_label']) ? $element['#error_label'] : $element['#title'];
foreach (static::$components as $key => $component) {
if (!empty($element[$key]['#value'])) {
if (!is_numeric($element[$key]['#value'])) {
$form_state->setError($element[$key], t('@title: @component_title is not numeric.', array('@title' => $error_label, '@component_title' => $component['title'])));
} elseif (abs($element[$key]['#value']) > $component['range']) {
$form_state->setError($element[$key], t('@title: @component_title is out of bounds.', array('@title' => $error_label, '@component_title' => $component['title'])));
}
}
if ($element[$key]['#value'] == '') {
$allFilled = FALSE;
} else {
$anyFilled = TRUE;
}
}
if ($anyFilled && !$allFilled) {
foreach (self::$components as $key => $component) {
if ($element[$key]['#value'] == '') {
$form_state->setError($element[$key], t('@title: @component_title must be filled too.', array('@title' => $error_label, '@component_title' => $component['title'])));
}
}
}
}
示例3: validateMatchedPath
/**
* Form element validation handler for matched_path elements.
*
* Note that #maxlength is validated by _form_validate() already.
*
* This checks that the submitted value matches an active route.
*/
public static function validateMatchedPath(&$element, FormStateInterface $form_state, &$complete_form)
{
if (!empty($element['#value']) && ($element['#validate_path'] || $element['#convert_path'] != self::CONVERT_NONE)) {
/** @var \Drupal\Core\Url $url */
if ($url = \Drupal::service('path.validator')->getUrlIfValid($element['#value'])) {
if ($url->isExternal()) {
$form_state->setError($element, t('You cannot use an external URL, please enter a relative path.'));
return;
}
if ($element['#convert_path'] == self::CONVERT_NONE) {
// Url is valid, no conversion required.
return;
}
// We do the value conversion here whilst the Url object is in scope
// after validation has occurred.
if ($element['#convert_path'] == self::CONVERT_ROUTE) {
$form_state->setValueForElement($element, array('route_name' => $url->getRouteName(), 'route_parameters' => $url->getRouteParameters()));
return;
} elseif ($element['#convert_path'] == self::CONVERT_URL) {
$form_state->setValueForElement($element, $url);
return;
}
}
$form_state->setError($element, t('This path does not exist or you do not have permission to link to %path.', array('%path' => $element['#value'])));
}
}
示例4: validateNumber
/**
* Form element validation handler for #type 'number'.
*
* Note that #required is validated by _form_validate() already.
*/
public static function validateNumber(&$element, FormStateInterface $form_state, &$complete_form)
{
$value = $element['#value'];
if ($value === '') {
return;
}
$name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
// Ensure the input is numeric.
if (!is_numeric($value)) {
$form_state->setError($element, t('%name must be a number.', array('%name' => $name)));
return;
}
// Ensure that the input is greater than the #min property, if set.
if (isset($element['#min']) && $value < $element['#min']) {
$form_state->setError($element, t('%name must be higher than or equal to %min.', array('%name' => $name, '%min' => $element['#min'])));
}
// Ensure that the input is less than the #max property, if set.
if (isset($element['#max']) && $value > $element['#max']) {
$form_state->setError($element, t('%name must be lower than or equal to %max.', array('%name' => $name, '%max' => $element['#max'])));
}
if (isset($element['#step']) && strtolower($element['#step']) != 'any') {
// Check that the input is an allowed multiple of #step (offset by #min if
// #min is set).
$offset = isset($element['#min']) ? $element['#min'] : 0.0;
if (!NumberUtility::validStep($value, $element['#step'], $offset)) {
$form_state->setError($element, t('%name is not a valid number.', array('%name' => $name)));
}
}
}
示例5: validateQuantity
/**
* Form element validation handler for #type 'uc_quantity'.
*
* Note that #required is validated by _form_validate() already.
*/
public static function validateQuantity(&$element, FormStateInterface $form_state, &$complete_form)
{
if (!preg_match('/^\\d+$/', $element['#value'])) {
$form_state->setError($element, t('The quantity must be a number.'));
} elseif (empty($element['#allow_zero']) && !$element['#value']) {
$form_state->setError($element, t('The quantity cannot be zero.'));
}
}
示例6: validateForm
/**
* {@inheritDoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state)
{
if (!filter_var($form_state->getValue('email_address'), FILTER_VALIDATE_EMAIL)) {
$form_state->setError($form['email_address'], 'Email address is invalid.');
}
if (!$this->validEmailAddress($form_state->getValue('email_address'))) {
$form_state->setError($form['email_address'], 'Sorry, we only accept Gmail or Yahoo email addresses at this time.');
}
}
示例7: validateNumericCode
/**
* Validates the numeric code.
*/
public function validateNumericCode(array $element, FormStateInterface &$form_state, array $form)
{
$currency = $this->getEntity();
$numeric_code = $element['#value'];
if ($numeric_code && !preg_match('/^\\d{3}$/i', $numeric_code)) {
$form_state->setError($element, $this->t('The numeric code must consist of three digits.'));
} elseif ($currency->isNew()) {
$loaded_currencies = $this->storage->loadByProperties(['numericCode' => $numeric_code]);
if ($loaded_currencies) {
$form_state->setError($element, $this->t('The numeric code is already in use.'));
}
}
}
示例8: validateStylesValue
/**
* #element_validate handler for the "styles" element in settingsForm().
*/
public function validateStylesValue(array $element, FormStateInterface $form_state)
{
$styles_setting = $this->generateStylesSetSetting($element['#value']);
if ($styles_setting === FALSE) {
$form_state->setError($element, t('The provided list of styles is syntactically incorrect.'));
} else {
$style_names = array_map(function ($style) {
return $style['name'];
}, $styles_setting);
if (count($style_names) !== count(array_unique($style_names))) {
$form_state->setError($element, t('Each style must have a unique label.'));
}
}
}
示例9: validateEntityStrings
/**
* {@inheritdoc}
*/
protected function validateEntityStrings(array &$form, array $values, FormStateInterface $form_state) {
$uids = array();
$missing = array();
foreach ($values as $value) {
if (Unicode::strtolower($value) === Unicode::strtolower(\Drupal::config('user.settings')->get('anonymous'))) {
$uids[] = 0;
}
else {
$missing[strtolower($value)] = $value;
}
}
if (!$missing) {
return $uids;
}
$result = Database::getConnection()->query("SELECT * FROM {users} WHERE name IN (:names)", array(':names' => array_values($missing)));
foreach ($result as $account) {
unset($missing[strtolower($account->name)]);
$uids[] = $account->uid;
}
if ($missing) {
$form_state->setError($form, $this->formatPlural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', $missing))));
}
return $uids;
}
示例10: validateOptionsForm
public function validateOptionsForm(&$form, FormStateInterface $form_state)
{
// Require a key if the format is key.
if ($form_state->getValue(array('options', 'format')) == 'key' && $form_state->getValue(array('options', 'key')) == '') {
$form_state->setError($form['key'], $this->t('You have to enter a key if you want to display a key of the data.'));
}
}
示例11: testSetError
/**
* @covers ::setError
*/
public function testSetError()
{
$element = ['#foo' => 'bar'];
$message = 'bar';
$this->decoratedFormState->setError($element, $message)->shouldBeCalled();
$this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setError($element, $message));
}
示例12: value
/**
* {@inheritdoc}
*/
public static function value(array &$element, &$input, FormStateInterface $form_state)
{
if (isset($input['filefield_imce']['file_path']) && $input['filefield_imce']['file_path'] != '') {
$instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
$field_settings = $instance->getSettings();
$scheme = $field_settings['uri_scheme'];
$wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
$file_directory_prefix = $scheme == 'private' ? 'system/files' : $wrapper->getDirectoryPath();
$uri = preg_replace('/^' . preg_quote(base_path() . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $input['filefield_imce']['file_path']);
// Resolve the file path to an FID.
$fid = db_select('file_managed', 'f')->condition('uri', rawurldecode($uri))->fields('f', array('fid'))->execute()->fetchField();
if ($fid) {
$file = file_load($fid);
if (filefield_sources_element_validate($element, $file)) {
if (!in_array($file->id(), $input['fids'])) {
$input['fids'][] = $file->id();
}
}
} else {
$form_state->setError($element, t('The selected file could not be used because the file does not exist in the database.'));
}
// No matter what happens, clear the value from the file path field.
$input['filefield_imce']['file_path'] = '';
}
}
示例13: value
/**
* {@inheritdoc}
*/
public static function value(array &$element, &$input, FormStateInterface $form_state)
{
if (isset($input['filefield_reference']['autocomplete']) && strlen($input['filefield_reference']['autocomplete']) > 0 && $input['filefield_reference']['autocomplete'] != FILEFIELD_SOURCE_REFERENCE_HINT_TEXT) {
$matches = array();
if (preg_match('/\\[fid:(\\d+)\\]/', $input['filefield_reference']['autocomplete'], $matches)) {
$fid = $matches[1];
if ($file = file_load($fid)) {
// Remove file size restrictions, since the file already exists on
// disk.
if (isset($element['#upload_validators']['file_validate_size'])) {
unset($element['#upload_validators']['file_validate_size']);
}
// Check that the user has access to this file through
// hook_download().
if (!$file->access('download')) {
$form_state->setError($element, t('You do not have permission to use the selected file.'));
} elseif (filefield_sources_element_validate($element, (object) $file, $form_state)) {
if (!in_array($file->id(), $input['fids'])) {
$input['fids'][] = $file->id();
}
}
} else {
$form_state->setError($element, t('The referenced file could not be used because the file does not exist in the database.'));
}
}
// No matter what happens, clear the value from the autocomplete.
$input['filefield_reference']['autocomplete'] = '';
}
}
示例14: elementValidateRequired
/**
* {@inheritdoc}
*/
public function elementValidateRequired($element, FormStateInterface $form_state)
{
// Set a custom validation error on the #required element.
if (!empty($element['#required_but_empty']) && isset($element['#form_test_required_error'])) {
$form_state->setError($element, $element['#form_test_required_error']);
}
}
示例15: validateTag
/**
* Validates the tag field.
*/
public function validateTag(array $element, FormStateInterface $form_state, array $form)
{
$tag = $element['#value'];
if (!empty($tag) && !preg_match('/[a-zA-Z0-9]+/', $tag)) {
$form_state->setError($element, $this->t('The tag must be a single word.'));
}
}