1212use Ajax \semantic \html \collections \form \HtmlFormInput ;
1313use Ajax \semantic \html \collections \form \HtmlFormDropdown ;
1414use Ajax \semantic \html \collections \form \HtmlFormTextarea ;
15+ use Ajax \semantic \html \collections \form \HtmlFormFields ;
16+ use Ajax \semantic \html \collections \HtmlMessage ;
1517
1618/**
1719 * @author jc
2123 */
2224trait FieldAsTrait{
2325
24- abstract protected function _getFieldIdentifier ($ prefix );
26+ abstract protected function _getFieldIdentifier ($ prefix, $ name = "" );
2527 abstract public function setValueFunction ($ index ,$ callback );
28+ abstract protected function _getFieldName ($ index );
29+ abstract protected function _getFieldCaption ($ index );
2630
2731 /**
2832 * @param HtmlFormField $element
@@ -45,17 +49,31 @@ private function _getLabelField($caption,$icon=NULL){
4549 return $ label ;
4650 }
4751
48- protected function _addRules ($ element ,&$ attributes ){}
52+ protected function _addRules ($ element ,&$ attributes ){
53+ if (isset ($ attributes ["rules " ])){
54+ $ rules =$ attributes ["rules " ];
55+ if (\is_array ($ rules )){
56+ $ element ->addRules ($ rules );
57+ }
58+ else
59+ $ element ->addRule ($ rules );
60+ unset($ attributes ["rules " ]);
61+ }
62+ }
4963
5064 protected function _fieldAs ($ elementCallback ,$ index ,$ attributes =NULL ,$ prefix =null ){
5165 $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,&$ attributes ,$ elementCallback ,$ prefix ){
52- $ name =$ this ->_instanceViewer ->getFieldName ($ index )."[] " ;
66+ $ caption =$ this ->_getFieldCaption ($ index );
67+ $ name =$ this ->_getFieldName ($ index );
68+ $ id =$ this ->_getFieldIdentifier ($ prefix ,$ name );
5369 if (isset ($ attributes ["name " ])){
5470 $ name =$ attributes ["name " ];
71+ unset($ attributes ["name " ]);
5572 }
56- $ element =$ elementCallback ($ this -> _getFieldIdentifier ( $ prefix ) ,$ name ,$ value ,"" );
57- if (\is_array ($ attributes ))
73+ $ element =$ elementCallback ($ id ,$ name ,$ value ,$ caption );
74+ if (\is_array ($ attributes )){
5875 $ this ->_applyAttributes ($ element , $ attributes ,$ index );
76+ }
5977 $ element ->setDisabled (!$ this ->_edition );
6078 return $ element ;
6179 });
@@ -120,9 +138,15 @@ public function fieldAsRadio($index,$attributes=NULL){
120138 }, $ index ,$ attributes ,"radio " );
121139 }
122140
141+ public function fieldAsRadios ($ index ,$ elements =[],$ attributes =NULL ){
142+ return $ this ->_fieldAs (function ($ id ,$ name ,$ value ,$ caption ) use ($ elements ){
143+ return HtmlFormFields::radios ($ name ,$ elements ,$ caption ,$ value );
144+ }, $ index ,$ attributes ,"radios " );
145+ }
146+
123147 public function fieldAsInput ($ index ,$ attributes =NULL ){
124- return $ this ->_fieldAs (function ($ id ,$ name ,$ value ){
125- $ input = new HtmlFormInput ($ id ,"" ,"text " ,$ value );
148+ return $ this ->_fieldAs (function ($ id ,$ name ,$ value, $ caption ){
149+ $ input = new HtmlFormInput ($ id ,$ caption ,"text " ,$ value );
126150 $ input ->setName ($ name );
127151 return $ input ;
128152 }, $ index ,$ attributes ,"input " );
@@ -148,17 +172,24 @@ public function fieldAsCheckbox($index,$attributes=NULL){
148172 return $ this ->_fieldAs (function ($ id ,$ name ,$ value ){
149173 $ input =new HtmlFormCheckbox ($ id ,NULL ,$ this ->_instanceViewer ->getIdentifier ());
150174 $ input ->setChecked (JString::isBooleanTrue ($ value ));
151- $ input ->getField ()-> setProperty ( " name " , $ name );
175+ $ input ->setName ( $ name );
152176 return $ input ;
153177 }, $ index ,$ attributes ,"ck " );
154178 }
155179
156180 public function fieldAsDropDown ($ index ,$ elements =[],$ multiple =false ,$ attributes =NULL ){
157181 return $ this ->_fieldAs (function ($ id ,$ name ,$ value ) use ($ elements ,$ multiple ){
158- //$dd=new HtmlDropdown($id,$value,$elements);
159182 $ dd =new HtmlFormDropdown ($ id ,$ elements ,NULL ,$ value );
160183 $ dd ->asSelect ($ name ,$ multiple );
161184 return $ dd ;
162185 }, $ index ,$ attributes ,"dd " );
163186 }
187+
188+ public function fieldAsMessage ($ index ,$ attributes =NULL ){
189+ return $ this ->_fieldAs (function ($ id ,$ name ,$ value ,$ caption ){
190+ $ mess = new HtmlMessage ("message- " .$ id ,$ value );
191+ $ mess ->addHeader ($ caption );
192+ return $ mess ;
193+ }, $ index ,$ attributes ,"message " );
194+ }
164195}
0 commit comments