本文整理汇总了PHP中Drupal\Core\Form\FormStateInterface::setValueForElement方法的典型用法代码示例。如果您正苦于以下问题:PHP FormStateInterface::setValueForElement方法的具体用法?PHP FormStateInterface::setValueForElement怎么用?PHP FormStateInterface::setValueForElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Form\FormStateInterface
的用法示例。
在下文中一共展示了FormStateInterface::setValueForElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validateName
/**
* Form element validation handler for 'name' in form_test_validate_form().
*/
public function validateName(&$element, FormStateInterface $form_state)
{
$triggered = FALSE;
if ($form_state->getValue('name') == 'element_validate') {
// Alter the form element.
$element['#value'] = '#value changed by #element_validate';
// Alter the submitted value in $form_state.
$form_state->setValueForElement($element, 'value changed by setValueForElement() in #element_validate');
$triggered = TRUE;
}
if ($form_state->getValue('name') == 'element_validate_access') {
$form_state->set('form_test_name', $form_state->getValue('name'));
// Alter the form element.
$element['#access'] = FALSE;
$triggered = TRUE;
} elseif ($form_state->has('form_test_name')) {
// To simplify this test, just take over the element's value into $form_state.
$form_state->setValueForElement($element, $form_state->get('form_test_name'));
$triggered = TRUE;
}
if ($triggered) {
// Output the element's value from $form_state.
drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state->getValue('name'))));
// Trigger a form validation error to see our changes.
$form_state->setErrorByName('');
}
}
示例2: validateForm
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state)
{
if ($form_state->getValue('name') == 'validate') {
// Alter the form element.
$form['name']['#value'] = '#value changed by #validate';
// Alter the submitted value in $form_state.
$form_state->setValueForElement($form['name'], 'value changed by $form_state->setValueForElement() in #validate');
// Output the element's value from $form_state.
drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state->getValue('name'))));
// Trigger a form validation error to see our changes.
$form_state->setErrorByName('');
}
}
示例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: defaultValuesFormSubmit
/**
* {@inheritdoc}
*/
public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state)
{
if ($form_state->getValue(['default_value_input', 'default_date_type']) || $form_state->getValue(['default_value_input', 'default_end_date_type'])) {
if ($form_state->getValue(['default_value_input', 'default_date_type']) == static::DEFAULT_VALUE_NOW) {
$form_state->setValueForElement($element['default_date'], static::DEFAULT_VALUE_NOW);
}
if ($form_state->getValue(['default_value_input', 'default_end_date_type']) == static::DEFAULT_VALUE_NOW) {
$form_state->setValueForElement($element['default_end_date'], static::DEFAULT_VALUE_NOW);
}
return [$form_state->getValue('default_value_input')];
}
return [];
}
示例5: validateForm
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
*/
public function validateForm(array &$form, FormStateInterface $form_state)
{
$routing_path = $form_state->getValue('routing_path');
if ($routing_path) {
$form_state->setValueForElement($form['routing']['routing_path'], $routing_path);
} else {
$form_state->setValueForElement($form['routing']['routing_path'], '/headless');
}
if ($routing_path[0] == '/') {
$form_state->setErrorByName('routing_path', $this->t("The path '%path' cannot start with a slash.", array('%path' => $routing_path)));
}
if (!UrlHelper::isValid($routing_path)) {
$form_state->setErrorByName('routing_path', $this->t("The path '%path' is invalid or you do not have access to it.", array('%path' => $routing_path)));
}
}
示例6: clearFormValues
/**
* Clear values from upload form element.
*
* @param array $element
* Upload form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state object.
*/
protected function clearFormValues(array &$element, FormStateInterface $form_state)
{
// We propagated entities to the other parts of the system. We can now remove
// them from our values.
$form_state->setValueForElement($element['upload']['fids'], '');
NestedArray::setValue($form_state->getUserInput(), $element['upload']['fids']['#parents'], '');
}
示例7: testSetValueForElement
/**
* @covers ::setValueForElement
*/
public function testSetValueForElement()
{
$element = ['#type' => 'foo'];
$value = 'BAR';
$this->decoratedFormState->setValueForElement($element, $value)->shouldBeCalled();
$this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setValueForElement($element, $value));
}
示例8: validateColor
/**
* Form element validation handler for #type 'color'.
*/
public static function validateColor(&$element, FormStateInterface $form_state, &$complete_form)
{
$value = trim($element['#value']);
// Default to black if no value is given.
// @see http://www.w3.org/TR/html5/number-state.html#color-state
if ($value === '') {
$form_state->setValueForElement($element, '#000000');
} else {
// Try to parse the value and normalize it.
try {
$form_state->setValueForElement($element, ColorUtility::rgbToHex(ColorUtility::hexToRgb($value)));
} catch (\InvalidArgumentException $e) {
$form_state->setError($element, t('%name must be a valid color.', array('%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'])));
}
}
}
示例9: validateEmail
/**
* Form element validation handler for #type 'email'.
*
* Note that #maxlength and #required is validated by _form_validate() already.
*/
public static function validateEmail(&$element, FormStateInterface $form_state, &$complete_form)
{
$value = trim($element['#value']);
$form_state->setValueForElement($element, $value);
if ($value !== '' && !valid_email_address($value)) {
$form_state->setError($element, t('The email address %mail is not valid.', array('%mail' => $value)));
}
}
示例10: validateUrl
/**
* Form element validation handler for #type 'url'.
*
* Note that #maxlength and #required is validated by _form_validate() already.
*/
public static function validateUrl(&$element, FormStateInterface $form_state, &$complete_form)
{
$value = trim($element['#value']);
$form_state->setValueForElement($element, $value);
if ($value !== '' && !UrlHelper::isValid($value, TRUE)) {
$form_state->setError($element, t('The URL %url is not valid.', array('%url' => $value)));
}
}
示例11: multipleValidate
/**
* Element validation helper.
*/
public static function multipleValidate($element, FormStateInterface $form_state)
{
$values = array_map('trim', explode(',', $element['#value']));
$items = array();
foreach ($values as $value) {
$items[] = array('value' => $value);
}
$form_state->setValueForElement($element, $items);
}
示例12: validatePasswordConfirm
/**
* Validates a password_confirm element.
*/
public static function validatePasswordConfirm(&$element, FormStateInterface $form_state, &$complete_form)
{
$pass1 = trim($element['pass1']['#value']);
$pass2 = trim($element['pass2']['#value']);
if (!empty($pass1) || !empty($pass2)) {
if (strcmp($pass1, $pass2)) {
$form_state->setError($element, t('The specified passwords do not match.'));
}
} elseif ($element['#required'] && $form_state->getUserInput()) {
$form_state->setError($element, t('Password field is required.'));
}
// Password field must be converted from a two-element array into a single
// string regardless of validation results.
$form_state->setValueForElement($element['pass1'], NULL);
$form_state->setValueForElement($element['pass2'], NULL);
$form_state->setValueForElement($element, $pass1);
return $element;
}
示例13: validateSelectorUri
/**
* Validation handler for the selection element.
*/
public function validateSelectorUri($element, FormStateInterface $form_state, $form)
{
if (!empty($element['#value'])) {
if (file_exists($file_path = $this->configuration['path'] . '/' . $element['#value'])) {
$form_state->setValueForElement($element, $file_path);
} else {
$form_state->setErrorByName(implode('][', $element['#parents']), $this->t('The selected file does not exist.'));
}
}
}
示例14: validatePath
/**
* {@inheritdoc}
*/
public function validatePath(&$element, FormStateInterface $form_state)
{
// Ensure the path has a leading slash.
$value = '/' . trim($element['#value'], '/');
$form_state->setValueForElement($element, $value);
// Ensure each path is unique.
$path = $this->entityQuery->get('page')->condition('path', $value)->condition('id', $form_state->getValue('id'), '<>')->execute();
if ($path) {
$form_state->setErrorByName('path', $this->t('The page path must be unique.'));
}
}
示例15: validate
/**
* Validate the color text field.
*/
public function validate($element, FormStateInterface $form_state)
{
$value = $element['#value'];
if (strlen($value) == 0) {
$form_state->setValueForElement($element, '');
return;
}
if (!preg_match('/^#([a-f0-9]{6})$/iD', strtolower($value))) {
$form_state->setError($element, t("Color must be a 6-digit hexadecimal value, suitable for CSS."));
}
}