You shoudn't use css property for required field marking..., but you
can replace css class on any html.
I am using nex solution:
1. Modify base form class lib/form/doctrine/BaseFormDoctrine.class.php
abstract class BaseFormDoctrine extends sfFormDoctrine
{
const REQUIRED_CLASS_NAME = 'required';
public function __construct($object = null, $options = array(),
$CSRFSecret = null)
{
parent::__construct($object, $options, $CSRFSecret);
$this->postSetup();
}
public function setup()
{
}
/**
* Post setup final method, cannot be overriden
*
*/
final private function postSetup()
{
$this->handleRequiredFields();
}
/**
* Adds a CSS class name to required widgets
*
*/
protected function handleRequiredFields()
{
if (!$this->validatorSchema)
{
return;
}
foreach ($this->validatorSchema->getFields() as $fieldName =>
$validator)
{
/* @var $validator sfValidatorBase */
if (true === $validator->getOption('required'))
{
if (!array_key_exists($fieldName, $this->widgetSchema-
>getFields()))
{
continue;
}
/* @var $widget sfWidget */
$widget = $this->widgetSchema[$fieldName];
$class = trim($widget->getAttribute('class'));
if (false===strpos($class, self::REQUIRED_CLASS_NAME))
{
$class = sprintf('%s %s', $class,
self::REQUIRED_CLASS_NAME);
$widget->setAttribute('class', $class);
}
}
}
}
}
2. Create your custom schema formater:
class sfWidgetFormSchemaFormatterRequired extends
sfWidgetFormSchemaFormatter
{
public function formatRow($label, $field, $errors = array(), $help =
'', $hiddenFields = null)
{
return strtr($this->getRowFormat(), array(
'%errorclass%' => (count($errors) ?' class="error"' : ''),
'%required%' => (false!==strpos($field, 'required') ?
'<span>*</span>' : ''), // Look here! This is the place where added
asterisk. You can add your own code.
'%label%' => $label,
'%field%' => $field,
'%error%' => $this->formatErrorsForRow($errors),
'%help%' => $this->formatHelp($help),
'%hidden_fields%' => is_null($hiddenFields) ? '%hidden_fields
%' : $hiddenFields,
));
}
}
3. In the form add form formatter:
classYourForm extends BaseFormDoctrine
{
public function setup()
{
...
$this->widgetSchema->setDefaultFormFormatterName('Required');
...
}
}
All of this steps was described at the link
http://groups.google.com/group/symfony-devs/browse_thread/thread/4c81264521f8bc60
On Feb 26, 1:42 pm, Tomasz Ignatiuk <[email protected]> wrote:
> It is not good solution because using css pre doesn't work in
> IE...which is used by....I don't know...50% of users?
>
> On 26 Lut, 06:58, avorobiev <[email protected]> wrote:
>
> > Look
> > athttp://groups.google.com/group/symfony-devs/browse_thread/thread/4c81...
>
> > On Feb 24, 2:20 pm, Tomasz Ignatiuk <[email protected]> wrote:
>
> > > On 16 Sty, 13:35, Thomas Dedericks <[email protected]> wrote:
>
> > > > Hi,
>
> > > > Not tested:
>
> > > > public function configure() {
>
> > > > ...
>
> > > > # add * torequiredfields' labels:
> > > > foreach ($this->widgetSchema as $name => $widget)
> > > > {
> > > > if (isset($this->validatorSchema[$name]) &&
> > > > $this->validatorSchema[$name]->hasOption('required') &&
> > > > $this->validatorSchema[$name]->getOption('required') == true)
> > > > {
> > > > $label = ($widget->getLabel() != null) ?
> > > > $widget->getLabel() : $name;
> > > > $widget->setLabel($label.' *');
> > > > }
> > > > }
>
> > > > }
>
> > > > Thomas Dedericks
>
> > > Unfortunatelly it doesn't work. But if you do some stuff before and
> > > with little change...it works :)
> > > 1. Required option is true as default. I don't know why it isn't set
> > > in widgetSchema. So this won't work:
> > > $this->validatorSchema[$name]->hasOption('required') &&
> > > $this->validatorSchema[$name]->getOption
>
> > > ('required') == true
> > > 2. If label is set in generator.yml, it isn't set in widgetSchema, so
> > > $widget->getLabel() != null is always null You have to set it manually
> > > in configure()
> > > 3. widgetSchema i a set of objects, not name fields, so you have to
> > > add this: foreach ($this->widgetSchema->getFields() as $name =>
> > > $widget)
>
> > > So you have to set labels for requried fields in configure() of a form
> > > and for some fields, like boolean you have to add manually required to
> > > true, like this: $this->validatorSchema['master'] = new
> > > sfValidatorBoolean(array('required' => true)); And also change this:
> > > && $this->validatorSchema[$name]->getOption('required') != false).....
> > > required != false
>
> > > foreach ($this->widgetSchema->getFields() as $name => $widget)
> > > {
> > > if (isset($this->validatorSchema[$name]) &&
> > > $this->validatorSchema[$name]->hasOption('required') && $this-
> > > >validatorSchema[$name]->getOption('required') != false)
>
> > > {
> > > $label = ($widget->getLabel() != null) ? $widget->getLabel() :
> > > $name;
> > > $widget->setLabel($label.' *');
> > > }
> > > }
>
> > > All in all it is faster just to add * manually for required fields:
> > > $this->widgetSchema->setLabels(array(
> > > 'logo' => 'Logo *',
> > > 'name' => 'Name *',
> > > 'master' => 'Master *',
> > > ));
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---