Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to validate CMS fields


Go to End


3 Posts   473 Views

Avatar
Erick Garcia

Community Member, 2 Posts

23 April 2015 at 5:08am

Edited: 23/04/2015 5:09am

Hi guys, I was wondering how can I validate a field list, I saw many examples but using form, and I need to validate this field and use it inside a GriedField:

 $fields = FieldList::create(
      $categories=DropdownField::create('ParentCat','Categoria',array(""=>"Seleccionar",1=>"Para Comprar",2=>"Para Disfrutar")),
      $subcat=TextField::create('Title','Subcategoria'),
      $icon=TextField::create('Icon','Icono'),
      TextField::create('BackgroundColor','Fondo categoria')
    );
  //I have tried this, but how to trigger it?
 $validator=new RequiredFields('ParentCat');
return $fields

Thanks in advanced.

Avatar
Devlin

Community Member, 344 Posts

23 April 2015 at 7:41pm

public function getCMSFields() {
	//DropdownField::create('ParentCat');
}

public function getCMSValidator() {
	$valid = new RequiredFields();
	$valid->addRequiredField('ParentCat');
	return $valid;
}

Avatar
Erick Garcia

Community Member, 2 Posts

24 April 2015 at 6:19am

I really appreciate your help Devlin, thank u very much man!
I'll leave the complete code here if any one have the same question.

class GaleriaInicio extends DataObject {
  private static $db = array (
    'Title' => 'Varchar'
  );
 public function getCMSValidator(){
    $valid=new RequiredFields();
    $valid->addRequiredField('Title');
    return $valid;
  }

  public function getCMSFields() {
    $fields = FieldList::create(
      TextField::create('Title','Titulo'),
      $uploader = UploadField::create('Photo')
    );
    return $fields;
  }
}